Android 工作室闹钟 MP3 歌曲
Android Studio Alarm Clock MP3 Song
我正在制作闹钟。我有下面的代码。但是,通知音乐正在运行。我想让它播放我设置的 mp3 文件。我该怎么办?
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
试试下面的代码:
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarmUri == null)
{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
}
当您在 BroadcastReceiver 中收到通知时,请使用下面的代码,然后调用 activity activity class 使用下面的代码播放声音文件。
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
我正在制作闹钟。我有下面的代码。但是,通知音乐正在运行。我想让它播放我设置的 mp3 文件。我该怎么办?
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
试试下面的代码:
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarmUri == null)
{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
}
当您在 BroadcastReceiver 中收到通知时,请使用下面的代码,然后调用 activity activity class 使用下面的代码播放声音文件。
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();