android声音代码,如何播放android通知声音

此博客给出了Android中设置通知声音的代码。通过Intent传递参数,根据不同类型选择不同声音,使用NotificationCompat.Builder构建通知,设置声音、标题等属性,还针对Android Oreo及以上版本创建通知渠道并设置声音,最后进行通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值