Custom notification sound not playing
Asked Answered
R

1

9

I'm trying to make a custom sound play on a status bar notification. The .mp3 file is in res/raw/. But when I notify the user the sound is not played. I've tryied with MediaPlayer, and it works, but I dont want to make it play with MediaPlayer.

Here is my method:

public void showNotification()
{
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.feedback;        // icon from resources
        CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence contentTitle = mContext.getString(R.string.statusbar_notification);  // message title
        CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed);      // message text

        Intent notificationIntent = new Intent(mContext, Main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);

        //notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.sound = Uri.parse("android.resource://" + getPackageName() + "/R.raw.notificationsound");

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1, notification);
}

Thanks.

Rapping answered 7/11, 2011 at 22:37 Comment(0)
D
26

From the documentation for ContentResolver:

The Uri should be one of the following formats: android.resource://package_name/id_number

You are passing the String "R.raw.notificationsound" which means nothing. Instead try this:

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound );
Dizzy answered 7/11, 2011 at 22:55 Comment(1)
how do i set duration cycle for these notification sound...how to repeat pattern on theseGroh

© 2022 - 2024 — McMap. All rights reserved.