I want media controls such as play/pause for streaming audio that I am playing in my app. I am using MediaPlayer to stream and play the audio.
Can someone provide a code snippet on how to use MediaController with MediaPlayer?
Thanks Chris
Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method. Similarly, you can pause the playing media file by using the pause() method. Stop the playback: You can stop playback by using the reset() method.
VideoView is a UI widget that is used to display video content to the users within android applications. We can add video in this video view from different resources such as a video stored on the user device, or a video from a server.
It's quite simple to add media controller in a media player. Make your activity implement MediaPlayerControl and add unimplemented methods. Code is as below:
I am using code from Api demos from here http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html
public class ActivityVedioPlay extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl { ..... private MediaPlayer mMediaPlayer; private MediaController mcontroller; private Handler handler = new Handler(); ..... @Override public boolean onTouchEvent(MotionEvent event) { /* * the MediaController will hide after 3 seconds - tap the screen to * make it appear again */ mcontroller.show(); return false; } ...... private void playVideo() { doCleanUp(); try { path = getIntent().getStringExtra("url"); if (path == "") { Toast.makeText(ActivityVedioPlay.this, "URL Not found", Toast.LENGTH_LONG).show(); } mMediaPlayer = new MediaPlayer(); mMediaPlayer.setDataSource(path); mMediaPlayer.setDisplay(holder); mMediaPlayer.prepare(); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setScreenOnWhilePlaying(true); mMediaPlayer.setOnVideoSizeChangedListener(this); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mcontroller = new MediaController(this); } catch (Exception e) { e.printStackTrace(); } } ...... public void onPrepared(MediaPlayer mediaplayer) { Log.d(TAG, "onPrepared called"); mIsVideoReadyToBePlayed = true; if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { startVideoPlayback(); } mcontroller.setMediaPlayer(this); mcontroller.setAnchorView(findViewById(R.id.mediaplayer_surfaceview_container)); handler.post(new Runnable() { public void run() { mcontroller.setEnabled(true); mcontroller.show(); } }); } //mediacontroller implemented methods public void start() { mMediaPlayer.start(); } public void pause() { mMediaPlayer.pause(); } public int getDuration() { return mMediaPlayer.getDuration(); } public int getCurrentPosition() { return mMediaPlayer.getCurrentPosition(); } public void seekTo(int i) { mMediaPlayer.seekTo(i); } public boolean isPlaying() { return mMediaPlayer.isPlaying(); } public int getBufferPercentage() { return 0; } public boolean canPause() { return true; } public boolean canSeekBackward() { return true; } public boolean canSeekForward() { return true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With