开发者

My music app dies after 7 songs are played

i have created an app with 16 btns,every btn plays a sound mp3.my app starts with no problem.i press and listen one mp3,two mp3...until 7 mp3...there my app forces down.i can play any mp3 i like but i can only play the 7 mp3 i click first,where is my error?

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

        });      

this is the way i have created every music btn...thanks!(hope my answer is understandable)

ps:this is my logcat view when it forces down:

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.44开发者_开发技巧9: 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 line is the mp.start(); in the onClick method of the first btn!

second 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)


The documentation for MediaPlayer#create(android.content.Context, int) states:

When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.

Whether this is the cause of you problem or not will depend on exactly the type of error you are experiencing.

EDIT

This is the kind of thing you need to do. Having never used the MediaPlayer myself this may need some work on your part to make it exactly fit your needs.

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


You have a NullPointerException at the mp.start() line. That means mp is null at that point. Check and ensure that the resources you're trying to create the MediaPlayer with actually exist in your file structure (e.g. on R.raw.gata, make sure gata is in your raw folder, and is a valid media format).

EDIT: A couple of things you can try. I'm writing this off my head, can't compile here so you may have to make some minor modifications, but you can just work off the basic idea. The easiest way would be to just add implements OnClickListener to your class that's extending Activity, and then use the following code:

//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
    }
}

Or you could try this. I've never done it like this, but I believe it would work, and be less code. I still think the first way's preferable, just thought this would be fun:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜