简体   繁体   English

在再次播放之前停止MediaPlayer无法正常工作

[英]Stopping MediaPlayer before playing again isn't working

I have a ListView with some items and each item plays a certain sound. 我有一个包含某些项目的ListView,每个项目都播放某种声音。 This is the code: 这是代码:

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

If I click an item and it starts playing and then click another item before the one I clicked before has finished playing, the MediaPlayer will just play both sounds at the same time. 如果我单击一个项目并开始播放,然后在我单击之前的另一个项目结束播放之前单击另一个项目,则MediaPlayer会同时播放两种声音。 How do I get my MediaPlayer to stop() playing the previous sound and start playing the new sound if I have clicked the item before it stopped playing? 如果我在停止播放之前单击该项目,如何使MediaPlayer停止播放先前的声音并开始播放新声音?

I tried adding player.Stop() before try, so that everytime I click an item, the MediaPlayer stops before setting a new source and starting again, but that just stops the previous sound and won't play the new sound. 我尝试在尝试添加前添加player.Stop(),以便每次单击一个项目时,MediaPlayer都会停止,然后再设置新的源并重新开始,但这只会停止先前的声音,而不会播放新的声音。

I also tried adding IF statement, like this: 我也尝试添加IF语句,如下所示:

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

I'd copy my actual code but I already deleted it since it didn't work. 我复制了我的实际代码,但由于无法正常工作,因此已将其删除。 It also just stopped the previous sound and didn't play the new one. 它也只是停止了先前的声音而没有播放新的声音。

Reset your player after stopping song. 停止播放歌曲后重设播放器。

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

You have to call setDataSource() in the idle state. 您必须在空闲状态下调用setDataSource() So you have to call reset() before calling setDateSource() 所以,你必须调用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();
        }
    }
});
 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();
    }
}

to reuse MediaPlayer , you must call reset() before setting new dataSource . 要重用MediaPlayer ,必须在设置新的dataSource之前调用reset() One more thing, you should not call prepare() in UI thread, it may cause ANR error. 还有一件事,您不应该在UI线程中调用prepare() ,这可能会导致ANR错误。 You would rather call prepareAsync() function. 您宁愿调用prepareAsync()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM