我的音乐应用程序在播放了7首歌后死亡

时间:2011-01-12 15:48:22

标签: android

我创建了一个16 btns的应用程序,每个btn播放声音mp3.my应用程序启动没有问题。我按下并听一个MP3,两个MP3 ...直到7 MP3 ...我的应用程序强制下来。我可以播放我喜欢的任何MP3但我只能播放7个mp3我先点击,我的错误在哪里?

Button button = (Button) findViewById(R.id.btn);  
button.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View v){
        MediaPlayer mp = MediaPlayer.create(ClickAndPlayMusic.this, R.raw.gata);  
        mp.start();
        Toast.makeText(ClickAndPlayMusic.this, "Cat", Toast.LENGTH_SHORT).show();

    }      

});      

        Button button2 = (Button) findViewById(R.id.btn2);  
        button2.setOnClickListener(new View.OnClickListener() {  
           public void onClick(View v){

               MediaPlayer mp = MediaPlayer.create(ClickAndPlayMusic.this, R.raw.lion);  
            mp.start();                    

               Toast.makeText(ClickAndPlayMusic.this, "Lion", Toast.LENGTH_SHORT).show();
           }

        });      

这就是我创造每一首音乐的方式...谢谢!(希望我的回答是可以理解的)

ps:当它强行关闭时,这是我的logcat视图:

01-12 17:59:39.445: WARN/dalvikvm(16090): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090): FATAL EXCEPTION: main
01-12 17:59:39.449: ERROR/AndroidRuntime(16090): java.lang.NullPointerException
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at com.example.ClickAndPlayMusic.ClickAndPlayMusic$1.onClick(ClickAndPlayMusic.java:33)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at android.view.View.performClick(View.java:2461)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at android.view.View$PerformClick.run(View.java:8888)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at android.os.Handler.handleCallback(Handler.java:587)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at android.os.Looper.loop(Looper.java:123)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at java.lang.reflect.Method.invoke(Method.java:521)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-12 17:59:39.449: ERROR/AndroidRuntime(16090):     at dalvik.system.NativeStart.main(Native Method)
01-12 17:59:39.457: WARN/ActivityManager(2796):   Force finishing activity com.example.ClickAndPlayMusic/.ClickAndPlayMusic
01-12 17:59:39.461: INFO/(2796): dumpmesg > "/data/log/dumpstate_app_error.log"

33行是mp.start();在第一个btn的onClick方法中!

第二次logcatview:

01-12 19:37:39.000: ERROR/PlayerDriver(2584): Command PLAYER_SET_DATA_SOURCE completed with an error or info -4
01-12 19:37:39.000: ERROR/MediaPlayer(20357): error (-4, -4)
01-12 19:37:39.000: ERROR/TAGNAME(20357): Prepare failed.: status=0xFFFFFFFC
01-12 19:37:39.000: WARN/System.err(20357): java.io.IOException: Prepare failed.:      status=0xFFFFFFFC
01-12 19:37:39.000: WARN/System.err(20357):     at    android.media.MediaPlayer.prepare(Native Method)
01-12 19:37:39.000: WARN/System.err(20357):     at com.example.ClickAndPlayMusic.ClickAndPlayMusic.playMedia(ClickAndPlayMusic.java:242)
01-12 19:37:39.000: WARN/System.err(20357):     at com.example.ClickAndPlayMusic.ClickAndPlayMusic.access$0(ClickAndPlayMusic.java:237)
01-12 19:37:39.000: WARN/System.err(20357):     at com.example.ClickAndPlayMusic.ClickAndPlayMusic$13.onClick(ClickAndPlayMusic.java:191)
01-12 19:37:39.000: WARN/System.err(20357):     at android.view.View.performClick(View.java:2461)
01-12 19:37:39.000: WARN/System.err(20357):     at   android.view.View$PerformClick.run(View.java:8888)
01-12 19:37:39.000: WARN/System.err(20357):     at android.os.Handler.handleCallback(Handler.java:587)
01-12 19:37:39.000: WARN/System.err(20357):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 19:37:39.000: WARN/System.err(20357):     at android.os.Looper.loop(Looper.java:123)
01-12 19:37:39.000: WARN/System.err(20357):     at   android.app.ActivityThread.main(ActivityThread.java:4627)
01-12 19:37:39.000: WARN/System.err(20357):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 19:37:39.000: WARN/System.err(20357):     at java.lang.reflect.Method.invoke(Method.java:521)
01-12 19:37:39.000: WARN/System.err(20357):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-12 19:37:39.000: WARN/System.err(20357):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-12 19:37:39.000: WARN/System.err(20357):     at dalvik.system.NativeStart.main(Native Method)
01-12 19:37:39.008: INFO/MediaPlayer(20357): Info (1,26)
01-12 19:37:39.008: ERROR/MediaPlayer(20357): Error (-4,-4)

2 个答案:

答案 0 :(得分:3)

MediaPlayer#create(android.content.Context, int)州的documentation

  

完成MediaPlayer后,您应该拨打release()以释放资源。如果未发布,则会有太多MediaPlayer个实例导致异常。

这是否是您遇到问题的原因将取决于您遇到的错误类型。

修改

这是你需要做的善良。从未使用MediaPlayer本身,这可能需要您做一些工作才能使其完全符合您的需求。

private MediaPlayer mp = new MediaPlayer();
...
Button button = (Button) findViewById(R.id.btn);  
button.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View v){
        playMedia("pathToCatSoundFile");
        Toast.makeText(ClickAndPlayMusic.this, "Cat", Toast.LENGTH_SHORT).show();
    }
});
...
private void playMedia(String songPath) {
    try {
        mp.reset();
        mp.setDataSource(songPath);
        mp.prepare();
        mp.start();    
    } catch (IOException e) {
        Log.e("TAGNAME", e.getMessage());
        e.printStackTrace();
    }
}

答案 1 :(得分:2)

mp.start()行有一个NullPointerException。这意味着mp在此时为空。检查并确保您尝试创建MediaPlayer的资源实际存在于您的文件结构中(例如,在R.raw.gata上,确保gata在您的原始文件夹中,并且是有效的媒体格式)

编辑:您可以尝试几件事。我正在写这篇文章,不能在这里编译,所以你可能需要做一些小修改,但你可以解决这个基本想法。最简单的方法是将implements OnClickListener添加到正在扩展Activity的类中,然后使用以下代码:

//inside onCreate()
int[] ids = {
    R.id.btn,
    R.id.btn2,
    //etc.
}

Button b;
MediaPlayer mp;

for (int i : ids) {
    b = 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(ClickAndPlayMusic.this, R.raw.gata);
            mp.start();
            break;
        case R.id.btn2:
            if (mp != null && mp.isPlaying()) mp.stop();
            mp = MediaPlayer.create(ClickAndPlayMusic.this, R.raw.lion);
            mp.start();
            break;
        //other cases here
    }
}

或者你可以试试这个。我从来没有这样做,但我相信它会起作用,而且代码更少。我仍然认为第一种方式更可取,只是觉得这很有趣:

int[][] data = {
    { R.id.btn, R.raw.gata },
    { R.id.btn2, R.raw.lion },
    // add all other buttons and data here
};  

MediaPlayer mp;

for (int i = 0; i < data.length; i++) {
    Button button = (Button)findViewById(data[i][0]);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (mp != null && mp.isPlaying()) mp.stop();
            mp = MediaPlayer.create(ClickAndPlayMusic.this, data[i][1]);
            mp.start();
        }
    }
}