2012-12-26 17 views
20

आप NotificationCompat.builder द्वारा बनाई गई अधिसूचना में ध्वनि कैसे जोड़ते हैं? मैंने res में एक कच्चा फ़ोल्डर बनाया और वहां ध्वनि को जोड़ा। तो अब मैं इसे अधिसूचना में कैसे जोड़ूं? setSound(Uri soundUri) - यह मेरा अधिसूचना कोडअधिसूचना में ध्वनि कैसे जोड़ें?

int NOTIFY_ID=100; 
    Intent notificationIntent = new Intent(this, Notification.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(R.drawable.notification) 
      .setContentTitle("Warning") 
      .setContentText("Help!") 

    NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mgr.notify(NOTIFY_ID, mBuilder.build()); 
+0

एक [सेटसाउंड] है (http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSound (android.net.Uri)) विधि [NotificationCompat.builder ] (http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html)। क्या आप यही खोज रहे हैं? –

उत्तर

40

मैं यहाँ समस्या अनुमान लगा रहा हूँ एक Uri के साथ ध्वनि के संदर्भ के लिए कैसे, के रूप में वहाँ NotificationCompat.Builder कक्षा में एक स्पष्ट तरीका है है।

अपने raw संसाधनों का उपयोग करने के लिए आप Uri बनाने के लिए इस प्रकार की जरूरत है:

android.resource://[PACKAGE_NAME]/[RESOURCE_ID]

तो कोड है कि तरह लग रही हो सकते हैं:

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd); 
mBuilder.setSound(sound); 
+0

मेरी ध्वनि फ़ाइल में किस प्रारूप में होना आवश्यक है? मुझे अपने बजाय एक डिफ़ॉल्ट ध्वनि मिल रही है। – karl

+2

मैंने परीक्षण और त्रुटि के साथ अपने स्वयं के प्रश्न का उत्तर दिया है: 1) एमपी 3 काम करता है, wav नहीं करता है, और 2) मुझे अधिसूचना बनाने और फिर 'n.defaults और = ~ अधिसूचना .DEFAULT_SOUND करके डिफ़ॉल्ट ध्वनि को अक्षम करने की आवश्यकता है ; – karl

+3

यदि आप अधिसूचना.बिल्डर का उपयोग कर रहे हैं, * builder.setDefaults (~ अधिसूचना.DEFAULT_SOUND); * चाल करेगा। टिप के लिए @karl के लिए धन्यवाद – Maragues

15

अपनी अधिसूचना के साथ एक ध्वनि खेलने के लिये:

Notification notification = new Notification(icon, tickerText, when); 

सामान्य अधिसूचना करें प्रक्रियाओं

अपनी अधिसूचना के साथ डिफ़ॉल्ट ध्वनि चलाने के लिए:

notification.defaults |= Notification.DEFAULT_SOUND; 

अपनी अधिसूचना के साथ एक कस्टम ध्वनि चलाने के लिए:

notification.sound = Uri.parse("file:///sdcard/notification/notification.mp3"); 

तो बस अधिसूचना प्रबंधक का उपयोग सूचना भेजने के लिए। यदि इन दोनों कथनों का उपयोग किया जाता है, तो एप्लिकेशन डिफ़ॉल्ट ध्वनि का उपयोग करने के लिए डिफ़ॉल्ट होगा।

संबंधित मुद्दे