Mobile native Wearable native

Media key: Managing Callbacks from Media Key Events

This tutorial demonstrates how you can manage callbacks for media key events.

Warm-up

Become familiar with the Media key API basics by learning about:

Monitoring Media Key Events

To monitor media key events:

  1. To use the media key-related features of the Media key API (in mobile and wearable applications), include the <media_key.h> header file in your application:

    #include <media_key.h>
    
  2. Implement the callback to handle media key events:
    void event_cb(media_key_e key, media_key_event_e status, void* user_data)
    {
       switch (key) 
       {
          case MEDIA_KEY_PLAY:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_PLAY");
             break;
          case MEDIA_KEY_STOP:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_STOP");
             break;
          case MEDIA_KEY_PAUSE:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_PAUSE");
             break;
          case MEDIA_KEY_PREVIOUS:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_PREVIOUS");
             break;
          case MEDIA_KEY_NEXT:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_NEXT");
             break;
          case MEDIA_KEY_FASTFORWARD:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_FASTFORWARD");
             break;
          case MEDIA_KEY_REWIND:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_REWIND");
             break;
          case MEDIA_KEY_PLAYPAUSE:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_PLAYPAUSE");
             break;
          case MEDIA_KEY_UNKNOWN:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : MEDIA_KEY_UNKNOWN");
             break;
          default:
             dlog_print(DLOG_INFO, LOG_TAG, "Key : Unknown");
             break;
       }
       switch (status) 
       {
          case MEDIA_KEY_STATUS_PRESSED:
             dlog_print(DLOG_INFO, LOG_TAG, "Event : MEDIA_KEY_STATUS_PRESSED");
             break;
          case MEDIA_KEY_STATUS_RELEASED:
             dlog_print(DLOG_INFO, LOG_TAG, "Event : MEDIA_KEY_STATUS_RELEASED");
             break;
          case MEDIA_KEY_STATUS_UNKNOWN:
             dlog_print(DLOG_INFO, LOG_TAG, "Event : MEDIA_KEY_STATUS_UNKNOWN");
             break;
          default:
             dlog_print(DLOG_INFO, LOG_TAG, "Event : Unknown");
             break;
       }
    }
    
  3. Register the defined callback with the media_key_reserve() function:
    void media_key_api_func()
    {
       int r = 0;
    
       r = media_key_reserve(event_cb, NULL);
    
       if (r == MEDIA_KEY_ERROR_NONE)
       {
          dlog_print(DLOG_INFO, LOG_TAG, "Reserve media key success.");
       }
       else
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "Reserve media key fail.");
       }
    
  4. When you no longer need to track media key events, unregister the callback with the media_key_release() function:

       r = media_key_release(event_cb, NULL);
    
       if (r == MEDIA_KEY_ERROR_NONE)
       {
          dlog_print(DLOG_INFO, LOG_TAG, "Release media key success.");
       }
       else if (r == MEDIA_KEY_ERROR_INVALID_PARAMETER)
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "Invalid parameter.");
       }
       else if (r == MEDIA_KEY_ERROR_OPERATION_FAILED)
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "Operation failed.");
       }
       else
       {
          dlog_print(DLOG_ERROR, LOG_TAG, "Fail. Unknown reason.");
       }
    }
    
Go to top