我想做一个通用音板。
但它不会运行,这很奇怪,而我没有任何错误。
我在网上搜索了解决方案,但找不到。
这是我使用的代码:
package com.soundboard;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class Soundboard extends Activity implements OnClickListener {
MediaPlayer player = new MediaPlayer();
Resources res = getResources();
int[] buttonIds = {R.id.sound1,R.id.sound2,R.id.sound3,R.id.sound4,R.id.sound5,R.id.sound6,R.id.sound7,R.id.sound8,R.id.sound9,R.id.sound10,R.id.sound11,R.id.sound12,R.id.sound13,R.id.sound14,R.id.sound15,R.id.sound16};
int[] soundIds = {R.raw.sound1, R.raw.sound2, R.raw.sound3, R.raw.sound4, R.raw.sound5, R.raw.sound6, R.raw.sound7, R.raw.sound8, R.raw.sound9, R.raw.sound10, R.raw.sound11, R.raw.sound12, R.raw.sound13, R.raw.sound14, R.raw.sound15, R.raw.sound16};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
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 = getResources().openRawResourceFd(soundIds[i]);
try {
player.reset();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
player.prepare();
player.start();
afd.close();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
我希望你们能帮助我。
谢谢,
流