Play an mp3 file from the phone storage

This is a piece of code showing how to play an mp3 file using Player API. To access a media file from the phone storage you need to request privilege. PRIVILEGE NEEDED: http://tizen.org/privilege/mediastorage
#include <player.h>
#include <dlog.h> //for logging purposes

player_h player;

int err = PLAYER_ERROR_NONE;

// Preparing the player:
if (player_create(&player) == PLAYER_ERROR_NONE
    && player_set_uri(player, "/opt/usr/media/Music/Your_music_file.mp3")== PLAYER_ERROR_NONE
    && player_prepare(player) == PLAYER_ERROR_NONE)
{
    LOGI("Player prepared.");
}
else
{
    LOGE("Error when preparing player!");
}
// Starting playback:
player_state_e state;
err = player_get_state(player, &state);
if (err == PLAYER_ERROR_NONE && state == PLAYER_STATE_READY)
{
    LOGI("Player is ready.");
    if (player_start(player)== PLAYER_ERROR_NONE)
    {
        LOGI("Player started!");
    }
}
else
{
    LOGE("Player is not ready! Player state: %d", state);
}

// When you want to stop playback:
if (player_stop(player) == PLAYER_ERROR_NONE)
{
    LOGI("Player stopped!");
}
else
{
    LOGE("Error when stopping!");
}

// When you don't need your player anymore:
if (player_unprepare(player) == PLAYER_ERROR_NONE)
{
    LOGI("Player unprepared!");
}
else
{
    LOGE("Error when unpreparing!");
}
if (player_destroy(player) == PLAYER_ERROR_NONE)
{
    LOGI("Player destroyed!");
}
else
{
    LOGE("Error when destroying!");
}

Responses

0 Replies