我希望在我的应用程序中播放流音频时,能够使用媒体控件,例如播放/暂停。我正在使用MediaPlayer来进行音频的流和播放。
有人可以提供一个代码片段,说明如何使用MediaController与MediaPlayer吗?
谢谢
Chris
我希望在我的应用程序中播放流音频时,能够使用媒体控件,例如播放/暂停。我正在使用MediaPlayer来进行音频的流和播放。
有人可以提供一个代码片段,说明如何使用MediaController与MediaPlayer吗?
谢谢
Chris
在媒体播放器中添加媒体控制器相当简单。让您的Activity实现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;
}
像这个代码一样容易,这个代码摘自这里。他们使用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视频链接
即可。