我需要制作一个从原始文件夹播放音频的应用程序。 我为图像设置了OnClickListener,但它没有工作。
这是我的代码:
public class DetailInko extends Activity {
DBHelper dbHelper;
private TextView hangeul, romanisasi, artiIndo, keterangan;
String txAud;
private ImageView Audio;
Button bck;
public DetailInko() {
}
public void openDB() {
dbHelper = new DBHelper(this);
dbHelper.open();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_inko);
hangeul = (TextView) findViewById(R.id.hangeul);
romanisasi = (TextView) findViewById(R.id.romanisasi);
artiIndo = (TextView) findViewById(R.id.artiIndonesia);
keterangan = (TextView) findViewById(R.id.artiPlus);
Audio = (ImageView) findViewById(R.id.buttonSound);
bck = (Button) findViewById(R.id.back);
//来自之前活动的getdata,它已经运作良好
Bundle b = new Bundle();
b = this.getIntent().getExtras();
if (b != null) {
hangeul.setText(b.getString("Kor"));
romanisasi.setText(b.getString("Rom"));
artiIndo.setText(b.getString("Indo"));
keterangan.setText(b.getString("Ket"));
txAud = b.getString("Audio").replaceAll(".WAV", ""); // I'm not sure about this code
}
final MediaPlayer mp;
int i;
//从原始文件夹中获取音频,但我不知道此代码是否可以从文件夹raw获取.wav文件。声音没有发挥
i = getResources().getIdentifier(txAud, "raw", getPackageName());
mp = MediaPlayer.create(DetailInko.this, i);
onCLick = new OnClickListener() {
@Override
public void onClick(View v) {
if(mp != null){
mp.start();
}
}
};
}
有人能解决这个问题吗?谢谢 -
答案 0 :(得分:0)
试试这个:
just Put your file in res/raw folder and use
public void onClick(查看v){
if(mp.isPlaying())
{
mp.stop();
} else{
try {
mp = MediaPlayer.create(this, R.raw.hello);
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}