2

我正在尝试使用MediaPlayer. 我试图播放文件的目录有大约 20 个文件,我编写的代码只播放三个文件然后停止播放。请指出我的错误并帮助我解决它。

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mediaPlayer.stop();
                        System.out.println("coming here to playNextMEdia");
                        playNextMedia();
                    }    
                });
            }

private void playNextMedia() {
        System.out.println("playNextMedia "+files.size()+"and"+counter );
        if(counter<files.size()){
            mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(files.get(counter)));

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    mediaPlayer.stop();
                    System.out.println("playNextFile() cehck 1");

                    //playNextFile();
                    //System.out.println("playNextFile() cehck 2");
counter++;
                    playNextMedia();
                }

            });

日志猫

10-09 21:49:10.129: I/System.out(2644): coming here to playNextMEdia
10-09 21:49:10.129: I/System.out(2644): playNextMedia 19and1
10-09 21:49:36.249: I/System.out(2644): playNextFile() cehck 1
10-09 21:49:36.249: I/System.out(2644): playNextMedia 19and2

可能,我错过了一个小的条件检查或其他东西。

编辑 1

if (fd != null) {
                try { 
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(fd);
                    //mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(files.get(counter)));  
                    mediaPlayer.setScreenOnWhilePlaying(true);


                } catch (IllegalStateException e) {  
                    e.printStackTrace();  

                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
                mediaPlayer.start();  
4

1 回答 1

0

尝试这样的事情:

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        counter++;
        if (counter < files.size()) {
            mediaPlayer.setDataSource(files.get(counter));
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
    }    
});
于 2013-10-09T17:45:49.833 回答