如何在Android中播放铃声/闹钟声音

Fed*_*ico 113 android android-emulator

我一直在寻找如何在Android中播放铃声/闹钟声音.

我按一个按钮,我想播放铃声/闹铃声.我找不到一个简单,直接的样本.是的,我已经查看了闹钟源代码......但它并不简单,我无法编译它.

我无法做到这一点:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
    player.setAudioStreamType(AudioManager.STREAM_ALARM);
    player.setLooping(true);
    player.prepare();
    player.start();
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

04-11 17:15:27.638: ERROR/MediaPlayerService(30): Couldn't open fd for
content://settings/system/ringtone
Run Code Online (Sandbox Code Playgroud)

所以..如果有人知道如何播放默认铃声/闹钟,请告诉我.

我不想上传任何文件.只需播放默认铃声.

mar*_*v00 180

您可以使用以下功能简单地播放设置的铃声:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
Run Code Online (Sandbox Code Playgroud)

  • 我仍然收到错误 - 无法打开铃声内容://settings/system/alarm_alert (3认同)
  • 很好,很简单.但是,根据设备的不同,此方法可能会中断可能在Android中播放的其他声音(如音乐). (3认同)
  • 铃声不可阻挡。如果再次开始铃声,播放双倍。stopPrevious 不起作用,顺便说一下,我使用相同的上下文对象创建铃声播放器,而不是 getapplicationcontext。 (2认同)

Blu*_*ell 64

如果用户从未在手机上设置闹钟,则TYPE_ALARM可以返回null.您可以通过以下方式解决此问题

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

if(alert == null){
    // alert is null, using backup
    alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // I can't see this ever being null (as always have a default notification)
    // but just incase
    if(alert == null) {  
        // alert backup is null, using 2nd backup
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);                
    }
}
Run Code Online (Sandbox Code Playgroud)


igo*_*rdc 52

这就是我做的方式:

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
Run Code Online (Sandbox Code Playgroud)

它类似于markov00的方式,但使用MediaPlayer而不是Ringtone,这可以防止中断可能已经在后台播放的其他声音,如音乐.

  • 我尝试了最佳答案(ringtone.play),但声音可能会被切断.我使用这种方法,它完美地工作. (5认同)

syn*_*nic 17

你的例子基本上就是我正在使用的.但是,它永远不会在模拟器上运行,因为默认情况下模拟器没有任何铃声,并且content://settings/system/ringtone无法解析任何可播放的内容.它在我的实际手机上工作正常.


Rez*_*eza 13

可能已经晚了,但对于任何想要它的人来说,这个问题都有一个新的简单解决方案。
在科特林中

import android.provider.Settings
val player = MediaPlayer.create(this,Settings.System.DEFAULT_RINGTONE_URI)
player.start()
Run Code Online (Sandbox Code Playgroud)

上面的代码将播放默认铃声,但如果你想要默认闹钟,请更改

Settings.System.DEFAULT_RINGTONE_URI

Settings.System.DEFAULT_ALARM_ALERT_URI


Kam*_*med 10

这很好用:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
MediaPlayer thePlayer = MediaPlayer.create(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

try {
    thePlayer.setVolume((float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) / 7.0)),
                        (float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) / 7.0)));
} catch (Exception e) {
    e.printStackTrace();
}

thePlayer.start();
Run Code Online (Sandbox Code Playgroud)

  • 你为什么把音量除以7.0?这是一个众所周知的工作价值还是你自己挖掘的东西? (2认同)

Ocu*_*cuS 9

您可以使用DDMS在/ sdcard文件夹中推送MP3文件,重新启动模拟器,然后打开媒体应用程序,浏览到MP3文件,长按它并选择"用作手机铃声".

错误消失了!

编辑:使用Ringdroid应用程序解决通知声音(例如SMS)的相同问题


Mat*_*kov 9

对于未来的googlers:使用RingtoneManager.getActualDefaultRingtoneUri()而不是RingtoneManager.getDefaultUri().根据它的名字,它会返回实际的uri,所以你可以自由地使用它.来自以下文件getActualDefaultRingtoneUri():

获取当前默认声音的Uri.这将给出实际的声音Uri,而不是使用它,大多数客户端可以使用DEFAULT_RINGTONE_URI.

同时getDefaultUri()说:

返回特定类型的默认铃声的Uri.而不是返回实际铃声的声音Uri,这将返回符号 Uri,它将在播放时解析为实际声音.


小智 5

public class AlarmReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        //this will update the UI with message
        Reminder inst = Reminder.instance();
        inst.setAlarmText("");

        //this will sound the alarm tone
        //this will sound the alarm once, if you wish to
        //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();

        //this will send a notification message
        ComponentName comp = new ComponentName(context.getPackageName(),
                AlarmService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}
Run Code Online (Sandbox Code Playgroud)