0

In my application, I record and save a file using the MediaRecorder. Then, when a notification gets sent, I want to play that audio file that I recorded. When I simply set the sound to the file location URI, the app crashes.

Here is the audio recorder which works and sounds great when I play it with MediaPlayer (This code is applicable to show where the file is getting saved):

//the filename is unique every time I record, but for purposes online
//it has been simplified.
_audioFilename = f.getAbsolutePath() + "/voice.3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioEncodingBitRate(133333);
mRecorder.setAudioSamplingRate(44100);
mRecorder.setOutputFile(_audioFilename);
mRecorder.setMaxDuration(10000);

Then later when the notification is called:

//Here I get the filename and prefix it with file://
//I have also tried prefixing it with content://
Uri audioUri = Uri.parse("file://" + task.getAudio());


//Create the notification channel for newer build SDKs.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        /* create a MyNotification channel */
        AudioAttributes audioAtts = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        String channelId = "myNotification";
        CharSequence channelName = "Some Channel";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.BLUE);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        notificationChannel.setSound(audioUri, audioAtts);
        notifManager.createNotificationChannel(notificationChannel);
    }


//here I build the notification and add the sound in case the SDK is older.
nb = new NotificationCompat.Builder(c, "myNotification")
           .setLargeIcon(BitmapFactory.decodeResource(c.getResources(),
                    R.mipmap.ic_launcher))
            .setSmallIcon(R.mipmap.proxi_icon_round)
            .setContentTitle("ProxiAlert: You are near "+task.getTask()+ " at " + task.getAddress())
           .setContentText("Task Description: "+task.getDescription())
            .setStyle(new NotificationCompat.BigTextStyle()
               .bigText("Task Description: "+task.getDescription()))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})      
            //program crashes here with the FileUriExposedException
            .setSound(audioUri); 

What is the solution to this? Do I have to save the Notification sound as a ringtone? Is there a different place that the file must be stored in order for the Notification to access it? Or are there other workarounds (other than using the MediaPlayer)?

cwbusacker
  • 507
  • 2
  • 12
  • 1
    maybe you can check this [link](https://commonsware.com/blog/2016/09/07/notifications-sounds-android-7p0-aggravation.html) and this [link](https://stackoverflow.com/a/48948694/6310518) – valentino Dec 11 '18 at 03:22
  • @valentino The notification doesn't throw the exception anymore, but the sound doesn't play. Is there another step that needs to be taken? – cwbusacker Dec 12 '18 at 00:35

0 Answers0