3

I'm trying to show a video stream that starts in one activity of my application and follows the user to other activities in the application without having to re-prepare it. A bit of a pause on activity change is OK, but it shouldn't restart. Everything's working fine in one activity, but when switching activities the MediaPlayer doesn't seem to want to connect to the new SurfaceHolder. The audio works beautifully though, with barely a hitch.

I've been trying to do this via a subclass of SurfaceView initialized with the application context. (Re-siting application-context views across activities seems to work fine when I instead use simple views such as Button, so I think that's not the problem.) In the constructor I create the MediaPlayer object, set the listeners and datasource (an MP4 stream via HTTP--and yeah, I verified that I can play it with a normal usage of MediaPlayer), and call prepareAsync. In my SurfaceHolder.Callback's surfaceCreated method I call setDisplay and (if the player has finished preparing) start. In the surfaceDestroyed method I call pause. And in the prepare callback if the surface has already been created I call start.

I'm at a loss of what to do at this point. Any ideas?

AeroX
  • 3,387
  • 2
  • 25
  • 39
Matt Tsōnto
  • 1,518
  • 1
  • 15
  • 30
  • Note: I've also tried with a local video file to reduce the number of things that might be going wrong. Same results. – Matt Tsōnto Jul 17 '11 at 20:14
  • If someone has an example of a MediaPlayer use that can continue playing video through an orientation change, that would probably include what I need to make this work. Unfortunately I haven't been able to find any such online. – Matt Tsōnto Jul 21 '11 at 00:34
  • could you share your some code for help I am also in same sort of trouble but unable to get the view at other activities..Here is my Running code Link: http://stackoverflow.com/questions/7739386/android-how-to-obtain-media-player-progress-bar-and-also-how-to-show-it-on-each-a – Arpit Garg Oct 13 '11 at 13:08

1 Answers1

0

The easier way is to store the position in the method onpause():

public void onPause(){

     super.onPause();
     int pos = player.getCurrentPosition();
}

and then in the new activity call:

player.seekTo(pos);

player.start();

But that isn´t very efficient. It would be nice to store the status of the video when leaving the activity and restoring when return to it, but I can´t find anything like this online.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Miguel
  • 26
  • 2
  • Pausing and resuming works fine for me, since I maintain a single instance across the activities. The problem is with reattaching it to a surface. – Matt Tsōnto Jul 30 '11 at 23:22