Media-controller server Sample Overview

Mobile native

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

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

Figure: Media-controller server main view

Media-controller-server main view

Prerequisites

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

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

Implementation

To use the media controller server:

  1. Create a media controller server handle in the create_player() function. The function sets the callbacks to receive commands from the media controller client. To handle the incoming playback state command, define a callback and register it using the mc_server_set_playback_state_command_received_cb() function.
    static void 
    create_player(appdata_s *ad)
    {
       ret = mc_server_create(&ad->player_controller);
       if (ret != MEDIA_CONTROLLER_ERROR_NONE) 
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "mc_server_create() failed. err = %d", ret);
    
          return;
       }
       mc_server_set_playback_state_command_received_cb(ad->player_controller, _playback_state_cb, (void*)ad);
       mc_server_set_custom_command_received_cb(ad->player_controller, __custom_command_received_cb, (void*)ad);
    } 
    
  2. Update the playback state, media metadata information, shuffle mode, and repeat mode in the play_media() function:
    void
    play_media(media_info_h media_info, appdata_s *ad)
    {
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_TITLE, file_name);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_ARTIST, artist);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_ALBUM, album);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_AUTHOR, author);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_GENRE, genre);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_DURATION, duration_c);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_DATE, date);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_COPYRIGHT, copyright);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_DESCRIPTION, description);
       mc_server_set_metadata(ad->player_controller, MC_META_MEDIA_TRACK_NUM, track_num);     
       mc_server_update_metadata(ad->player_controller);
    
       // Update shuffle mode information for clients
       mc_server_update_shuffle_mode(ad->player_controller, MC_SHUFFLE_MODE_OFF);
    
       // Update repeat mode information for clients
       mc_server_update_repeat_mode(ad->player_controller, MC_REPEAT_MODE_ON);
    
       switch (player_state) 
       {
          // Case when the player state is playing
             mc_server_set_playback_state(ad->player_controller, MC_PLAYBACK_STATE_PLAYING);
             mc_server_update_playback_info(ad->player_controller);
    
          // Case when the player state is paused
             mc_server_set_playback_state(ad->player_controller, MC_PLAYBACK_STATE_PAUSED);
             mc_server_update_playback_info(ad->player_controller);
       }
    } 
    
  3. Handle the custom command in the mc_server_set_custom_command_received_cb() function and send a reply to the media controller client application with the mc_server_send_command_reply() function:
    void 
    __custom_command_received_cb(const char *client_name, const char *command, bundle *data, void *user_data)
    {
       ret = mc_server_send_command_reply(ad->player_controller, client_name, 0, bundle_reply);
       if (ret != MEDIA_CONTROLLER_ERROR_NONE)
          dlog_print(DLOG_ERROR, LOG_TAG, "mc_server_send_command_reply() failed. err = %d", ret);
    
       return;
    }
    
  4. Unset the callbacks with the mc_client_unset_xxx_command_received_cb() functions. The media controller server handle must be destroyed using the mc_server_destroy() function.
    static void 
    __destroy_player(appdata_s *ad)
    {
       ret = mc_server_unset_playback_state_command_received_cb(ad->player_controller);
       if (ret != MEDIA_CONTROLLER_ERROR_NONE) 
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "mc_server_unset_playback_state_command_received_cb() failed. err = %d", ret);
       }
    
       ret = mc_server_unset_custom_command_received_cb(ad->player_controller);
       if (ret != MEDIA_CONTROLLER_ERROR_NONE) 
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "mc_server_unset_playback_state_command_received_cb() failed. err = %d", ret);
       }
    
       ret = mc_server_destroy(ad->player_controller);
       if (ret != MEDIA_CONTROLLER_ERROR_NONE) 
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "mc_server_destroy() failed. err = %d", ret);
       }
    }