完成播放后,如何释放媒体播放器? [英] How do I release my media player after it is done playing?

查看:138
本文介绍了完成播放后,如何释放媒体播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个音板,在单击大约30种不同的声音后,它停止工作;我相信android内存不足.下面是我的代码.如何实现.release()以便在播放完声音后将其释放?我不太在乎是否同时播放两件事.剪辑太短了,这是可能的.我只想设置我的代码.

I am creating a sound board and after clicking about 30 different sounds it stops working; I believe android is running out of memory. Below is my code. How can I implement .release() so that when the sound is done playing it is released? I don't really care if two things play at the same time; the clips are t0o short for this to be possible. I would just like to get my code set.

public class soundPageOne extends Activity {
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.soundsone);

        final MediaPlayer pg1 = MediaPlayer.create(this, R.raw.peter1);
        Button playSound1 = (Button) this.findViewById(R.id.peter1Button);

        playSound1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                pg1.start();

        }
    });

我已经进行了很多搜索,但是由于我缺乏Java/Android知识,所以我什么都无法工作.预先感谢,让我知道是否有人需要任何代码.

I have done a lot of searching around but due to my lack of java/android knowledge I have not been able to get anything to work. Thanks in advance, let me know if anyone needs anymore code.

推荐答案

我发表了评论,但我将发布答案以表明我的意思...

I left a comment, but I'll post an answer to show what I mean anyway...

这个想法是您可以使用一定数量的MediaPlayer实例.这样,您就永远不会超过最大实例数.数组应为您希望能够听到的并发声音的长度.如果声音是本地文件,则准备声音所需的时间几乎可以忽略不计,因此在单击处理程序中调用create不会导致糟糕的性能.我想您的每个按钮都与一个特定的资源相关联,所以我设置了一个辅助方法,以相同的方式为每个按钮创建和播放声音.

The idea is that you have a set number of MediaPlayer instances that you can use. That way you never exceed the maximum number of instances. The array should be the length of the number of concurrent sounds you expect to be able to hear. If the sounds are local files, the length of time it takes to prepare the sounds should be almost negligible, so calling create inside the click handler should not result in terrible performance. Each of your buttons is associated with a particular resource, I suppose, so I set up a helper method to create and play the sounds for each button in the same way.

public class soundPageOne extends Activity {

    private MediaPlayer[] mPlayers = new MediaPlayer[2];
    private int mNextPlayer = 0;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.soundsone);
        Button playSound1 = (Button)this.findViewById(R.id.peter1Button);
        playSound1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSound(R.raw.peter1);
            }
        });
    }

    public void onDestroy() {
        super.onDestroy(); // <---------------------- This needed to be there
        for (int i = 0; i < mPlayers.length; ++i)
            if (mPlayers[i] != null)
                try {
                    mPlayers[i].release();
                    mPlayers[i] = null;
                }
                catch (Exception ex) {
                    // handle...
                }
    }

    private void startSound(int id) {
        try {
            if (mPlayers[mNextPlayer] != null) {
                mPlayers[mNextPlayer].release();
                mPlayers[mNextPlayer] = null;
            }
            mPlayers[mNextPlayer] = MediaPlayer.create(this, id);
            mPlayers[mNextPlayer].start();
        }
        catch (Exception ex) {
            // handle
        }
        finally {
            ++mNextPlayer;
            mNextPlayer %= mPlayers.length;
        }
    }

}

这篇关于完成播放后,如何释放媒体播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆