0

嗨,我为我的一个名为 Aussie Bloke 的角色制作了一个音板——音板包含 30 个按钮和 30 个媒体播放器。我最初对所有按钮都使用了一个媒体播放器,但最终在大约 20 次随机按下后声音会停止播放。所以我决定为我使用的原始音板模板中使用的每个声音使用单独的媒体播放器对象。这工作得很好,你可以整天按下按钮并让声音正常工作..但我遇到的问题是,当活动被发送到后台并在几秒钟后再次恢复时,当你按下按钮时应用程序崩溃..我尝试创建一个 onResume 方法来准备所有玩家,但这只是让应用程序从一开始就崩溃..

package com.zammacat.aussiebloke1;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AUSSIEBLOKE1 extends Activity {

private static final String BASE64_PUBLIC_KEY = "key here";

    MediaPlayer beer1;
    MediaPlayer beer2; 
    MediaPlayer beer3; 
    MediaPlayer beauty; 
    MediaPlayer boner; 
    MediaPlayer death; 
    MediaPlayer dingo; 
    MediaPlayer dogupya; 
    MediaPlayer donotpress; 
    MediaPlayer enough; 
    MediaPlayer heaven; 
    MediaPlayer help; 
    MediaPlayer hungry; 
    MediaPlayer kebab; 
    MediaPlayer kids; 
    MediaPlayer knees; 
    MediaPlayer later; 
    MediaPlayer meatpie; 
    MediaPlayer medal; 
    MediaPlayer oath; 
    MediaPlayer poem; 
    MediaPlayer politics; 
    MediaPlayer pub; 
    MediaPlayer sex; 
    MediaPlayer sport; 
    MediaPlayer strewth; 
    MediaPlayer tango; 
    MediaPlayer v8; 
    MediaPlayer what; 
    MediaPlayer yoursister; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aussiebloke1);




     beer1 = MediaPlayer.create(this, R.raw.beer1);
     beer2 = MediaPlayer.create(this, R.raw.beer2);
     beer3 = MediaPlayer.create(this, R.raw.beer3);
     beauty = MediaPlayer.create(this, R.raw.beauty);
     boner = MediaPlayer.create(this, R.raw.boner);
     death = MediaPlayer.create(this, R.raw.death);
     dingo = MediaPlayer.create(this, R.raw.dingo);
     dogupya = MediaPlayer.create(this, R.raw.dogupya);
     donotpress = MediaPlayer.create(this, R.raw.donotpress);
     enough = MediaPlayer.create(this, R.raw.enough);
     heaven = MediaPlayer.create(this, R.raw.heaven);
     help = MediaPlayer.create(this, R.raw.help);
     hungry = MediaPlayer.create(this, R.raw.hungry);
     kebab = MediaPlayer.create(this, R.raw.kebab);
     kids = MediaPlayer.create(this, R.raw.kids);
     knees = MediaPlayer.create(this, R.raw.knees);
     later = MediaPlayer.create(this, R.raw.later);
     meatpie = MediaPlayer.create(this, R.raw.meatpie);
     medal = MediaPlayer.create(this, R.raw.medal);
     oath = MediaPlayer.create(this, R.raw.oath);
     poem = MediaPlayer.create(this, R.raw.poem);
     politics = MediaPlayer.create(this, R.raw.politics);
     pub = MediaPlayer.create(this, R.raw.pub);
     sex = MediaPlayer.create(this, R.raw.sex);
     sport = MediaPlayer.create(this, R.raw.sport);
     strewth = MediaPlayer.create(this, R.raw.strewth);
     tango = MediaPlayer.create(this, R.raw.tango);
     v8 = MediaPlayer.create(this, R.raw.v8);
     what = MediaPlayer.create(this, R.raw.what);
     yoursister = MediaPlayer.create(this, R.raw.yoursister); 



   Button b01 = (Button) findViewById(R.id.beer1);
   b01.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                beer1.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            beer1.start();              
            }
    });

   Button b02 = (Button) findViewById(R.id.beer2);
   b02.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                beer2.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            beer2.start();              
            }
    });

   Button b03 = (Button) findViewById(R.id.beer3);
   b03.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                beer3.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            beer3.start();              
            }
    });

   Button b04 = (Button) findViewById(R.id.beauty);
   b04.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                beauty.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            beauty.start();             
            }
    });

   Button b05 = (Button) findViewById(R.id.boner);
   b05.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                boner.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            boner.start();              
            }
    });

   Button b06 = (Button) findViewById(R.id.death);
   b06.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                death.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            death.start();              
            }
    });

   Button b07 = (Button) findViewById(R.id.dingo);
   b07.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                dingo.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            dingo.start();              
            }
    });

   Button b08 = (Button) findViewById(R.id.dogupya);
   b08.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                dogupya.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            dogupya.start();                
            }
    });

   Button b09 = (Button) findViewById(R.id.donotpress);
   b09.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                donotpress.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            donotpress.start();             
            }
    });

   Button b10 = (Button) findViewById(R.id.enough);
   b10.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                enough.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            enough.start();             
            }
    });

   Button b11 = (Button) findViewById(R.id.heaven);
   b11.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                heaven.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            heaven.start();             
            }
    });

   Button b12 = (Button) findViewById(R.id.help);
   b12.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                help.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            help.start();               
            }
    });

   Button b13 = (Button) findViewById(R.id.hungry);
   b13.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                hungry.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            hungry.start();             
            }
    });

   Button b14 = (Button) findViewById(R.id.kebab);
   b14.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                kebab.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            kebab.start();              
            }
    });

   Button b15 = (Button) findViewById(R.id.kids);
   b15.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                kids.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            kids.start();               
            }
    });

   Button b16 = (Button) findViewById(R.id.knees);
   b16.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                knees.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            knees.start();              
            }
    });

   Button b17 = (Button) findViewById(R.id.later);
   b17.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                later.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            later.start();              
            }
    });

   Button b18 = (Button) findViewById(R.id.meatpie);
   b18.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                meatpie.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            meatpie.start();                
            }
    });

   Button b19 = (Button) findViewById(R.id.medal);
   b19.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                medal.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            medal.start();              
            }
    });

   Button b20 = (Button) findViewById(R.id.oath);
   b20.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                oath.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            oath.start();               
            }
    });

   Button b21 = (Button) findViewById(R.id.poem);
   b21.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                poem.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            poem.start();               
            }
    });

   Button b22 = (Button) findViewById(R.id.politics);
   b22.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                politics.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            politics.start();               
            }
    });

   Button b23 = (Button) findViewById(R.id.pub);
   b23.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                pub.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            pub.start();                
            }
    });

   Button b24 = (Button) findViewById(R.id.sex);
   b24.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                sex.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sex.start();                
            }
    });

   Button b25 = (Button) findViewById(R.id.sport);
   b25.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                sport.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sport.start();              
            }
    });

   Button b26 = (Button) findViewById(R.id.strewth);
   b26.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                strewth.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            strewth.start();                
            }
    });

   Button b27 = (Button) findViewById(R.id.tango);
   b27.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                tango.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            tango.start();              
            }
    });

   Button b28 = (Button) findViewById(R.id.v8);
   b28.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                v8.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            v8.start();             
            }
    });

   Button b29 = (Button) findViewById(R.id.what);
   b29.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                what.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            what.start();               
            }
    });

   Button b30 = (Button) findViewById(R.id.yoursister);
   b30.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           try {
                yoursister.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            yoursister.start();             
            }
    });



}


protected void onStop() {

     super.onStop();
     beer1.release();
     beer2.release(); 
     beer3.release(); 
     beauty.release(); 
     boner.release(); 
     death.release(); 
     dingo.release(); 
     dogupya.release(); 
     donotpress.release(); 
     enough.release(); 
     heaven.release(); 
     help.release(); 
     hungry.release(); 
     kebab.release(); 
     kids.release(); 
     knees.release(); 
     later.release(); 
     meatpie.release(); 
     medal.release(); 
     oath.release(); 
     poem.release(); 
     politics.release(); 
     pub.release(); 
     sex.release(); 
     sport.release(); 
     strewth.release(); 
     tango.release(); 
     v8.release(); 
     what.release(); 
     yoursister.release(); 




}

}

4

1 回答 1

0

您需要onResume()在您的活动中实现方法并在那里重新创建您的媒体播放器对象:

public void onResume()  {
  super.onResume();

 // ...

 beer1 = MediaPlayer.create(this, R.raw.beer1);
 beer2 = MediaPlayer.create(this, R.raw.beer2);
 beer3 = MediaPlayer.create(this, R.raw.beer3);

 // ...
}   

您可以在此处阅读有关活动生命周期的更多信息:http: //developer.android.com/training/basics/activity-lifecycle/index.html

于 2013-02-10T08:32:06.120 回答