带有 MediaPlayer 的 MediaController

发布于 2024-09-04 04:09:59 字数 145 浏览 4 评论 0 原文

我想要媒体控件,例如我在应用程序中播放的流媒体音频的播放/暂停。我正在使用 MediaPlayer 来流式传输和播放音频。

有人可以提供有关如何将 MediaController 与 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 snippet on how to use MediaController with MediaPlayer?

Thanks
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-09-11 04:10:00

在媒体播放器中添加媒体控制器非常简单。让您的活动实现 MediaPlayerControl 并添加未实现的方法。代码如下:

我正在使用来自此处的 Api 演示的代码 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;
    }

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;
    }
雨轻弹 2024-09-11 04:10:00

就像从此处提取的代码一样简单。他们使用 VideoView 而不是 MediaPlayer,这可以节省您几行代码:

布局:

<?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>

和代码:

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();

    }
}

就这样。只需将 mp4 视频链接 替换为流媒体视频网址即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文