Search code examples
androidaudioandroid-notifications

How can I create a notification that plays an audio file when being tapped?


I would like to display a notification and when the user taps on it a sound file should be played.

In Android Studio I have copied the file test.mp3 into the folder app\res\raw. The notification is emitted by this code:

Resources resources = getResources();                                                 
Uri uri = new Uri.Builder()                                                           
        .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)                              
        .authority(resources.getResourcePackageName(R.raw.test))                      
        .appendPath(resources.getResourceTypeName(R.raw.test))                        
        .appendPath(resources.getResourceEntryName(R.raw.test))                       
        .build();                                                                     
                                                                                      
Intent playSoundIntent = new Intent();                                                
playSoundIntent.setAction(android.content.Intent.ACTION_VIEW);                        
playSoundIntent.setDataAndType(uri, "audio/*");                                       
                                                                                      
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,                      
        playSoundIntent, 0);                                                          
                                                                                      
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,             
        MainActivity.notificationChannelId)                                           
        .setSmallIcon(android.R.drawable.ic_media_play)                               
        .setContentTitle(getResources().getString(R.string.app_name))                 
        .setContentText("Tap to play sound!")                                         
        .setContentIntent(pendingIntent)                                              
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)                             
        .setAutoCancel(true);                                                         

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

notificationManager.notify(12345678, builder.build());                                

It does not work as expected. The notification is displayed and if I tap it, it disappears (because of setAutoCancel(true)). But I hear no sound. Why?

How can I debug it?

Thank you very much!


Solution

  • I have managed it by creating the intent like this:

    Intent playSoundIntent = new Intent(this, PlaySoundActivity.class);
    
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, playSoundIntent, 0);
    

    and adding the activity:

    public class PlaySoundActivity extends AppCompatActivity
    {
        private static MediaPlayer mediaPlayer;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            mediaPlayer = MediaPlayer.create(this, R.raw.test);
            mediaPlayer.start();
        }
    
        @Override
        public void onStop()
        {
            super.onStop();
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
    

    Although this works, I am still interested in why the former approach didn't work. I have seen many code snippets like that. And still I wonder how I could debug the former approach to understand the error. Can anybody help me?