我有一个使用VideoView播放视频的Android应用程序,我也在使用媒体控制器。
我的问题是我无法阻止媒体控制器在视频播放时消失,我还希望在视频启动之前使媒体控制器可见,以便用户可以使用媒体控制器初始启动视频。我尝试使用MediaController.show(0),但这并没有解决我的问题。这是我的代码:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
MediaController mc = new MediaController(this);
mVideoView.setMediaController(mc);
mPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
}
private void playVideo() {
try {
final String path = "http://toop.voxelperfect.net/video6.mp4";
System.out.println("path "+path);
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(SampleActivity.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
System.out.println("else ");
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
System.out.println("mVideoView.start() ");
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
current = path;
MediaController mc = new MediaController(this);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(Uri.parse(path));
mVideoView.start();
mVideoView.requestFocus();
mc.show(0);
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage());
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
答案 0 :(得分:0)
我通过创建一个实现MediaController并覆盖hide方法的嵌套类来解决我的问题。