Media Key Events
PUBLISHED
Tizen enables you to manage media key events. The media key events are generated by the remote control buttons in a Bluetooth headset or Bluetooth earphone. The media_key_e
enumerator (in mobile and wearable applications) defines the available key types.
The main features of the Media key API include:
- Registering a callback
You can register a callback for media key events using the
media_key_reserve()
function. The first parameter of the function defines themedia_key_event_cb
type callback, which you can implement to perform any actions you need. - Deregistering a callback
You can deregister a callback for media key events using the
media_key_release()
function.
Prerequisites
To use the functions and data types of the Media key API (in mobile and wearable applications), include the <media_key.h>
header file in your application:
#include <media_key.h>
Monitoring Media Key Events
To monitor media key events:
- Implement a callback to handle media key events.
In the following example, the callback simply prints out the key from which the event originated and the state change that caused the event:
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; } }
- 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 failed.");
-
When you no longer need to monitor the media key events, deregister the callback with the
media_key_release()
function:r = media_key_release(); 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, "Failed. Unknown reason."); }