Android音板错误

时间:2011-10-10 02:00:01

标签: android android-mediaplayer

我的任务已经完成,并且在最后几个声音中运行良好。由于一些奇怪的原因,它保持强制关闭,我看了看,但不明白为什么。我把声音带走了,并做了其他几个步骤,我不明白是否有尺寸限制或什么?

这是它完成的第二个程序,任何想法?

MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9, mp10, mp11, mp12,
    mp13, mp14, mp15, mp16, mp17, mp18, mp19, mp20, mp21, mp22, mp23, mp24, mp25, 
    mp26, mp27, mp28;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  //set up the button sound
    mp1 = MediaPlayer.create(this, R.raw.backtoyou);

    //button 1 coding 
    ImageButton Button1 = (ImageButton) findViewById (R.id.button01);
    Button1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp1.start();

        }
    });
    //longclick creates ringtone notification
    //Button1.setLongClickable(true);


    //set up the button sound
    mp2 = MediaPlayer.create(this, R.raw.blow);

    //button 1 coding 
    ImageButton Button2 = (ImageButton) findViewById (R.id.button02);
    Button2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp2.start();

        }
    });

     //set up the button sound
    mp3 = MediaPlayer.create(this, R.raw.boomstick);

    //button 1 coding 
    ImageButton Button3 = (ImageButton) findViewById (R.id.button03);
    Button3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp3.start();

        }
    });

    //set up the button sound
    mp4 = MediaPlayer.create(this, R.raw.byebye);

    //button 1 coding 
    ImageButton Button4 = (ImageButton) findViewById (R.id.button04);
    Button4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp4.start();

        }
    });

....等等等等。那么文件的错误在哪里?为什么它会继续强制关闭?

2 个答案:

答案 0 :(得分:1)

我怀疑有28个MediaPlayer个对象占用了大量内存。考虑使用每次按下按钮时使用新声音重新初始化的MediaPlayer个对象。看看这篇文章:http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/

答案 1 :(得分:0)

是的,你肯定有太多的MediaPlayer对象和冗余代码。我要问的问题是你是否需要声音重叠。对于一个音板,我不会假设(事实上,如果你这样做,我认为这将是一个恼人的UI怪癖 - 我见过很多次)。

尝试这种效果:

MediaPlayer player = new MediaPlayer();
Resources res = getResources();

//just keep them in the same order, e.g. button01 is tied to backtoyou
int[] buttonIds = { R.id.button01, R.id.button02, R.id.button03 /*etc...*/ };
int[] soundIds = { R.raw.backtoyou, R.raw.blow, R.raw.boomstick /*etc...*/ };

View.OnClickListener listener = new View.OnClickListener() {
    public void onClick(View v) {
        //find the index that matches the button's ID, and then reset
        //the MediaPlayer instance, set the data source to the corresponding
        //sound effect, prepare it, and start it playing.
        for(int i = 0; i < buttonIds.length; i++) {
            if(v.getId() == buttonIds[i]) {
                AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
                player.reset();
                player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                player.prepare();
                player.start();
                break;
            }
        }
    }
};

//set the same listener for every button ID, no need
//to keep a reference to every button
for(int i = 0; i < buttonIds.length; i++) {
    Button soundButton = (Button)findViewById(buttonIds[i]);
    soundButton.setOnClickListener(listener);
}