[英]Android Studio Alarm Clock MP3 Song
I'm making an alarm clock.我在做闹钟。 I have the codes below.我有下面的代码。 However, the notification music is working.但是,通知音乐正在运行。 I want it to play the mp3 file I set.我想让它播放我设置的 mp3 文件。 What should I do?我该怎么办?
@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();
}
Try this below code :试试下面的代码:
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarmUri == null)
{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
}
Please used below code when you get notification in BroadcastReceiver then call activity in that activity class used below code so play sound file.当您在 BroadcastReceiver 中收到通知时,请使用下面的代码,然后在下面使用的代码中调用该活动类中的活动,以便播放声音文件。
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.