5

I have the following working code:

@Override
        protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.player);

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

        // video finish listener
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                        mp.start();
                }
        });
    }

    public void playVideo() {
                MediaController mc = new MediaController(this);
                videoView.setMediaController(mc);
                videoView.setVideoURI(Uri.parse("http://sayedhashimi.com/downloads/android/movie.mp4"));
                videoView.requestFocus(); 
        }

I just want to change the MediaPlayer data source when the video finishes (setOnCompletionListener).

Rui Gonçalves
  • 2,423
  • 6
  • 30
  • 42

2 Answers2

9

I'm working on this same issue. Here is what I've come up with:

   public void onCompletion(MediaPlayer mp) {
       mp.reset();
       mp.setDataSource(this, newUri);
       mp.start();
   }
Alan Moore
  • 6,525
  • 6
  • 55
  • 68
  • 1
    this will result in: 'start called in state 2' – Stefan Alexandru Apr 23 '13 at 14:20
  • I actually have never seen that, are you getting that message in your log? – Alan Moore Apr 23 '13 at 16:42
  • 1
    yes. calling .prepare() before calling .start() helps. But sometimes I still have random errors like (1, something) or (-38, something). the strange thing is that some times it works and some times it doesn't. More about the strange errors here: http://stackoverflow.com/questions/15839082/android-4-2-with-4-mediaplayers-cant-play-this-video – Stefan Alexandru Apr 24 '13 at 12:22
0

Here is my solution:

mp.release()
mp = createNew(context,newUri)
mp.start()

Anything else I tried gave me errors as mentioned here. Creating new media player every time won't be that harmful for performance as you will most likely have only one at a time anyway.

hope it helps ; )

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20