Android:在闹钟上播放自己的声音

Nit*_*ish 1 android alarmmanager

我的应用程序有闹钟功能。我的要求是在闹钟响起时播放自己的声音,但我无法做到这一点。仅当警报响起时才显示通知。我要播放的声音文件位于 Res 的 raw 文件夹内。下面我发布我的代码:

在我的活动课上:

Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
        AlarmIntent.putExtra("Ringtone", 
                Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
        PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 
                (60 * 1000), (24 * 60 * 60 * 1000), Sender);
Run Code Online (Sandbox Code Playgroud)

在接收器类中:

public void onReceive(Context context, Intent intent) { 

    Intent in = new Intent(context, SnoozeEvent.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
    manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.sound = (Uri)intent.getParcelableExtra("Ringtone");
    manager.notify(1, notification);  
}
Run Code Online (Sandbox Code Playgroud)

iAn*_*oid 5

您可以在捆绑包或 SD 卡中设置自定义声音。仅将声音文件的路径设置为通知声音。使用下面的代码。

notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
Run Code Online (Sandbox Code Playgroud)