Android IP Camera - android

I'm looking for a good working solution to integrate an IP-Cameras video stream in my Android App. At the moment i am using the "Axis P1214-E" which has a good image quality, but i couldn't get a "LIVE" stream from it. Either the stream is very laggy or it's a few seconds delayed (sometimes even more) or the stream is shutting down after awhile. What i tried so far:
Using a SurfaceView to get the MJPEG Stream as described in this post: Android and MJPEG
Problem: Lagging
Using a WebView to get the RTSP stream:
public class MainActivity extends Activity {
private static final String TAG = "VideoViewExample.MainActivity";
private static final String RTSP_URL = "rtsp://ip/axis-media/media.amp";
private VideoView videoView;
private MediaController mediaController;
private OnPreparedListener opl;
private int position = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null)
position = savedInstanceState.getInt("POSITION");
videoView = (VideoView) findViewById(R.id.videoView);
opl = new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.seekTo(position);
if (position == 0) {
videoView.start();
}
else {
videoView.pause();
}
}
};
if (mediaController == null){
mediaController = new MediaController(this);
}
mediaController.setAnchorView(videoView);
AsyncTask<Void, Void, Void> at = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
try{
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(RTSP_URL));
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
videoView.requestFocus();
videoView.setOnPreparedListener(opl);
return null;
}
};
at.execute();
}
#Override
protected void onPause() {
position = videoView.getCurrentPosition();
super.onPause();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("POSITION", position);
}
}
Problem: Good video Quality, but delayed.
Using external frameworks like FFMPEG and GSTREAMER (Only some examples so far)
Problem: Also very laggy and/or delayed.
Now i'm running out of ideas to get this working. It's very important for my Application that the stream is live and not lagging.
I'm developing on a "Banana Pi" board with Android 4.2.2 (4.4 is possible as well).
Does anybody know how to get this working? Or maybe should i use an other camera? Do you have any suggestions that would work well with android?
Thanks in advance
Christian

Related

How to play video upto specific time, from main video in android

public class MergeVideo extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myfirstpage);
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoPath("/storage/emulated/0/Android/data/com.example.android.camera2video/files/a.mp4");
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.seekTo(6000);
myVideoView.start();
}
}
this is my code for play video . i have one main video file 19 second length i want to play video from 6 second to 12 second i am able to start video from 6 second . i don't know how to stop video play on 12 sec please suggest me how implement this .
Not tested this, but it should work in your case.
private VideoView mVideoView;
private boolean mShouldStop = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVideoView = (VideoView) findViewById(R.id.myvideoview);
mVideoView.setVideoPath("/storage/emulated/0/Android/data/com.example.android.camera2video/files/a.mp4");
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.seekTo(6000);
mVideoView.start();
trackProgress();
}
private void trackProgress() {
new Thread(new Runnable() {
#Override
public void run() {
while (!mShouldStop) {
if (mVideoView != null && mVideoView.isPlaying()) {
if (mVideoView.getCurrentPosition() >= 12000) {
mVideoView.stopPlayback();
mShouldStop = true;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
Since the VideoView documentation doesn't have hooks for this, let's take a step back: what do we want to do?
Play the video (already done)
After x amount of time, stop the video (to do)
Implement a timer
We want to play a video for an amount of time, then stop the video. Why not implement a Timer (see documentation)?
On this timer, you can schedule a task to be performed.
One caveat: Don't forget to cancel() the timer. Otherwise it'll keep calling the method to pause/stop the video.

Video not playing but audio plays fine on Live Streaming in android

I have a VideoView which is used to play live stream. I am not able to find where the problem occurs.
In the beginning, I used RTMP to play the live stream using Vitamio which resulted in playing the audio leaving the screen blank. I checked out with the RTMP link, it works fine with a website. I surfed a lot about this, but I did not find any solution.
So now, I switched to HTTP to play the live stream which also results in the same problem (i.e audio playing fine but video is blank).
I am expecting a solution for either RTMP or HTTP.
Any Suggestions???
UPDATE 1: I have found a problem with the link. I used VLC Media Player to check whether my RTMP and HTTP link is working fine or not. The RTMP link works fine whereas the problem was with the HTTP link. The HTTP link plays only the audio.
On the other hand, having the RTMP link working fine, it doesn't solve the problem when using Vitamio. Any Suggestions for RTMP??
Here is my code:
public class ITVLiveStreamActivity extends Activity {
private VideoView liveVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_itvlive_stream);
liveVideoView = (VideoView) findViewById(R.id.liveVideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(liveVideoView);
Uri uri = Uri.parse(getIntent().getStringExtra("rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw"));
liveVideoView.setVideoURI(uri);
liveVideoView.setMediaController(mediaController);
liveVideoView.requestFocus();
liveVideoView.start();
}
}
UPDATE 2:
Here is my code using Vitamio:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
public class ITVLiveStreamActivity extends Activity {
private String pathToFileOrUrl="rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw";
private VideoView mVideoView;
private ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.activity_itvlive_stream);
mVideoView = (VideoView)findViewById(R.id.vitamio_videoView);
progDailog = new ProgressDialog(this);
progDailog.setCancelable(false);
progDailog.setMessage(getResources().getString(R.string.loading));
progDailog.show();
mVideoView.setVideoPath(pathToFileOrUrl);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
progDailog.dismiss();
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
I am getting the same result (i.e) Audio is playing but video is blank.
Here I have added the screenshot
You can create an activity like this to open default Video View
public class VideoViewActivity extends Activity {
// Declare variables
ProgressDialog pDialog;
VideoView videoview;
// Insert your Video URL
// String VideoURL; /*= "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";*/
String VideoURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.video_view);
// Find your VideoView in your video_main.xml layout
videoview = (VideoView) findViewById(R.id.VideoView);
// Execute StreamVideo AsyncTask
VideoURL = getIntent().getExtras().getString(Constants.LINK);
// Log.v("video", VideoURL);
// Create a progressbar
pDialog = new ProgressDialog(VideoViewActivity.this);
// Set progressbar title
//pDialog.setTitle("Android Video Streaming Tutorial");
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoViewActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
// Log.e("Error", e.getMessage());
pDialog.dismiss();
e.printStackTrace();
} finally {
videoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
CommonUtilities.showToast(VideoViewActivity.this, "Video Format not supported by device.");
VideoViewActivity.this.finish();
return true;
}
});
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
}
}
hi i have done this and its working fine here is code
public class PlayingLiveStream extends Activity {
VideoView vvmyliveplaying;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playinglivestream);
vvmyliveplaying = (VideoView) findViewById(R.id.vvmyliveplaying);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vvmyliveplaying);
Uri uri = Uri.parse(getIntent().getStringExtra("url_play"));
vvmyliveplaying.setVideoURI(uri);
vvmyliveplaying.setMediaController(mediaController);
vvmyliveplaying.requestFocus();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
try {
vvmyliveplaying.pause();
finish();
} catch (Exception e) {
}
}
return false;
}
}
<VideoView
android:id="#+id/vvmyliveplaying"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Edited Answer
I have tested streaming on vitamio library demo sample and its working fine.
public class VideoViewDemo extends Activity {
private VideoView mVideoView;
private String pathToFileOrUrl ="rtmp://61.16.143.170:1935/live/7khh-8fhu-vxd3-8fuw";
private ProgressDialog progDailog;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);
progDailog = new ProgressDialog(this);
progDailog.setCancelable(false);
progDailog.setMessage("Please wait");
progDailog.show();
mVideoView.setVideoPath(pathToFileOrUrl);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
progDailog.dismiss();
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
}
}
and xml layout,
<io.vov.vitamio.widget.VideoView
android:id="#+id/surface_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Now i'm sure code work well.
Thanks hope this will help you .

Playing video from the pause state in VideoView

For custom videoview I had to override the MediaController (to change the design of the buttons and connect to the buttons pause / play a selector). The situation is this: put the video on pause and turning the application, and then again restored, the video starts playing again, and not from the point at which it was suspended, and therefore the question arose: how to make the video starts playing from the place , where it was stopped? What do I need to change in the VideoActivity?
public class VideoActivity extends Activity {
VideoView videoView;
VkMediaController mc;
private static final String CURRENT = "duration";
private static final String URL = "url";
private Uri mURI;
private int mCurrentPosition = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.video_view_player);
videoView = (VideoView) findViewById(R.id.videoViewplayer);
mc = new VkMediaController(this);
mURI = getIntent().getData();
if (savedInstanceState != null) {
mURI = Uri.parse(savedInstanceState.getString(URL));
mCurrentPosition = savedInstanceState.getInt(CURRENT);
}
videoView.setMediaController(mc);
videoView.setVideoURI(mURI);
videoView.requestFocus();
}
#Override
protected void onStart() {
super.onStart();
videoView.start();
if (mCurrentPosition != -1) {
videoView.seekTo(mCurrentPosition);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(CURRENT, videoView.getCurrentPosition());
outState.putString(URL, mURI.toString());
super.onSaveInstanceState(outState);
}
}
I think the problem is that videoView.getCurrentPosition() will always return 0 when you call inside onSaveInstanceState(Bundle outState), cos at that time the video already reset, so call videoView.getCurrentPosition() when onPause(), this will return you the desired value.
I had the same problem. I thought I did everything right but it didnt work. Then I just changed the order so I first call VideoView.seekTo() and then VideoView.start() . That worked.
I also have VideoView.requestFocus() in there but I dont know if that matters.
videoPlayer.requestFocus();
videoPlayer.seekTo(position);
videoPlayer.start();
So if youre sure you position variable has the right value, this might be your answer.
You may not be able to seek when the Video is not loaded. Implement onPreparedListener like this:
public class VideoActivity extends Activity implements OnPreparedListener {
VideoView videoPlayer;
int position;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null){
position=savedInstanceState.getInt("pos")
}
// setting up the videoview
setContentView(R.layout.activity_video);
getIntent().getExtras().getString("url");
Uri videouri = Uri.parse(getIntent().getExtras().getString("url"));
videoPlayer = (VideoView) findViewById(R.id.videoView);
videoPlayer.setOnPreparedListener(this);
videoPlayer.setKeepScreenOn(true);
videoPlayer.setVideoURI(videouri);
}
/**
* Start the plaback when the video is loaded
*
* #param mp
* #see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer)
*/
public void onPrepared(MediaPlayer mp) {
//this is a TextView infront of the VideoView which tells the User the Video is loading- hide that
findViewById(R.id.tv_video_load).setVisibility(View.GONE);
videoPlayer.requestFocus();
videoPlayer.seekTo(position)
videoPlayer.start();
}
}
Portrait to LandScape LifeCycle Demo
VideoView videoView;
int lastPosition;
int Video_STATE_POSITION = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
String message = savedInstanceState.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
lastPosition=savedInstanceState.getInt("STATE_POSITION");
videoView.seekTo(lastPosition);
videoView.start();
Log.e("Keshav", "MainActivity onCreate() is called savedInstanceState lastPosition->" + lastPosition);
}
}
#Override
public void onSaveInstanceState(Bundle outState)
{
outState.putString("message", "This is my message to be reloaded");
outState.putInt("STATE_POSITION", Video_STATE_POSITION);
super.onSaveInstanceState(outState);
}
#Override
protected void onPause() {
super.onPause();
Video_STATE_POSITION = videoView.getCurrentPosition();
Log.e("Keshav", "MainActivity onPause is called ->" +Video_STATE_POSITION);
}

Android: Video cannot be played from non hard-coded URL

I'm trying to stream a video and play it using VideoView. I supply the view with the source URL of the video with setVideoURI() as shown below. With a hardcoded value like urlString = "www.myvideoserver.com/videos/bigbuckbunny.mp4", the video plays fine. However, when urlString is given the value from the intent (coming from the previous activity where the user chose the video), I get the message: "Sorry, Video cannot be played". I've read that one of the common causes is video format, like it's described here and here. I'm almost certain that it is not a format issue because I can play the video when the URL is fixed (and I know this because I can see from Log.d("PVurl", urlString); that the value of urlString is exactly the same as the one I fixed it to. That is, in LogCat, I copy paste the value into the line urlString = getIntent()... // "www.myvideoserver.com/videos/bigbuckbunny.mp4", and it works but not when urlString is set to the intent return value . LogCat Errror panel gives the following:
04-13 17:35:32.786: ERROR/MediaPlayer(2620): error (1, -1007)
04-13 17:35:32.786: ERROR/MediaPlayer(2620): Error (1,-1007)
I've searched around the internet but it doesn't seem that anyone has encountered such an error code before.
I'd very much appreciate if anyone has any idea what could be the problem. Thanks!
public void playvideo () { // obtain URL of the requested video from the intent in previous activity
try
{
urlString = getIntent().getStringExtra("mypackage.fulladdr");
if (urlStr != null)
{
Log.d("PVurl", urlString);
VideoView v = (VideoView) findViewById(R.id.videoView1);
// play video
v.setVideoURI(Uri.parse(urlString));
v.setMediaController(new MediaController(this));
v.start();
v.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
showRatingDialog();
}
});
}
}
catch (Exception e)
{
Log.d("PV_TAG", +e.getMessage());
e.printStackTrace();
}
}
You haven't added the scheme which the Uri needs to find out what it is referring to (http, in your case) .
urlString = "www.myvideoserver.com/videos/bigbuckbunny.mp4" change it to urlString = "http://www.myvideoserver.com/videos/bigbuckbunny.mp4"
(1, -1007) error means:
MEDIA_ERROR_UNKNOWN - "File or network related operation errors."
this may come from a corrupt file
see also javadoc of android.media.MediaPlayer.OnErrorListener#onError
http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html#onError
#param what the type of error that has occurred:
MEDIA_ERROR_UNKNOWN
MEDIA_ERROR_SERVER_DIED
#param extra an extra code, specific to the error. Typically implementation dependent.
MEDIA_ERROR_IO
MEDIA_ERROR_MALFORMED
MEDIA_ERROR_UNSUPPORTED
MEDIA_ERROR_TIMED_OUT
I tried the same thing and was able to get it to work. Perhaps something else is going on in your code. Here is what I have:
// Intent to start the activity that launches the video player
Intent i = new Intent(this, ExampleActivity.class);
i.putExtra("url", "http://www.yourvideo.com/yourvid.m4v");
startActivity(i);
And here is the code that deals with the player:
private MediaController mController;
private Uri videoUri;
private int percentBuffered= 0;
private VideoView videoView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mController = new MediaController(this);
videoView = (VideoView) findViewById(R.id.videoView1);
// access String from the intent that launched this Activity
videoUri = Uri.parse(getIntent().getStringExtra("url"));
}
#Override
public void onResume() {
super.onResume();
videoView.setOnCompletionListener(this);
try {
videoView.setVideoURI(videoUri);
mController.setMediaPlayer(videoView);
videoView.setMediaController(mController);
videoView.requestFocus();
videoView.start();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mController.show();
return super.onTouchEvent(event);
}
#Override
public int getBufferPercentage() {
return percentBuffered;
}
#Override
public int getCurrentPosition() {
return videoView.getCurrentPosition();
}
#Override
public int getDuration() {
return videoView.getDuration();
}
#Override
public boolean isPlaying() {
return videoView.isPlaying();
}
#Override
public void pause() {
videoView.pause();
}
#Override
public void seekTo(int pos) {
videoView.seekTo(pos);
}
#Override
public void start() {
videoView.start();
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
percentBuffered = percent;
}
#Override
public void onCompletion(MediaPlayer mp) {
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
Alright, I found the problem after many futile hours of digging through to find what MediaPlayer error(1, -1007) means.
In reality, it has to do with the rather unfortunate fact that the line
getIntent().getStringExtra("mypackage.fulladdr");
returns a string with an extra white space at the end. I think it's an implementation issue with getIntent().
Anyone knows where to report these kind of bugs, that'd be great.
Thanks to #Akhil and #javaMe for their attempts!

How to Play mp4 Video in Android Emulator Using Remote URL?

How to Play mp4 Video in Android Emulator Using Remote URL ? I used following code but this code give me error "Sorry, this video cannot be played".
07-05 16:58:19.525: INFO/AwesomePlayer(34): mConnectingDataSource->connect() returned -1007
07-05 16:58:19.525: ERROR/MediaPlayer(1242): error (1, -1007)
07-05 16:58:19.525: ERROR/MediaPlayer(1242): Error (1,-1007)
07-05 16:58:19.525: DEBUG/VideoView(1242): Error: 1,-1007
My Code is:-
public class VideoPlayerController extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
String Video="http://s509.photobucket.com/albums/s338/eveanthony/?action=view&current=Video013.mp4";
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(Video));
videoView.start();
}
}
You need to execute the app on original device rather than emulator since it does not supports playing video files. In rare cases it may but it really depends upon your system configurations.
Android 4.1.2 version seems to play mp4 video in the emulator in youtube app, not elsewhere. I tested it. Both Intel and non-Intel version work. 4.0.3 did not play them.
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private static final String Videos_URL = "*Your_URI*";
// Get the layout from video_main.xml
setContentView(R.layout.activity_main);
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.videoView);
// Create a progressbar
progressDialog = new ProgressDialog(this);
// Set progressbar title
progressDialog.setTitle("Anything u Want");
// Set progressbar message
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
// Show progressbar
progressDialog.show();
try {
Uri video = Uri.parse(Videos_URL);
myVideoView.setVideoURI(video);
myVideoView.setMediaController(mediaControls);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
myVideoView.pause();
}
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
}

Categories

Resources