2011-10-10 6 views
0

私のタスクが実行され、最後のいくつかのサウンドまでうまく動作します。それは力の閉鎖を保持するいくつかの奇妙な理由で、私は見て見て、理由を取得しないでください。私は離れて音を出し、いくつかの他のステップを行ったと私はサイズの制限や何かを理解していない?Androidのサウンドボードのエラー

2番目のプログラムですが、これは何のアイデアですか?

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

     } 
    }); 

....などがあります。だから、ファイルのエラーはどこにあり、なぜ強制終了されますか?

+0

あなたはアプリのクラッシュ製造されているlogcatメッセージを投稿することができますか? – slayton

+0

ヌルポインタの例外はそれがリストしている唯一のものですが、私はそれがクラッシュするメディアプレーヤーのあまりにも多くのインスタンスに沸騰していると確信しています。 –

答えて

1

私は28 MediaPlayerオブジェクトを持つことが多くのメモリを食べていると思っています。ボタンを押すたびに新しいサウンドで再初期化されるオブジェクトMediaPlayerの使用を検討してください。この記事をチェックしてください:http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/

+0

私は16MBの制限を壊してしまったので、サウンドプールは動作しません –

+1

サウンドプールを使用することを勧めていませんでしたか? – slayton

+0

私はstealthcopterリンクを使用して、さらに研究をしました。私の方法はうまくいっていましたが、全く新しいスタイル。このリンクをありがとう。 –

0

確かに、FARには多すぎる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); 
} 
+0

リスナーに問題があることを除いて、コードはすべてコンパイルされますが、どの変数をリスナーとしてグローバルに定義すれば問題はありませんか? –

+0

私はこれをOncreateアクティビティに直接配置しました.Im new to javaはこれを実装するはずです。 –

+0

さて、これは私の頭の提案の一番上にあったものであり、必ずしもコンパイルされたものではありません。リスナーを定義しようとする場所の前でリスナー定義を移動する必要があります(リスナー宣言の後にsetOnClickListener forループを移動するなど)。 – kcoppock