在使用VideoView时,我无法找到将MediaController保留在屏幕上的方法。我想在我的应用程序中播放res / raw中的音频文件。该文件正在播放,但我想将控制器保留在屏幕上,以便用户查看文件的长度以及在结束之前剩余的时间等。我在论坛上发现很多时间我们应该使用该方法.show (时间)为了做到这一点,但我无法弄清楚如何使它发挥作用。
以下是我使用的代码:
package com.sample.VideoViewExample;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoViewExample extends Activity implements SurfaceHolder.Callback{
private VideoView mVideoView;
private MediaController mMedia;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
//mVideoView.getHolder().addCallback(this);
//mMedia.show(50000);
//mVideoView.setMediaController(mMedia);
MediaController mMedia = new MediaController(this);
mMedia.setMediaPlayer(mVideoView);
mMedia.setAnchorView(mVideoView);
mVideoView.setMediaController(mMedia);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.osa_patient));
mVideoView.requestFocus();
mVideoView.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mMedia.show(500000);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
当我添加行mVideoView.getHolder()。addCallback(this);应用程序错误,我不知道为什么。
任何帮助或替代我想做的事情都非常受欢迎:)
谢谢, JB
答案 0 :(得分:12)
MediaController mediaController = new MyMediaController(this, true);
public class MyMediaController extends MediaController {
public MyMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
@Override
public void hide() {
mediaController.show(0);
}
}
答案 1 :(得分:4)
好的,我找到了!花了几个小时后! 所以我不知道如何在不同的论坛上做到这一点。这是我发现的一个技巧:
FIRST CLASS(例子......)
package com.sample.VideoViewExample;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;
public class VideoViewExample extends Activity implements SurfaceHolder.Callback{
private VideoView mVideoView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setKeepScreenOn(true);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
SurfaceHolder holder = mVideoView.getHolder();
holder.addCallback(this);
MediaController_2 mMedia = new MediaController_2(this);
mMedia.setMediaPlayer(mVideoView);
mMedia.setAnchorView(mVideoView);
mVideoView.setMediaController(mMedia);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.osa_patient));
mVideoView.requestFocus();
mVideoView.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
第二类(与罢工)
package com.sample.VideoViewExample;
import android.content.Context;
import android.widget.MediaController;
public class MediaController_2 extends MediaController{
public MediaController_2(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void hide() {
}
}
诀窍就是创建一个扩展Mediacontroller的新MediaController,而hide()什么都不做!
如果您想查看MediaController源代码以便更好地理解,可以在这里查看:// MediaController的源代码http://hi-android.info/src/android/widget/MediaController.java.html
希望这对某人有帮助, JB
答案 2 :(得分:0)
从目前为止给出的答案来看,现在问题是后面的功能无法正常工作,所以要按照此代码进行修复。
public class CustomMediaController extends MediaController {
public CustomMediaController(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void hide() {
}
//需要使后台功能正常工作
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
super.hide();
Activity activity = (Activity)getContext();
activity.finish();
}
return super.dispatchKeyEvent(event);
}}
答案 3 :(得分:0)
您所要做的就是用您自己的类覆盖MediaController,然后覆盖onTouchEvent。
public class SmartLinkVideoController extends MediaController implements MediaPlayerControl {
//declare needed variables
private SmartLinkVideoPlayer mVideoPlayer;
public SmartLinkVideoController(Context context) {
super(context);
}
public SmartLinkVideoController(Context context, SmartLinkVideoPlayer videoPlayer) {
this(context);
this.setMediaPlayer(this);
this.mVideoPlayer = videoPlayer;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//super implementation sets default time to hide of ~ 3 seconds
show(0);
return true;
}
@Override
public void start() {
mVideoPlayer.start();
}
@Override
public void pause() {
mVideoPlayer.pause();
}
@Override
public int getDuration() {
return mVideoPlayer.getDuration();
}
@Override
public int getCurrentPosition() {
return mVideoPlayer.getCurrentPosition();
}
@Override
public void seekTo(int pos) {
mVideoPlayer.seekTo(pos);
}
@Override
public boolean isPlaying() {
return mVideoPlayer.isPlaying();
}
@Override
public int getBufferPercentage() {
return mVideoPlayer.getPercentBuffered();
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
@Override
public int getAudioSessionId() {
return 0;
}
public boolean hasVideoPlayer() {
return (mVideoPlayer != null);
}
public void prepareVideo() {
if(mVideoPlayer.getVideoType() == SmartLinkVideoPlayer.VIDEO_TYPE_INTERNAL
|| mVideoPlayer.getVideoType() == SmartLinkVideoPlayer.VIDEO_TYPE_EXPANSION)
mVideoPlayer.prepare();
else
mVideoPlayer.prepareAsync();
}
public void prepareSurface(SurfaceHolder holder) {
mVideoPlayer.setDisplay(holder);
}
public void rewindVideo() {
mVideoPlayer.seekTo(0);
}
public void setVideoPlayer(SmartLinkVideoPlayer videoPlayer) {
mVideoPlayer = videoPlayer;
}
public void stopVideoPlayer() {
mVideoPlayer.stop();
}
public void resetVideoPlayer() {
mVideoPlayer.reset();
}
public void releaseVideoPlayer() {
mVideoPlayer.release();
mVideoPlayer = null;
}
}