I'm trying to make a audio app in Android using this video as a guide, and I'm having trouble with creating the notification using the audio's metadata.
Here is the code I use to extract the audio files from the device:
public void loadData() {
ContentResolver contentResolver = context.getContentResolver();
String selection = Media.IS_MUSIC + "!= 0";
String[] projection = {
Media._ID,
Media.ARTIST,
Media.TITLE,
Media.DISPLAY_NAME,
Media.DURATION,
Media.ALBUM
};
Cursor cursor = contentResolver.query(
Media.EXTERNAL_CONTENT_URI, projection, selection, null, null
);
if(cursor == null || !cursor.moveToFirst()) return;
else {
int size = cursor.getCount();
metadata = new ArrayList<>(size);
mediaItems = new ArrayList<>(size);
}
int
idIndex = cursor.getColumnIndex(Media._ID),
artistIndex = cursor.getColumnIndex(Media.ARTIST),
titleIndex = cursor.getColumnIndex(Media.TITLE),
displayNameIndex = cursor.getColumnIndex(Media.DISPLAY_NAME),
durationIndex = cursor.getColumnIndex(Media.DURATION),
albumIndex = cursor.getColumnIndex(Media.ALBUM);
do {
String mediaId = cursor.getString(idIndex);
MediaMetadataCompat data = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, cursor.getString(idIndex))
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, cursor.getString(albumIndex))
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, cursor.getString(artistIndex))
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, cursor.getString(titleIndex))
.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, cursor.getString(displayNameIndex))
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, cursor.getLong(durationIndex))
.build();
metadata.add(data);
mediaItems.add(new MediaBrowserCompat.MediaItem(
data.getDescription(),
MediaItem.FLAG_PLAYABLE
));
} while(cursor.moveToNext());
cursor.close();
}
public List<MediaItem> getMediaItems() {
return mediaItems;
}
And here's my MediaBrowserService's onLoadChildren code:
@Override
public void onLoadChildren(@NonNull String parentId,
@NonNull Result<List<MediaBrowserCompat.MediaItem>> result
) {
if(parentId.equals(MEDIA_ROOT_ID)) {
result.sendResult(source.getMediaItems());
}
}
And lastly, the code throwing the error (I call createNotifcation() after creating the MediaSession and setActive(true)):
private void createNotification() {
NotificationCompat.Action playAction = new NotificationCompat.Action(
android.R.drawable.ic_media_play,
getString(R.string.play),
MediaButtonReceiver.buildMediaButtonPendingIntent(
ExoMusicService.this,
PlaybackStateCompat.ACTION_PLAY
)
);
NotificationCompat.Action pauseAction = new NotificationCompat.Action(
android.R.drawable.ic_media_pause,
getString(R.string.pause),
MediaButtonReceiver.buildMediaButtonPendingIntent(
ExoMusicService.this,
PlaybackStateCompat.ACTION_PAUSE
)
);
MediaControllerCompat controller = mediaSession.getController();
Log.i(LOG_TAG, "createNotification: " + (controller == null)); // false
MediaMetadataCompat metadata = controller.getMetadata();
Log.i(LOG_TAG, "createNotification: " + (metadata == null)); // true
MediaDescriptionCompat description = metadata.getDescription();
notification = new NotificationCompat.Builder(ExoMusicService.this, PLAYBACK_CHANNEL_ID)
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setContentIntent(controller.getSessionActivity())
.setStyle(
new MediaStyle()
.setShowActionsInCompactView(0)
.setMediaSession(mediaSession.getSessionToken())
.setShowCancelButton(true)
)
.setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
ExoMusicService.this,
PlaybackStateCompat.ACTION_STOP
))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.addAction(playerManager.isPlaying()? pauseAction : playAction)
.setSmallIcon(R.drawable.icon)
.build();
}
If I understood correctly, I believe that's all the code relating to this issue. Any idea as to what I could be overlooking?
Before show notification you should call mediaSession.setMetadata(/* your metadata */).
Metadata should contains information about current music item, which is playing (or preparing, paused, ect.).
Please refer to the documentation for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With