2014-03-26 22 views
0

我試圖製作一個音板應用程序,但是我遇到了處理聲音文件的問題。更具體地說,我不知道如何獲得它,所以當按下按鈕時,它會播放相應的聲音。Android按鈕音板

我用這種方式 Play sound on button click android

我試圖實現對每個聲音文件和按鈕,但在啓動該應用程序崩潰的媒體播放器嘗試。

我有10個按鈕與10對應的是分佈在SRC/RES原始文件的聲音文件/生/

如何使它工作的任何想法?

//this is a breaking bad soundboard app, so please excuse the language. Sorry if you  find it offensive 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()) 
       .commit(); 
    } 

    whatup = (Button)this.findViewById(R.id.button); 
    haveatit = (Button)this.findViewById(R.id.button2); 
    hello = (Button)this.findViewById(R.id.button3); 
    whining = (Button)this.findViewById(R.id.button7); 
    money = (Button)this.findViewById(R.id.button4); 
    yeah= (Button)this.findViewById(R.id.button8); 
    miserable = (Button)this.findViewById(R.id.button5); 
    mother = (Button)this.findViewById(R.id.button9); 
    gatorade = (Button)this.findViewById(R.id.button6); 
    science = (Button)this.findViewById(R.id.button10); 

    whatup.setOnClickListener(this); 
    haveatit.setOnClickListener(this); 
    hello.setOnClickListener(this); 
    whining.setOnClickListener(this); 
    money.setOnClickListener(this); 
    yeah.setOnClickListener(this); 
    miserable.setOnClickListener(this); 
    mother.setOnClickListener(this); 
    gatorade.setOnClickListener(this); 
    science.setOnClickListener(this); 

    WhatUp = MediaPlayer.create(MainActivity.this, R.raw.whatupb); 
    HaveAtIt = MediaPlayer.create(MainActivity.this, R.raw.haveatit); 
    Hello = MediaPlayer.create(MainActivity.this, R.raw.hello); 
    Whining = MediaPlayer.create(MainActivity.this, R.raw.stopwhining); 
    Money = MediaPlayer.create(MainActivity.this, R.raw.wheresmymoney); 
    Yeah = MediaPlayer.create(MainActivity.this, R.raw.yeahb); 
    Miserable = MediaPlayer.create(MainActivity.this, R.raw.miserableb); 
    Mother = MediaPlayer.create(MainActivity.this, R.raw.motherofgod); 
    Gatorade = MediaPlayer.create(MainActivity.this, R.raw.gatorade); 
    Science = MediaPlayer.create(MainActivity.this, R.raw.yeahscience); 

} 

這是用於按下按鈕時的處理。顯然需要更多,但我只是測試1個按鈕,當我嘗試啓動它時崩潰。

@Override 
public void onClick(View view) { 
    if(view==whatup) 
    { 
     WhatUp.start(); 
    } 
} 

登錄貓錯誤: http://imgur.com/ZWYsLl7

回答

0

我假設你的聲音可能是sound_1sound_2sound_3等,你的按鈕button_1button_2等..
你應該創建一個循環在onCreate方法中獲得ID:

for (int i = 0; i < 10; i++) { 
    // get the id for buttons 
    int btnId = getResources().getIdentifier("button_"+i, "id", getPackageName()); 
    // get the res for sounds 
    int rawId = getResources().getIdentifier("sound_"+i, "raw", getPackageName()); 
    // set a click listener to all your buttons 
    button[i] = (Button) findViewById(id); 
    button[i].setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      // create the media player with the raw id 
      mp = MediaPlayer.create(MainActivity.this, rawId); 
      mp.setOnCompletionListener(new OnCompletionListener() { 
       @Override 
       public void onCompletion(MediaPlayer mp) { 
        // TODO Auto-generated method stub 
        // avoid NullPointerException see the links below 
        mp.release(); 
       } 
      }); 
      // play the sound 
      mp.start(); 
     } 
    }); 
} 

閱讀topi c參考:Using MediaPlayer。您還可以根據上下文獲得標識:How to dynamically generate the raw resource identifier in android?,這可能會幫助您解釋release()方法:Play sound on button click - Null pointer exception並從Using MediaPlayer中讀取Releasing the MediaPlayer部分。我不知道,但它應該做的伎倆..
祝你好運。


UPDATE

更改此檢查if(view==whatup)if(view.getId() == R.id.button)
onClick方法中,您需要檢查與視圖相關的ID,方法爲getId()

+0

這看起來很有幫助。我剛發現一篇文章說我應該使用soundpool而不是媒體播放器。你知道差異嗎? – Programatic

+0

我剛剛讀到相反的:http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/ - 太棒了! ^^ – Fllo

+0

我切換了它,它仍然在啓動時崩潰。這是我的代碼,我有onCreate – Programatic