Darko Petkovski
Darko Petkovski

Reputation: 3912

Android notifications play sound from assets folder

I'm trying to play notifications sound from the assets folder, but when notification triggers the notification is shown but no sound is played and there is no error or warning in the logCat. I'm using this code for creating notifications:

builder = new NotificationCompat.Builder(context)
                    // Set Icon
                    .setSmallIcon(R.drawable.ic_launcher)
                    // Set Ticker Message
                    .setTicker(message)
                    // Set Title
                    .setContentTitle(message)
                    // Set Text
                    .setContentText(context.getString(R.string.app_name))
                    // Add an Action Button below Notification
                    .addAction(R.drawable.share,
                            context.getString(R.string.share), pendingShare)
                    // Set PendingIntent into Notification
                    .setContentIntent(contentIntent)
                    // Dismiss Notification
                    .setAutoCancel(true)
                    .setSound(
                            Uri.parse("file:///android_assets/"
                                    + prefs.getString(Constants.NOTIF_SOUND,
                                            "mp3/al_affassi_full.mp3")));

can someone help me solve this issue? Thanks!

Upvotes: 1

Views: 2501

Answers (3)

Mangala Savadi
Mangala Savadi

Reputation: 1

Create a folder named raw under res, and copy your mp3 file into that folder.

Then you can set,

setSound(Uri.parse("android.resource://"your package name"/"
                    + R.raw.your_map3_name);

And it should work.

Upvotes: 0

Hardip
Hardip

Reputation: 360

Android notifications play sound from raw folder   
 try
    {

        int res_sound_id = MainActivity.this.getResources().getIdentifier("sound name", "raw", MainActivity.this.getPackageName());
        Uri u= Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/" +res_sound_id );

        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        long[] pattern = { 0, 200, 500 };
        Notification mNotification = new Notification.Builder(MainActivity.this)
                .setContentTitle("Its time to Pray")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setSound(u)
                .setVibrate(pattern)
                .setAutoCancel(true)
                .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, mNotification);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

Upvotes: 0

greywolf82
greywolf82

Reputation: 22183

Put your mp3 file under raw folder. If you put it under asset/ it's going to be compressed twice and you can't use it.

Upvotes: 1

Related Questions