我有一个视频视图,我在其中显示视频,我想显示默认的媒体控制器。出于某种原因,控制器似乎不想出现。
我尝试使用xml创建MediaController,将其设置为始终可见,将其附加到mMediaController.setMediaPlayer(mVideoView)
的媒体播放器,但似乎没有任何效果。
我正在使用Google的经典视频播放器代码,可在此处找到:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html
会发生什么?听众是否会输掉比赛?它不附加到实际的视频播放?我应该在我正在使用的代码中添加其他东西(见下文)吗?
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_content_video);
[...]
mVideoView = (VideoView) findViewById(R.id.surface);
mainVideoHolder = (LinearLayout) findViewById(R.id.main_video_holder);
holder = mVideoView.getHolder();
holder.addCallback(this);
mMediaController = new MediaController(this);
mMediaController.show();
}
private void playVideo() {
doCleanUp();
try {
mMediaPlayer = new MediaPlayer();
Log.d(tag, "surfaceCreated");
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());
URI uri = URI.create("file://" + (files[0].toString()));
File file = new File(uri);
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
mMediaPlayer.setDataSource(parcel.getFileDescriptor());
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(mMediaController);
} catch (Exception e) {
Log.e(tag, "error: " + e.getMessage(), e);
}
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d(tag, "surfaceCreated called");
playVideo();
}
任何想法都会受到高度赞赏吗?
活动中没有应用主题,视频正常播放而不会出现任何错误。只是媒体控制器没有显示出来!
感谢。
答案 0 :(得分:1)
在尽力而为之后,以下代码对我有用!
mVideoView = (VideoView) findViewById(R.id.video);
mMediaController = new MediaController(this) {
//for not hiding
@Override
public void hide() {}
//for 'back' key action
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Activity a = (Activity)getContext();
a.finish();
}
return true;
}
};
mMediaController.setAnchorView(mVideoView);
mMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(mMediaController);
mMediaController.requestFocus();
//only this showed the controller for me!!
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mMediaController.show(900000000);
}
});
//finish after playing
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
finish();
}
});
答案 1 :(得分:0)
您将控制器添加到布局中的哪个位置?还要确保容器足够大。给它一个背景,看看它是否在添加视频控制器之前在屏幕上显示良好。
答案 2 :(得分:0)
我不确定这个答案有点晚,但也许其他人有这个问题。 doku说同伴:
使用此类的方法是以编程方式实例化它。 MediaController将创建一组默认控件,并将它们放在浮动应用程序上方的窗口中。具体来说,控件将浮动在setAnchorView()指定的视图上方。如果闲置三秒钟,窗口将消失,当用户触摸锚视图时窗口将重新出现。
所以你可以这样做(在这种情况下,MediaPlayer是一个VideoView:
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVideoPath(uri);
MediaController controller = new MediaController(this);
controller.setAnchorView(videoView);
controller.setMediaPlayer(videoView);
videoView.setMediaController(controller);
videoView.start();
这对我有用。
答案 3 :(得分:0)
this work for me:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.UNKNOWN);
mVideoView = (VideoView)findViewById(R.id.videoview);
mc = new MediaController(this){
@Override
public void hide() {} //dont hide control
};
new MyDelay2().execute("");
}
private class MyDelay2 extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
Uri uri2 = Uri.parse("android.resource://com.blah/"+R.raw.cool);
mVideoView.setVideoURI(uri2);
return null;
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
mc.setAnchorView(mVideoView);
mc.setMediaPlayer(mVideoView);
mc.setEnabled(true);
mVideoView.setMediaController(mc);
mc.show();
}
}