def load(self, content, activity_operation_cb=None, operation_user_data=None): """Load the given content or the current track. In the first case, set the current_track to the given track. :param content: The track/tracklist to load :param activity_operation_cb: A callback triggered after operation. See module docstring. :param operation_user_data: Any object your operation_callback can manipulate. Must inherit from Structure class. :type content: str :type activity_operation_cb: dz_activity_operation_cb_func :type operation_user_data: Same as operation_user_data in your callback. Must inherit from Structure as it is used by ctypes """ if content: self.current_content = content context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_load( self.handle, cb, context, c_char_p(self.current_content.encode('utf-8'))): raise PlayerRequestFailedError("load: Unable to load selected \ track. Check connection and \ tracklist data.")
def shutdown(self, activity_operation_cb=None, operation_user_data=None): """Deactivate connection associated to the handle.""" context = py_object(operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if self.handle: libdeezer.dz_connect_deactivate(self.handle, cb, context) self.active = False
def play(self, activity_operation_cb=None, operation_user_data=None, command=PlayerCommand.START_TRACKLIST, index=PlayerIndex.CURRENT): """Play the current track if loaded. The player gets data and renders it. :param command: Player command :param index: Index of the track to play :param activity_operation_cb: Called when async result is available :param operation_user_data: A reference to user's data :type command: PlayerCommand :type index: int :type activity_operation_cb: dz_activity_operation_cb_func :type operation_user_data: Same as operation_user_data in your callback. Must inherit from structure as it is used by ctypes. """ context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_play(self.handle, cb, context, command, index) not in range(0, 2): raise PlayerRequestFailedError("play: Unable to play selected \ track. Check player commands and \ info.") self.is_playing = True
def shutdown(self, activity_operation_cb=None, operation_user_data=None): """Deactivate the player""" context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if self.handle: libdeezer.dz_player_deactivate(self.handle, cb, context)
def resume(self, activity_operation_cb=None, operation_user_data=None): """Resume the track if it has been paused""" context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_resume(self.handle, cb, context): raise PlayerRequestFailedError("play: Unable to resume track. \ Check player commands and info.") self.is_playing = True
def stop(self, activity_operation_cb=None, operation_user_data=None): """Stop the currently playing track""" context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_stop(self.handle, cb, context): raise PlayerRequestFailedError("play: Unable to stop track. Check \ player commands and info.") self.is_playing = False
def play_audio_ads(self, activity_operation_cb=None, operation_user_data=None): """Load and play an audio ad when required""" context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_play_audioads(self.handle, cb, context): raise PlayerRequestFailedError("play: Unable to play audio ads. \ Check connection.")
def set_output_volume(self, volume, activity_operation_cb=None, operation_user_data=None): """Set the output volume of the player. :param volume: Set to the percentage of the output volume :type volume: int """ context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_set_output_volume(self.handle, cb, context, volume): raise PlayerRequestFailedError("play: Unable to set volume. Check \ player commands and info.")
def set_repeat_mode(self, repeat_mode, activity_operation_cb=None, operation_user_data=None): """Set the repeat mode of the player. :param repeat_mode: The repeat mode to set :type repeat_mode: PlayerRepeatMode """ context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_set_repeat_mode(self.handle, cb, context, repeat_mode): raise PlayerRequestFailedError("play: Unable to set repeat mode. \ Check player commands and info.")
def enable_shuffle_mode(self, shuffle_mode, activity_operation_cb=None, operation_user_data=None): """Set the shuffle mode of the player (randomize track selection) :param shuffle_mode: Set to true to activate the random track selection :type shuffle_mode: bool """ context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_enable_shuffle_mode(self.handle, cb, context, shuffle_mode): raise PlayerRequestFailedError("play: Unable to set repeat mode. \ Check player commands and info.")
def set_output_mute(self, is_mute, activity_operation_cb=None, operation_user_data=None): """Set the mute mode of the player :param is_mute: Set to true to mute the output volume :type is_mute: bool """ context = py_object( operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_player_set_output_mute(self.handle, cb, context, is_mute): raise PlayerRequestFailedError("play: Unable to (un)mute the \ output. Check player commands \ and info.")
def set_cache_max_size(self, cache_max_size, activity_operation_cb=None, operation_user_data=None): """Set cache size limitation. :param activity_operation_cb: The callback of the operation. :param operation_user_data: An object you want to pass to activity_operation_cb. :param cache_max_size: The maixum size of the cache in kilobyte :type activity_operation_cb: dz_activity_operation_cb_func :type operation_user_data: The type of the object you want to manipulate :type cache_max_size: int """ context = py_object(operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_connect_smartcache_quota_set(self.handle, cb, context, c_int(cache_max_size)): raise ConnectionRequestFailedError( 'connect_cache_max_size: Request failed. Check connection and \ callbacks if used.')
def _activate(self, supervisor=None): """Activate the player. :param supervisor: An object that can be manipulated by your dz_player_on_event_cb to store info. :type supervisor: Same as userdata in dz_player_on_event_cb """ context = py_object(supervisor) if supervisor else c_void_p(0) if libdeezer.dz_player_activate(self.handle, context): raise PlayerActivationError("Player activation failed. Check \ player info and your network \ connection.") self.active = True
def cache_path_set(self, user_cache_path, activity_operation_cb=None, operation_userdata=None): """Set the cache path for debug purposes and logs. Cache will be stored in the specified path. Calls activity_operation_cb after the operation. :param user_cache_path: The desired path :param activity_operation_cb: The callback to this function. :param operation_userdata: An object you want to pass to activity_operation_cb. :type user_cache_path: str :type activity_operation_cb: dz_activity_operation_cb_func :type operation_userdata: The type of the object you want to manipulate """ context = py_object(operation_userdata) if operation_userdata else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_connect_cache_path_set(self.handle, cb, context, c_char_p(user_cache_path.encode('utf-8'))): raise ConnectionRequestFailedError( 'cache_path_set: Request failed. Check connection \ and/or path validity.')
def set_access_token(self, user_access_token, activity_operation_cb=None, operation_user_data=None): """ Set the user access token given by OAuth process. Mandatory to allow connection. :param user_access_token: The token given by OAuth 2 process. Refer to the API documentation. :param activity_operation_cb: The callback to this function. :param operation_user_data: An object you want to pass to activity_operation_cb. :type user_access_token: unicode :type activity_operation_cb: dz_activity_operation_cb_func :type operation_user_data: The type of the object you want to manipulate """ context = py_object(operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_connect_set_access_token(self.handle, cb, context, c_char_p(user_access_token.encode('utf-8'))): raise ConnectionRequestFailedError('set_access_token: \ Request failed. Check access \ token or update it.')
def set_offline_mode(self, offline_mode_forced, activity_operation_cb=None, operation_user_data=None): """Force offline mode in lib. Calling this function is mandatory to force user login. :param activity_operation_cb: The callback of the operation. :param operation_user_data: An object you want to pass to activity_operation_cb. :param offline_mode_forced: Force offline mode. Leave to False if just to allow connection. :type activity_operation_cb: dz_activity_operation_cb_func :type operation_user_data: The type of the object you want to manipulate :type offline_mode_forced: bool """ context = py_object(operation_user_data) if operation_user_data else c_void_p(0) cb = activity_operation_cb if activity_operation_cb else c_void_p(0) if libdeezer.dz_connect_offline_mode(self.handle, cb, context, c_bool(offline_mode_forced)): raise ConnectionRequestFailedError( 'connect_offline_mode: Request failed. Check connection and \ callbacks if used.')
def _activate(self, user_data=None): """Launch the connection. Call this after init_handle. Calls self.dz_connect_on_event_cb after activation. You can provide any object you want through user_data as long as it is managed by this callback. :param user_data: An object you want to pass to dz_connect_on_event_cb. :type user_data: The type of the object you want to manipulate """ context = py_object(user_data) if user_data else c_void_p(0) if libdeezer.dz_connect_activate(self.handle, context): raise ConnectionActivationError('Failed to activate connection. \ Check your network connection.') self.active = True
def __init__(self, context, app_id, product_id, product_build_id, user_profile_path, dz_connect_on_event_cb=None, anonymous_blob=None, dz_connect_crash_reporting_delegate=None): """ :param app_id: The ID of the application :param product_id: The name of your application :param product_build_id: The version number :param user_profile_path: The cache path of the user. Deprecated. :param dz_connect_on_event_cb: The event listener to connection operations :param anonymous_blob: Deprecated :param dz_connect_crash_reporting_delegate: The error callback :type app_id: unicode :type product_id: unicode :type product_build_id: unicode :type user_profile_path: unicode :type dz_connect_on_event_cb: function :type dz_connect_crash_reporting_delegate: function """ self.context = context self.app_id = app_id self.product_id = product_id self.product_build_id = product_build_id self.user_profile_path = user_profile_path self.dz_connect_on_event_cb = \ dz_on_event_cb_func(dz_connect_on_event_cb) self.anonymous_blob = anonymous_blob self.dz_connect_crash_reporting_delegate = \ dz_connect_crash_reporting_delegate_func(dz_connect_crash_reporting_delegate) self.handle = 0 self.active = False self.set_event_cb(dz_connect_on_event_cb) config = DZConnectConfiguration( c_char_p(self.app_id.encode('utf-8')), c_char_p(self.product_id.encode('utf-8')), c_char_p(self.product_build_id.encode('utf-8')), c_char_p(self.user_profile_path.encode('utf-8')), self.dz_connect_on_event_cb, c_void_p(self.anonymous_blob), self.dz_connect_crash_reporting_delegate) self.handle = libdeezer.dz_connect_new(byref(config)) if not self.handle: raise ConnectionInitFailedError('Connection handle failed initialize. Check connection info you gave.') self._activate(context)
def get_event(event_obj): return libdeezer.dz_player_event_get_type(c_void_p(event_obj))
def event_track_selected_next_track_dzapiinfo(event): return libdeezer.dz_player_event_track_selected_next_track_dzapiinfo( c_void_p(event))
def event_track_selected_rights(event, can_pause_unpause, can_seek, nb_skip_allowed): libdeezer.dz_player_event_track_selected_rights( c_void_p(event), byref(can_pause_unpause), byref(can_seek), byref(nb_skip_allowed))
def is_selected_track_preview(event_handle): return libdeezer.dz_player_event_track_selected_is_preview( c_void_p(event_handle))
def get_queuelist_context(event, streaming_mode, idx): return libdeezer.dz_player_event_get_queuelist_context( c_void_p(event), byref(streaming_mode), byref(idx))
def get_event(event_obj): """Get the event value from the event_obj given by the SDK.""" return int(libdeezer.dz_player_event_get_type(c_void_p(event_obj)))