Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android VideoView Playback Controls Show "Play" Initially Instead of "Pause" Even Though File is Already Playing

Good afternoon/morning! Hoping someone could help me out with a small problem I'm having. I'm playing a remote .mp3 file using a VideoView and a custom MediaController.

My MediaController looks like this:

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

}

And my code to attach it to my VideoView looks like this:

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

The problem I'm having is that when the .mp3 file starts playing, the control bar at the bottom has the "Play" button showing (i.e. triangle) instead of the "Pause" button (two parallel bars) even though the audio is already playing. Anyone know how to fix this?

EDIT 1:

I'd also be interested in any other solutions for playing a remote .mp3. The only requirements I have are that the user can pause/play the audio and also see what the name of the audio file (title) is.

Thank you!

like image 359
Mr. Spock Avatar asked Mar 23 '13 02:03

Mr. Spock


1 Answers

Try This : It solved the Issue For Me.

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
        mMediaController.show();
    }
});
like image 91
iAviatorJose Avatar answered Nov 10 '22 11:11

iAviatorJose