开发者

Keeping MediaController on the screen in a VideoView

I have trouble finding a way to keep the MediaController on the screen when using it with a VideoView. I want to play an Aud开发者_StackOverflow社区io file which is located in res/raw in my app. The file is playing but I would like to keep the controller on the screen for the user to see the length of the file and how much time is left before the end etc. I found many time on forums that we should use the method .show(time) in order to do that, but I cannot figure out how to make it work.

Here is the code I am using:

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

}
}

When I add the line mVideoView.getHolder().addCallback(this); the app bug, I am not sure why.

Any help or alternative to what I would like to do is more than welcome :)

Thanks, JB


this worked for me. Just extend the media controller class. And override the hide method.

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

}


Ok I have found! After spending hours on it!! So there is to my knowledge no clue on how to do it on the different forums. Here is a trick I found:

FIRST CLASS (example...)

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

}
}

SECOND CLASS (WITH THE TRICK)

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() {
    }

}

the trick is just to create a new MediaController that extends Mediacontroller and which function hide() does nothing!

If you want to have a look at the MediaController source code for a better understanding, you can look here: // source code for MediaController http://hi-android.info/src/android/widget/MediaController.java.html

Hope this will be of help to sb, JB


From the answers given so far , now the issue is that back functionality will not work so to fix it follow this code.

public class CustomMediaController extends MediaController {


public CustomMediaController(Context context) {
    super(context);

    // TODO Auto-generated constructor stub
}


public void hide() {

}

// Needed to make the back functionality working

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

        super.hide();
        Activity activity = (Activity)getContext();
        activity.finish();

    }
   return super.dispatchKeyEvent(event);
}}


All you have to do is override the MediaController with your own class and then override the 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;
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜