Get all the media items from the media db on the device
This set of functions will help you to get all the media items listed in the media database and list their properties such as name and file path.
#include <dlog.h> //for logging purposes
#include <media_content.h>
#include <stdlib.h>
//Callback to be executed on each media item found in the media database on the device.
bool media_callback(media_info_h media, void *user_data)
{
// Here you can do anything with the media item found.
// In this snippet we get the display name and file path and print them to logs.
char *name = NULL;
char *path = NULL;
if ((media_info_get_display_name(media, &name) == MEDIA_CONTENT_ERROR_NONE)
&& (media_info_get_file_path(media, &path) == MEDIA_CONTENT_ERROR_NONE))
{
dlog_print(DLOG_INFO, LOG_TAG, "Media item found!");
dlog_print(DLOG_INFO, LOG_TAG, "Name: %s", name);
dlog_print(DLOG_INFO, LOG_TAG, "Path: %s", path);
}
else
{
dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get media properties!");
}
free (name);
free (path);
return true;
}
bool list_media_items()
{
if (media_content_connect() == MEDIA_CONTENT_ERROR_NONE)
{
if (media_info_foreach_media_from_db(NULL, media_callback, NULL) != MEDIA_CONTENT_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when trying to list media!");
media_content_disconnect();
return false;
}
if (media_content_disconnect() != MEDIA_CONTENT_ERROR_NONE)
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when disconnecting from media db!");
return false;
}
return true;
}
else
{
dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when connecting to media db!");
return false;
}
}