1

我有一个包含一些项目的 ListView,每个项目都会播放某种声音。这是代码:

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            try {
                player.setDataSource(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
                player.prepare();
                player.start();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

如果我单击一个项目并开始播放,然后在我之前单击的项目完成播放之前单击另一个项目,则 MediaPlayer 将同时播放两种声音。如果我在停止播放之前单击了该项目,如何让我的 MediaPlayer 停止()播放上一个声音并开始播放新声音?

我在尝试之前尝试添加 player.Stop() ,这样每次我单击一个项目时,MediaPlayer 在设置新源并重新启动之前停止,但这只会停止以前的声音并且不会播放新声音。

我还尝试添加 IF 语句,如下所示:

if (!player.isPlaying()){
//normally start the player
}
else if (player.isPlaying()){
//stop it before starting it again
}

我会复制我的实际代码,但我已经删除了它,因为它不起作用。它也只是停止了之前的声音,并没有播放新的声音。

4

4 回答 4

1

停止歌曲后重置播放器。

  listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
        try {
             if (player!=null && player.isPlaying())
              {                     
               player.stop();
               player.reset();
              }

            player.setDataSource(externalStoragePath +     "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
            player.prepare();
            player.start();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});
于 2013-08-19T06:56:21.063 回答
0

要重用MediaPlayer,必须reset()在设置 new 之前调用dataSource。还有一件事,你不应该prepare()在 UI 线程中调用,它可能会导致ANR错误。你宁愿调用prepareAsync()函数。

于 2013-08-19T11:13:57.543 回答
0

您必须setDataSource()在空闲状态下调用。所以你必须在打电话reset()之前打电话setDateSource()

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
        try {
            player.reset();
            player.setDataSource(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
            player.prepare();
            player.start();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});
于 2013-08-19T06:51:32.947 回答
0
 listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
        try {
            stopPlaying();
            initializeMediaPlayer(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
            startPlaying();

        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});



private void stopPlaying() {
    if (player!=null && player.isPlaying()) {
        player.stop();
        player.release();
    }
}

private void initializeMediaPlayer(String path) {
    player = new MediaPlayer();
    try {
        player.setDataSource(path);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void startPlaying() {
    try {
        player.prepareAsync();
        player.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            if(player!=null)
            player.start();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
于 2013-08-19T07:01:44.427 回答