所以我有一個.mp3 in
資產的文件夾(我打算以後有更多),我有相關的發揮它的簡單點擊:.MP3播放的MediaPlayer使用只有一個時間,而不是循環
public static MediaPlayer m = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.btnDrum);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
PlayMusic(v);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void PlayMusic(View v) throws IOException {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd(Kit.KickDrum);
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
然而,每次我嘗試點擊按鈕,我都在循環播放音樂!我只需要它來玩一次,並停止!如何才能做到這一點?
謝謝!
是不是很明顯,你要求它循環在你的代碼? 'm.setLooping(true);' –