gpt4 book ai didi

android - 带有 MediaPlayer 的 MediaController

转载 作者:IT老高 更新时间:2023-10-28 23:02:32 35 4
gpt4 key购买 nike

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

有人可以提供有关如何将 MediaController 与 MediaPlayer 一起使用的代码 fragment 吗?

谢谢克里斯

最佳答案

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

我正在使用来自此处 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html 的 Api 演示代码

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

关于android - 带有 MediaPlayer 的 MediaController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2961749/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com