Prateek
Prateek

Reputation: 4013

Mediaplayer track change issue

This is my code to play and update Track and on a button click I call the update() function but the track doesn't change properly.

First track plays just fine but when I try to switch to the next then it behaves improperly where am I wrong.

MediaPlayer mPlayer;


        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPlayer=new MediaPlayer();
        mPlayer.setOnPreparedListener(this);
        }

         public void prepareMediaPlayer(FileDescriptor fd)
         {
             try {
                 mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                 Log.d("player", "setAudiotStreamType");
                 mPlayer.setDataSource(fd); 
                 Log.d("player", "setDataSource");
                 mPlayer.prepareAsync();
                 Log.d("player", "prepareAsync");

             } 
             catch (IOException e) {}
             catch (IllegalArgumentException e) {} 
             catch (IllegalStateException e) {}
         }

        @Override
        public void onPrepared(MediaPlayer arg0) {
        }

        public void updateTrack()
        {
                        File f=(File) imageFilexxSm.values().toArray()[index];
                    FileInputStream inputStream = new FileInputStream(f);
                    prepareMediaPlayer(inputStream.getFD());
                    inputStream.close();

                    if(mPlayer.isPlaying())
                    {   
                    mPlayer.stop();
                    mPlayer.reset();
                    mPlayer.reset();
                    }

                    else
                    {
                    mPlayer.start();    
                    }   

        }

Log:

05-11 13:17:42.782: E/MediaPlayer(14969): start called in state 4
05-11 13:17:42.782: E/MediaPlayer(14969): error (-38, 0)
05-11 13:17:42.821: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.372MB for 4194320-byte allocation
05-11 13:17:43.040: I/System.out(14969): index value is 0/1024/0
05-11 13:17:43.087: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.457MB for 2261008-byte allocation
05-11 13:17:43.189: E/MediaPlayer(14969): attachNewPlayer called in state 8
05-11 13:17:43.220: I/Choreographer(14969): Skipped 35 frames!  The application may be doing too much work on its main thread.
05-11 13:17:43.275: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.452MB for 4194320-byte allocation
05-11 13:17:43.392: I/MediaPlayer(14969): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
05-11 13:17:43.392: E/MediaPlayer(14969): Error (-38,0)
05-11 13:17:57.642: I/System.out(14969): action down
05-11 13:17:57.642: I/System.out(14969): points 1.6014493?21
05-11 13:17:57.642: I/System.out(14969): show one page
05-11 13:17:57.642: I/System.out(14969): start curl right
05-11 13:17:57.650: I/System.out(14969): index value is 1/1024/1
05-11 13:17:57.681: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.401MB for 2261008-byte allocation
05-11 13:17:57.775: E/MediaPlayer(14969): attachNewPlayer called in state 128
05-11 13:17:57.821: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.401MB for 4194320-byte allocation
05-11 13:18:04.704: I/System.out(14969): action down
05-11 13:18:04.704: I/System.out(14969): points 1.692029?21
05-11 13:18:04.704: I/System.out(14969): show one page
05-11 13:18:04.704: I/System.out(14969): start curl right
05-11 13:18:04.704: I/System.out(14969): index value is 2/1024/2
05-11 13:18:04.736: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.315MB for 2261008-byte allocation
05-11 13:18:04.829: E/MediaPlayer(14969): attachNewPlayer called in state 128
05-11 13:18:04.876: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation
05-11 13:18:08.845: I/System.out(14969): action down
05-11 13:18:08.845: I/System.out(14969): points 1.5471015?21
05-11 13:18:08.845: I/System.out(14969): show one page
05-11 13:18:08.845: I/System.out(14969): start curl right
05-11 13:18:08.845: I/System.out(14969): index value is 3/1024/3
05-11 13:18:08.876: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation
05-11 13:18:08.970: E/MediaPlayer(14969): attachNewPlayer called in state 16
05-11 13:18:09.025: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation
05-11 13:18:12.892: I/System.out(14969): action down
05-11 13:18:12.892: I/System.out(14969): points 1.5688405?21
05-11 13:18:12.892: I/System.out(14969): show one page
05-11 13:18:12.892: I/System.out(14969): start curl right
05-11 13:18:12.892: I/System.out(14969): index value is 4/1024/4
05-11 13:18:12.923: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation
05-11 13:18:13.025: E/MediaPlayer(14969): start called in state 4
05-11 13:18:13.025: E/MediaPlayer(14969): error (-38, 0)
05-11 13:18:13.064: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation
05-11 13:18:13.126: E/MediaPlayer(14969): Error (-38,0)
05-11 13:18:13.126: I/MediaPlayer(14969): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
05-11 13:18:18.228: I/System.out(14969): action down
05-11 13:18:18.236: I/System.out(14969): points -1.5289855?21
05-11 13:18:18.236: I/System.out(14969): show one page
05-11 13:18:18.236: I/System.out(14969): index value is 3/1024/3
05-11 13:18:18.259: I/dalvikvm-heap(14969): Grow heap (frag case) to 30.316MB for 2261008-byte allocation
05-11 13:18:18.353: E/MediaPlayer(14969): attachNewPlayer called in state 8
05-11 13:18:18.400: I/dalvikvm-heap(14969): Grow heap (frag case) to 34.316MB for 4194320-byte allocation

Upvotes: 0

Views: 8832

Answers (1)

Michael
Michael

Reputation: 58507

Two things stick out:

  1. You're using prepareAsync, which is asynchronous. That is, you should wait for the onPrepared callback before you do anything that relies on the preparation to be complete (like calling start). That's why you get the "start called in state 4" error message (state 4 is MEDIA_PLAYER_PREPARING).

  2. If updateTrack is called during ongoing playback it doesn't seem like you stop the MediaPlayer before preparing it for a new song. You've got a call to stop after the call to prepareMediaPlayer but that won't do you much good. To be able to re-prepare the player you have to stop it first, and if you also want to change the data source you'll have to reset the player after stopping it.

Refer to the MediaPlayer documentation's state diagram for the set of valid state transitions.

Upvotes: 6

Related Questions