【问题标题】:sound for notification通知声音
【发布时间】:2012-11-16 17:06:42
【问题描述】:

我想在我的Notification 被触发时播放 mp3 声音。为此,我在“res/raw”文件夹中放置了一个 mp3 文件,并使用了以下说明:

notification.sound=Uri.parse("android.resource://"+getPackageName()+"/" + R.raw.mySound);.

但是当Notifications 出现时我没有声音!

有什么想法吗?

【问题讨论】:

    标签: java android audio notifications


    【解决方案1】:

    只需将下面的代码放在将在通知发生时触发的块中..

    mMediaPlayer = new MediaPlayer();
    mMediaPlayer = MediaPlayer.create(this, R.raw.mySound);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setLooping(true);  // Set false if you don't want it to loop
    mMediaPlayer.start();
    

    【讨论】:

    • 解决附带问题:声音级别不受通知声音级别控制,而是由媒体声音级别控制...
    【解决方案2】:

    我在这里找到了一个例子 -

    这是使用的代码

    try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone ring = RingtoneManager.getRingtone(getApplicationContext(), notification);
            ring.play();
        } catch (Exception e) {}
    

    另一方面,如果你想自定义声音(我猜你就是这样做的),你应该使用来自here的这段代码

    notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
    notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE
    

    ;

    【讨论】:

    • 第二种方式不行。它准确地显示了我在做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2018-08-21
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多