我試圖製作一個音板應用程序,但是我遇到了處理聲音文件的問題。更具體地說,我不知道如何獲得它,所以當按下按鈕時,它會播放相應的聲音。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
這看起來很有幫助。我剛發現一篇文章說我應該使用soundpool而不是媒體播放器。你知道差異嗎? – Programatic
我剛剛讀到相反的:http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/ - 太棒了! ^^ – Fllo
我切換了它,它仍然在啓動時崩潰。這是我的代碼,我有onCreate – Programatic