ANDROID应用开发中 怎么给ACTIVITY添加背景音乐最简单

2024-12-23 06:54:17
推荐回答(1个)
回答1:

在您需要的地方调用即可

private MediaPlayer mMediaPlayer;

private void playLocalFile() {
mMediaPlayer = MediaPlayer.create(this, R.raw.in_call_alarm);
//播放工程res目录下的raw目录中的音乐文件in_call_alarm

try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {

} catch (IOException e) {

}

mMediaPlayer.start();
headsetplay.setEnabled(false);

mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//播完了接着播或者关闭mMediaPlayer
});
}