0

这是我播放和更新轨道的代码,单击按钮我调用该update()函数,但轨道没有正确更改。

第一首曲目播放得很好,但是当我尝试切换到下一首曲目时,它的行为不正确,我错在哪里。

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

        }

日志:

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
4

1 回答 1

6

有两点突出:

  1. 您正在使用prepareAsync,这是异步的。也就是说,您应该等待onPrepared回调,然后再执行依赖于准备完成的任何操作(例如调用start)。这就是为什么您会收到 " start called in state 4" 错误消息(状态 4 是MEDIA_PLAYER_PREPARING)。

  2. 如果updateTrack在正在进行的播放过程中被调用,它似乎并没有MediaPlayer在为新歌曲准备之前停止。stop 在调用之后你有一个调用,prepareMediaPlayer但这对你没有多大好处。为了能够重新准备播放器,您必须先stop对其进行准备,如果您还想更改数据源,则必须reset在停止播放器后对其进行准备。

有关一组有效状态转换,请参阅MediaPlayer 文档的状态图

于 2013-05-11T09:02:30.800 回答