开发者

MediaController with MediaPlayer

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 snippe开发者_StackOverflowt on how to use MediaController with MediaPlayer?

Thanks Chris


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;
    }


As easy as this code extracted from here. They use VideoView instead of a MediaPlayer, which saves you a few lines of code:

Layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="2px" android:paddingRight="2px"
    android:paddingTop="2px" android:paddingBottom="2px"
    android:layout_width="fill_parent" android:orientation="vertical">

    <VideoView android:layout_height="fill_parent"
        android:layout_width="fill_parent" android:id="@+id/VideoView"></VideoView>

</LinearLayout>

And code:

public class VideoPlayerController extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.video);

        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
        Uri video = Uri.parse("mp4 video link");
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();

    }
}

That's all. Just replace mp4 video link with a streaming video url.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜