开发者

App forces down suddently

i have created a music app.the app has 16 music btns.the app is running with no problem but as i press the btns many times the app forces down..

     super.onCreate(icicle);
        setContentView(R.layout.main);  
        int[] ids = {R.id.btn,R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btn10,
                R.id.btn11, R.id.btn12, R.id.btn13, R.id.btn14, R.id.btn15, R.id.btn16 };



            for (int i : ids) {
                b =  (Button) findViewById(i);
                b.setOnClickListener(this);
            }}
      //outside of onCreate()
        @Override
        public void onClick(View v) {

            switch(v.getId()) {
                case R.id.btn:
                     if (mp != null && mp.isPlaying()) mp.stop();
                    mp = MediaPlayer.create(zoo.this, R.raw.gata);
                    mp.start();
                    break;

this is the code and i use case for every btn.When the app forces down, the logCat is finding a NullPoi开发者_如何学运维nterException in the mp.start(); of the button that forces the app down..please help!

EDIT in from comment below:

case R.id.btn: 
    if (mp != null && mp.isPlaying()) mp.stop(); 
    mp.reset(); 
    try { 
        mp.setDataSource("zoo.this,R.raw.gata"); 
    } catch (IllegalArgumentException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
    } catch (IllegalStateException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
    } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
    }
    mp.start(); 
    break;


I think the point is that a MediaPlayer is a pretty heavy weight resource and you should not create too many of them. Also, as soon as you are done with it, call it's release() method. Anon's point's are both valid: you should try to reuse your media play instead of creating a new one and you should become very familiar with the MediaPlayer documentation. For instance from the MediaPlayer documentation:

Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether.

The hypothesis is that you are allocating lots of MediaPlayer objects and/or not releasing them fast enough. However, without more code it's impossible to be certain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜