2012-08-13 117 views
1

我的应用程序具有闹钟功能。我的要求是在闹铃响起时播放自己的声音,但我无法做到这一点。它只在闹铃响起时才显示通知。我想播放的声音文件在Res中的原始文件夹内。 下面我张贴我的代码:Android:在闹钟上播放自己的声音

在我的活动类:

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

在接收器类:

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

回答

4

您可以在您的套装或SD卡中设置自定义声音。只需将声音文件的路径设置为otification sound.use下面的代码。

notification.sound = Uri.parse("android.resource://my.package.name/raw/notification"); 
+0

谢谢,它的工作........ – Nitish 2012-09-03 16:50:36

+0

@iAndroid这不工作在我的最后你如何设置声音? – user3233280 2014-10-26 19:42:36

+0

@ user3233280请将网址字符串中的包名称替换,并在原始文件夹中添加声音文件。 – iAndroid 2014-10-27 06:50:31

0

试试这个代码,以使自己的通知声音:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
Intent notificationIntent = new Intent(this, YourActivity.class); 
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 
Notification note = new Notification(R.drawable.icon, "Your Text to Show", System.currentTimeMillis()); 
note.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 
int nid = 123456789; 
manager.notify(nid, note); 
+0

我的通知声音是原始文件夹内的水库内。我不想写到SD卡 – Nitish 2012-08-13 07:54:18

1

试图改变这两条线:

//In your activity: 
AlarmIntent.putExtra("Ringtone",getResources().getResourceName(R.raw.shankh_final_mid)); 
.... 
//In your reciever 
notification.sound = (Uri)(Uri.parse(intent.getStringExtra("Ringtone")));