Issue
I am trying to play sound from a notification, so I have code like
Notification notification = new NotificationCompat.Builder(context)
.setSound(uri)
// ... more builder options
.build();
I tried two different ways of constructing the uri. The first was
Uri uri = Uri.parse(String.format("android.resource://%s/%d",
context.getPackageName(),
resourceId));
and the second was
Uri uri = new Uri.Builder().scheme("android.resource")
.path("//")
.appendPath(context.getPackageName())
.appendPath(Integer.toString(resourceId))
.build();
If I print the uris generated by each of these techniques, I get an identical string:
android.resource://com.example.notification/2130968576
However, the sound plays when I use the first technique but not when I use the second one. Why is this?
I have observed this behavior on Android 4.3 and Android 4.4, with the v4 support library.
Solution
You have to set the correct authority in your uri instead of prepending //:
Uri uri = new Uri.Builder().scheme("android.resource")
.authority(context.getPackageName())
.path(Integer.toString(resourceId))
.build();
I admit it's confusing since both methods return the same string and compare equal, but that's what fixed it for me.
Answered By - mick88
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.