即使文件已经播放,Android VideoView播放控件最初显示“播放”而不是“暂停”

时间:2013-03-23 02:48:43

标签: android android-videoview mediacontroller

下午好/早上好!希望有人可以帮我解决我遇到的一个小问题。我正在使用VideoView和自定义MediaController播放远程.mp3文件。

我的MediaController看起来像这样:

public class MyMediaController extends MediaController {

public MyMediaController(Context context) {
    super(context);      
}

// Do nothing on the overridden hide method so the playback controls will never go away.
@Override
public void hide() {

}

// Override the dispatchKeyEvent function to capture the back KeyEvent and tell the activity to finish.
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) 
    {
        ((Activity) getContext()).finish();
    }

    return super.dispatchKeyEvent(event);
}

}

我将其附加到VideoView的代码如下所示:

    VideoView videoView = (VideoView) findViewById(R.id.VideoView);

    // Use our own media controller, which inherits from the standard one. Do this to keep 
    // playback controls from disappearing.
    mediaController = new MyMediaController(this);
    mediaController.setAnchorView(videoView);

    Uri video = Uri.parse(URL);

    videoView.setMediaController(mediaController);
    videoView.setVideoURI(video);

    // Set a handler that will show the playback controls as soon as audio starts.
    videoView.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer arg0) {

            mediaController.show();
        }

    });

    videoView.start();

我遇到的问题是当.mp3文件开始播放时,底部的控制栏有“播放”按钮显示(即三角形)而不是“暂停”按钮(两个平行条),即使音频已播放。任何人都知道如何解决这个问题?

编辑1:

我也会对播放遥控器.mp3的任何其他解决方案感兴趣。我唯一的要求是用户可以暂停/播放音频,还可以看到音频文件(标题)的名称。

谢谢!

2 个答案:

答案 0 :(得分:3)

试试这个:它解决了我的问题。

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
        mMediaController.show();
    }
});

答案 1 :(得分:0)

简单地:

@Override
        public void onPrepared(MediaPlayer arg0) {

         if (mediaController.isShowing==false) {mediaController.show();}
        }