0

我的任务完成了,直到最后几个声音都运行良好。出于某种奇怪的原因,它一直强制关闭,我看了又看,不明白为什么。我带走了声音并做了其他几个步骤,但我不明白是否有大小限制或其他什么?

这是它完成的第二个程序,有什么想法吗?

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

        }
    });

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

4

2 回答 2

1

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

于 2011-10-10T02:07:50.930 回答
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);
}
于 2011-10-10T03:43:19.487 回答