2

Hi I am developing android application in which I have one simple activity which contains button to start new activity. new activity contain surface view to show video. I know that prepare media player work on main thread that's why I use prepareAsync. SO my problem is like this. I click on activity stat button which start new activity and start loading video. If I click back before video start i.e. media player start() it come back to previous activity. But UI of that activity is not active for some time. That mean I am not able to click on start button for some time. After some time it become active again. Is there any one facing same problem. My code looks like :

@Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        Log.i("FFFFFFFFFFFFFFFFFFF  ", "FFFFFFFFFFFFFFFFFFF ");

         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
         mediaPlayer.setDisplay(surfaceHolder);

         try {
             mediaPlayer.setDataSource(getApplicationContext(), targetUri);
             mediaPlayer.prepareAsync();
             //mediaPlayer.prepare();    
         } catch (IllegalArgumentException e) {
             e.printStackTrace();
         } catch (IllegalStateException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }

         mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub

                mediaPlayer.start();

            }
            });
    } 

How to avoid this situation. Am I doing some thing wrong. Need help. Thank you.

nilkash
  • 7,408
  • 32
  • 99
  • 176

1 Answers1

0

Well this is a common issue faced while video is being load by mediaplayer or videoview. This happens because mediaplayer is initializing your video and preparing it for playing. This lag can cause a delay of upto 1-2 seconds depending on your video size and hardware.

Now to answer your question, you can handle this by either initializing mediaplayer in a separate thread or you can handle it using async task. If you use async and as you have already used it, you can just show a progress visualization for user to wait. This is the approach used even by most mediaplayers in android.

Hope this helps. Cheers.:)

BlueSword
  • 1,290
  • 12
  • 25