我正在尝试制作音板应用,但我遇到了处理声音文件的问题。更具体地说,我不知道如何获得它,所以当按下按钮时它会播放相应的声音。
我尝试过这种方式 Play sound on button click android
我尝试为每个声音文件和按钮实现媒体播放器,但应用程序在启动时崩溃。
我有10个按钮,其中有10个相应的声音文件位于src / res / raw /
中的原始文件中关于如何使其发挥作用的任何想法?
//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();
}
}
记录Cat错误: http://imgur.com/ZWYsLl7
答案 0 :(得分:0)
我认为您的声音可能是sound_1
,sound_2
,sound_3
等,而您的按钮是button_1
,button_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();
}
});
}
阅读主题参考: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
部分。我不确定但它应该做的伎俩..
祝好运。
<强>更新强>
将此检查if(view==whatup)
更改为if(view.getId() == R.id.button)
在onClick
方法中,您需要使用getId()
方法检查有关视图的ID。