Page Example

Media-controller client Sample Overview

Mobile native

The media-controller-client sample application demonstrates how you can create a custom media controller client type application. This sample application is based on a simple lockscreen application.

The following figure illustrates the main view of the media-controller-client sample application.

Figure: Media-controller-client main view

Media-controller-client main view

Prerequisites

To ensure proper application execution, the following privilege must be set:

  • http://tizen.org/privilege/mediacontroller.client

Implementation

To use the media controller client:

  1. Create the media controller client handle in the __create_player_client() function. The function sets the callbacks to receive the update event from the media controller server.
    static void 
    __create_player_client(void)
    {
       int ret = mc_client_create(&s_player_data.player_client);
    
       if (ret != MEDIA_CONTROLLER_ERROR_NONE) 
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "Media controller client creation [%d]", ret);
    
          return;
       }
    
       // Callbacks on server events
       mc_client_set_server_update_cb(s_player_data.player_client, __player_state_changed_cb, NULL);
       mc_client_set_playback_update_cb(s_player_data.player_client, __playback_updated_cb, NULL);
       mc_client_set_metadata_update_cb(s_player_data.player_client, __metadata_updated_cb, NULL);
       mc_client_set_shuffle_mode_update_cb(s_player_data.player_client, __shuffle_mode_updated_cb, NULL);
       mc_client_set_repeat_mode_update_cb(s_player_data.player_client, __repeat_mode_updated_cb, NULL);    
    
       return;
    } 
    
  2. Initialize the media information with the latest server information in the __create_player_client() function. The function can also get the latest media information using the mc_client_get_latest_server_info() and mc_client_get_server_xxx() functions. The server name is necessary to use the mc_client_get_server_xxx() functions. To get the server name, use the mc_client_foreach_server() or mc_client_get_latest_server_info() function.
    static void 
    __create_player_client(void)
    {
       int ret = mc_client_create(&s_player_data.player_client);
    
       ret = mc_client_foreach_server(s_player_data.player_client, __server_list_cb, NULL);
    
       ret = mc_client_get_latest_server_info(s_player_data.player_client, &server_name, &server_state);
    
       if (ret == MEDIA_CONTROLLER_ERROR_NONE
           && server_name
           && !s_player_data.connected) 
       {
          s_player_data.app_name = strdup(server_name);
    
          mc_client_get_server_playback_info(s_player_data.player_client, s_player_data.app_name, &s_player_data.playback_info);
          mc_client_get_server_metadata(s_player_data.player_client, s_player_data.app_name, &s_player_data.metadata);
          mc_client_get_server_shuffle_mode(s_player_data.player_client, s_player_data.app_name, &shuffle_mode);
          mc_client_get_server_repeat_mode(s_player_data.player_client, s_player_data.app_name, &repeat_mode);
       } 
       else 
       {
          dlog_print(DLOG_DEBUG, LOG_TAG, "Didn't get information about latest server");
       }
    
       return;
    } 
    
  3. Send the playback state command to the media controller server application with the mc_client_send_playback_state_command() function. This function can change the playback state of the media controller server.
    static void 
    __play_clicked_cb(void *data, Evas_Object *obj, void *event_info)
    {
       mc_client_send_playback_state_command(s_player_data.player_client, s_player_data.app_name, MC_PLAYBACK_STATE_PLAYING);
       dlog_print(DLOG_DEBUG, LOG_TAG, "Send Play command to player app [%s]", s_player_data.app_name);
    }
    
    static void 
    __prev_clicked_cb(void *data, Evas_Object *obj, void *event_info)
    {
       mc_client_send_playback_state_command(s_player_data.player_client, s_player_data.app_name, MC_PLAYBACK_STATE_PREV_FILE);
       dlog_print(DLOG_DEBUG, LOG_TAG, "Send Previous track command to player app [%s]", s_player_data.app_name);
    }
    
    static void 
    __next_clicked_cb(void *data, Evas_Object *obj, void *event_info)
    {
       mc_client_send_playback_state_command(s_player_data.player_client, s_player_data.app_name, MC_PLAYBACK_STATE_NEXT_FILE);
       dlog_print(DLOG_DEBUG, LOG_TAG, "Send Next track command to player app [%s]", s_player_data.app_name);
    }
    
  4. Unset the callbacks with the mc_client_unset_xxx_update_cb() functions. The media controller client handle must be destroyed using the mc_client_destroy() function.
    static void 
    __destroy_player_client(void)
    {
       mc_client_unset_server_update_cb(s_player_data.player_client);
       mc_client_unset_playback_update_cb(s_player_data.player_client);
       mc_client_unset_metadata_update_cb(s_player_data.player_client);
       mc_client_unset_shuffle_mode_update_cb(s_player_data.player_client);
       mc_client_unset_repeat_mode_update_cb(s_player_data.player_client);
    
       mc_client_destroy(s_player_data.player_client);
       mc_client_destroy_playback(s_player_data.playback_info);
       mc_client_destroy_metadata(s_player_data.metadata);
    
       if (s_player_data.app_name) 
       {
          free(s_player_data.app_name);
       }
    
       return;
    }