How can I check if a streaming rtsp on android ends? - android

I have an application that show a streaming rtsp. How can I check if it ends?
Buffer empty? Anything else?

Socket is closed
Anything on control channel from media server?

private VideoView vidView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vidView = (VideoView) findViewById(R.id.myVideo);
uri = Uri.parse(RTSP_URL);
vidView.setMediaController(new MediaController(this));
vidView.setVideoURI(uri);
vidView.requestFocus();
vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Runnable runnable = new Runnable() {
#Override
public void run() {
vidView.start();
}
};
runOnUiThread(runnable);
}
});
vidView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("Error ", what + " " + extra);
return false;
}
});
vidView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(getClass().getName(), " callback COMPLETE HERE ");
}
});
}

Related

Detecting buffering error (or time_out) if media player is unable to load audio content from server

I am developing an android application which will play live radio audio stream. I have implemented android media player for this purpose which constantly receives the audio data from a remote source.
The play button will show a buffering ProgressDialog to the user on click of it and will start the media player. I am successful in doing all so, but the only problem that I face is that I want to notify the user if the media player is unable to load audio content from the streaming server. Is there a timeout or some sort of an error that I can catch? Nothing is currently being thrown, if media player is unable to load audio content form server:
My code looks like:
public class MainActivity extends AppCompatActivity
{
Button playbtn_flat,stopbtn_flat;
static MediaPlayer mPlayer;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playbtn_flat = (Button) findViewById(R.id.play_button);
stopbtn_flat = (Button) findViewById(R.id.stop_button);
playbtn_flat.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
listen_live("http://192.168.**.***:port/mountpoint");
}
});
stopbtn_flat.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
}
}
});
}
void listen_live(String stream_ip) {
try {
final ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Please Wait...", "Buffering");
mPlayer = new MediaPlayer();
final MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
mp.start();
}
};
final MediaPlayer.OnErrorListener onErrorListener = new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
Log.e("abc", String.format("Error(%s%s)", what, extra));
return false;
}
};
final Handler onPreparedHandle = new Handler() {
#Override
public void handleMessage(Message msg) {
mPlayer.setOnErrorListener(onErrorListener); // set onErrorListener
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
onPreparedListener.onPrepared(mPlayer);
}
};
final String url = stream_ip;
new Thread() {
#Override
public void run() {
mPlayer = MediaPlayer.create(MainActivity.this, Uri.parse(url)); // create new instance of MediaPlayer and prepare it
onPreparedHandle.sendMessage(onPreparedHandle.obtainMessage()); // send message to main thread
}
}.start();
} catch (Exception e) {
Log.e("abc ", e.toString());
}
}
}

Live streaming does not working when try to play live streaming videos using vitamio library

Here is my Class
public class VideoViewActivity extends Activity implements OnInfoListener, OnBufferingUpdateListener {
boolean isStart;
VideoView mVideoView;
ProgressBar pb;
TextView downloadRateView, loadRateView;
String videourl = "";
ImageView img_add;
ImageLoader imageLoader = ImageLoader.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.video_view);
img_add = (ImageView) findViewById(R.id.img_add);
// imageLoader.init(GetOption.getConfig(this));
WebAPIHelper web = new WebAPIHelper(Constant.videoaddimage, VideoViewActivity.this, true);
web.callRequestGet("http://fillatv.com/fillatv/service/get_banners.php");
pb = (ProgressBar) findViewById(R.id.probar);
downloadRateView = (TextView) findViewById(R.id.download_rate);
loadRateView = (TextView) findViewById(R.id.load_rate);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
Button btn_back = (Button) findViewById(R.id.back);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
VideoViewActivity.this.finish();
}
});
Intent intent = getIntent();
videourl = ((DataContainer) intent.getSerializableExtra("v_url")).link;
//videourl = intent.getStringExtra("v_url");
PlayVideo();
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
private void PlayVideo() {
Log.e("video url", videourl);
Uri uri = Uri.parse(videourl);
mVideoView.setVideoURI(uri);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnInfoListener(this);
mVideoView.setOnBufferingUpdateListener(this);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
mVideoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getApplicationContext(), "no Streaming Found try after some time", Toast.LENGTH_LONG).show();
finish();
return false;
}
});
}
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
if (mVideoView.isPlaying()) {
mVideoView.pause();
isStart = true;
pb.setVisibility(View.VISIBLE);
downloadRateView.setVisibility(View.VISIBLE);
loadRateView.setVisibility(View.VISIBLE);
}
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
if (isStart) {
mVideoView.start();
pb.setVisibility(View.GONE);
downloadRateView.setVisibility(View.GONE);
loadRateView.setVisibility(View.GONE);
}
break;
case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
downloadRateView.setText(" " + extra + "kb/s" + " ");
break;
}
return true;
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
loadRateView.setText(percent + "%");
}
public void viewdata(String data) {
String img;
try {
JSONArray j_array = new JSONArray(data);
for (int i = 0; i < j_array.length(); i++) {
JSONObject j_obj = j_array.getJSONObject(i);
System.out.print(j_obj.getString("status"));
System.out.print(j_obj.getString("id"));
System.out.print(j_obj.getString("Image"));
img = j_obj.getString("Image");
imageLoader.displayImage(img, img_add, GetOption.getOption());
}
} catch (Exception e) {
}
}
}
When Try to run it shows below error. spent one day to resolve this but can't find important hope you guys have solution for this.
E/Vitamio[4.2.6][Player]: FIND_NAME_SYM vvo, render_yuv
11-26 18:59:29.556 28707-28882/com.fillatv.live E/Vitamio[4.2.6][Player]: avformat_open_input: Invalid data found when processing input : -1094995529
11-26 18:59:29.556 28707-28879/com.fillatv.live E/Vitamio[4.2.6][Player]: error (1, -1094995529)
11-26 18:59:29.637 28707-28707/com.fillatv.live E/Vitamio[Player]: Error (1, -1094995529)
If anyone needs more detail please tell me,I will provide.Any help would be appreciated,Thanks in advance.

MediaPlayer: error (-19, 0)

I tried all the solutions provided in this site & still i am getting this error.Don't know why it happen! please help me.
Here my code.
MediaPlayer mediaPlayer;
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.square);
mediaPlayer.setOnErrorListener(MainActivity.this);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mWinMusic[blocks].release();
}
});
public boolean onError(MediaPlayer mp, int what, int extra) {
mp = MediaPlayer.create(getApplicationContext(), R.raw.square);
mp.start();
return true;
}
Create player like this and add mediaPlayer.prepare(); and when ready then start in onPrepared()
public class MainActivity extends Activity {
MediaPlayer mediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.square);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(MainActivity.this, "Some problem ", Toast.LENGTH_LONG).show();
return false;
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(MainActivity.this, "track completed", Toast.LENGTH_SHORT).show();
//mWinMusic[blocks].release();
}
});
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Some problem " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}

How to prepare mediaplayer in android while another playing?

I using MediaPlayerto play video from http stream is any solution to prepare second media player with another http url while first one still playing?
I want to play two videos without black screen between them, when first one end second start immediately.
Here is my code:
public class VideoActivity extends Activity implements SurfaceHolder.Callback {
private MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private SurfaceView playerSurfaceView;
private String videoSrc = "http://192.168.1.101:8090/api/video/";
private String android_id;
private MediaPlayer next;
private static boolean firstCall = true;
private String secondVideoSrc = "http://192.168.1.101:8090/api/secondvideo/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
playerSurfaceView = (SurfaceView)findViewById(R.id.playersurface);
surfaceHolder = playerSurfaceView.getHolder();
surfaceHolder.addCallback(this);
GetDeviceId();
videoSrc += android_id;
secondVideoSrc += android_id;
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
prepareVideoPlayer();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
private void GetDeviceId() {
android_id = Settings.Secure.getString(getBaseContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
}
private void prepareVideoPlayer() {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setDataSource(this, Uri.parse(videoSrc));
mediaPlayer.setScreenOnWhilePlaying(true);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if(firstCall) {
mediaPlayer.start();
firstCall = false;
}
new AsyncPrepareNext().execute();
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
next.start();
}
});
}
catch(Exception ex) {
}
}
private void prepareNext() {
try {
next = new MediaPlayer();
next.setDisplay(surfaceHolder);
next.setDataSource(this, Uri.parse(secondVideoSrc));
next.setScreenOnWhilePlaying(true);
next.prepareAsync();
next.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
}
});
next.setAudioStreamType(AudioManager.STREAM_MUSIC);
next.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
}
catch(Exception ex) {
}
}
private class AsyncPrepareNext extends AsyncTask<Void,Void,Void> {
#Override
protected Void doInBackground(Void... params) {
prepareNext();
return null;
}
}
}
When i run this first video starts for 2 or 3 seconds and then stop with some errors like media server died, audio flinger died, error (100,0), error (38,0)
You'll probably need to implement the OnPreparedListener() and OnCompletedListener() to accomplish this and you might need 2 media players for it as well.
#Override
public void onPrepared(MediaPlayer mp){
// Set Video Source for second video
mp.setDataSource(path);
// Prepare second video
mp.prepareAsync();
}
#Override
public void onCompletion(MediaPlayer mp){
// Start second video here
mp.start();
}
and set them like this:
mediaPlayer.setOnPreparedListener(new OnPreparedListener());
mediaPlayer.setOnCompletedListener(new OnCompletionListener());

Android - Where to implement onBufferingUpdate(...) using VideoView and MediaController

I am confused by the MediaController and VideoView APIs in Android.
I am using it to stream videos through HTTP, which is working fine. Only problem is that it stops for buffering from time to time. I now want to get notified whenever the video starts to buffer to display a progress indicator.
I learned that I need to implement public abstract void onBufferingUpdate (MediaPlayer mp, int percent) from android.media.MediaPlayer.OnBufferingUpdateListener, my question is just where do I have to implement it? I followed the advice from another SO question and implemented:
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(Constants.LOG, "onPrepared called");
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
//do something
Log.d(Constants.LOG, "Buffering update: " + percent + "%");
}
});
}
But it didn't work... Do I have to subclass VideoView and implement it there??
Works:
VideoView myVideoView = (VideoView)this.findViewById(R.id.video_view);
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
myVideoView.start();
if ( dialog != null )
dialog.dismiss();
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener()
{
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
Log.d("URL", "Buffering update: " + percent + "%");
}
});
}
});
Dialog dialog = new ProgressDialog(VideoActivity.this);
dialog.setMessage("Loading ...");
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
myVideoView.setVideoURI( Uri.parse( "http://..." ) );

Categories

Resources