1
我的應用程序中有40多個聲音文件。在我的一項活動中,我想根據Bundle b
(id範圍:1 - 50)附帶的id
播放only one sound file
。我嘗試這樣做:Android MediaPlayer聲音解決問題
在的onCreate():
Bundle b = getIntent().getExtras();
id = (int) b.getInt("id");
tv.setText("id : "+id); // Just to make sure that I received a correct id
setResourseId(id);
mPlayer.start();
的setResourceId():
public void setResourseId(int id) {
switch(id) {
case 1: mPlayer = MediaPlayer.create(this, R.raw.sound1);
case 2: mPlayer = MediaPlayer.create(this, R.raw.sound2);
case 3: mPlayer = MediaPlayer.create(this, R.raw.sound3);
case 4: mPlayer = MediaPlayer.create(this, R.raw.sound4);
case 5: mPlayer = MediaPlayer.create(this, R.raw.sound5);
.
.
.
.
.
default: mPlayer = MediaPlayer.create(this, R.raw.sound0);
}
}
然而,它總是玩sound0 file
(默認的) !這似乎很奇怪,因爲TextView tv
總是打印正確的ID
-
對於任何解決方案?
我忘了添加它,因爲我最初正在返回一個MediaPlayer對象。謝謝。 – iTurki
歡迎你。 :) – MByD