繁体   English   中英

如何播放Android通知声音

[英]How to play an android notification sound

我想知道如何在不通过媒体流播放通知声音的情况下播放通知声音。 现在,我可以通过媒体播放器来执行此操作,但是我不希望它作为媒体文件播放,我希望它作为通知或警报或铃声播放。 这是我的代码现在看起来像的一个示例:

MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();

如果有人仍在寻找解决方案,我在如何在Android中播放铃声/警报声音中找到了答案

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

您可以将TYPE_NOTIFICATION更改为TYPE_ALARM,但是您希望跟踪自己的Ringring r以便停止播放……例如,当用户单击按钮或其他东西时。

现在,您可以通过在构建通知时包括声音来实现此目的,而不必分别调用声音。

//Define Notification Manager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(icon)
        .setContentTitle(title)
        .setContentText(message)
        .setSound(soundUri); //This sets the sound to play

//Display notification
notificationManager.notify(0, mBuilder.build());

如果要播放默认的通知声音,则可以使用NotificationCompat.Builder类的setDefaults(int)方法:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(someText)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setAutoCancel(true);

我相信这是完成任务的最简单方法。

尝试这个:

public void ringtone(){
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
     } catch (Exception e) {
         e.printStackTrace();
     }
}

提问以来已有一段时间,但是...您是否尝试设置音频流类型?

mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

必须在准备之前完成。

我有几乎相同的问题。 经过研究,我认为如果要播放默认的系统“通知声音”,则几乎必须显示一个通知,并告诉它使用默认声音。 在其他一些答案中,还有一些要说的论点,即如果您正在播放通知声音,则还应该显示一些通知消息。

但是,对通知API进行一些调整,即可接近所需的内容。 您可以显示空白通知,然后在几秒钟后自动将其删除。 我认为这对我有用; 也许它将为您工作。

我在com.globalmentor.android.app.Notifications.java创建了一组便捷方法,这些方法可让您创建如下通知声音:

Notifications.notify(this);

LED也会闪烁,如果您允许振动,将会发生振动。 是的,通知图标将出现在通知栏中,但会在几秒钟后消失。

此时,您可能会意识到,由于无论如何该通知都会消失,因此您还可能在通知栏中显示滚动条消息。 您可以这样做:

Notifications.notify(this, 5000, "This text will go away after five seconds.");

此类中还有许多其他便利方法。 您可以从其Subversion存储库下载整个库,并使用Maven进行构建。 它取决于globalmentor-core库,该库也可以使用Maven构建和安装。

您可以使用NotificationNotificationManager来显示所需的通知。 然后,您可以自定义要与通知一起播放的声音。

Intent intent = new Intent(this, MembersLocation.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("type",type);
    intent.putExtra("sender",sender);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);

    Uri Emergency_sound_uri=Uri.parse("android.resource://"+getPackageName()+"/raw/emergency_sound");
   // Uri Default_Sound_uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if(type.equals("emergency"))
    {
        playSound=Emergency_sound_uri;
    }
    else
    {
        playSound= Settings.System.DEFAULT_NOTIFICATION_URI;
    }

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setSound(playSound, AudioManager.STREAM_NOTIFICATION)
                    .setAutoCancel(true)
                    .setColor(getColor(R.color.dark_red))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent);

   // notificationBuilder.setOngoing(true);//for Android notification swipe delete disabling...

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_HIGH);
        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build();
        channel.setSound(Emergency_sound_uri, att);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }

    if (notificationManager != null) {
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

将声音设置为通知频道

        Uri alarmUri = Uri.fromFile(new File(<path>));

        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build();

        channel.setSound(alarmUri, attributes);

我认为Android UI的“通知声音”概念有些错误。

Android的预期行为是使用标准通知来提醒用户。 如果您播放没有状态栏图标的通知声音,则会使用户感到困惑(“那是什么声音?这里没有图标,也许我有听力问题?”)。

如何在通知上设置声音,例如,在这里: 设置通知的声音

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM