7

将 MediaController 与 VideoView 一起使用时,我很难找到将它保持在屏幕上的方法。我想在我的应用程序中播放位于 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

4

4 回答 4

13

这对我有用。只需扩展媒体控制器类。并覆盖隐藏方法。

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

}
于 2012-01-02T11:11:05.310 回答
4

好的,我找到了!在花了几个小时之后!!因此,据我所知,不知道如何在不同的论坛上进行操作。这是我发现的一个技巧:

头等舱(例如...)

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有所帮助

于 2011-07-14T00:29:26.680 回答
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;
}
}
于 2015-12-05T01:10:23.080 回答
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);
}}
于 2015-08-12T07:01:49.020 回答