// // background audio (using mmrenderer) // void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { if (!s_isBackgroundInitialized) { const char *mmrname = NULL; const char *ctxtname = "mmrplayer"; char cwd[PATH_MAX]; mode_t mode = S_IRUSR | S_IXUSR; getcwd(cwd, PATH_MAX); string path = "file://"; path += cwd; path += "/"; path += pszFilePath; s_mmrConnection = mmr_connect(mmrname); if (!s_mmrConnection) { perror("mmr_connect"); s_hasMMRError = true; return; } s_mmrContext = mmr_context_create(s_mmrConnection, ctxtname, 0, mode); if (!s_mmrContext) { perror(ctxtname); s_hasMMRError = true; return; } if ((s_audioOid = mmr_output_attach(s_mmrContext, "audio:default", "audio")) < 0) { mmrerror(s_mmrContext, "audio:default"); return; } if (mmr_input_attach(s_mmrContext, path.data(), "autolist") < 0) { fprintf(stderr, "unable to load %s\n", path.data()); mmrerror(s_mmrContext, path.data()); return; } s_currentBackgroundStr = pszFilePath; s_isBackgroundInitialized = true; setBackgroundVolume(s_volume); } }
void SimpleAudioEngine::rewindBackgroundMusic() { if (s_mmrContext && mmr_seek(s_mmrContext, "1:0") < 0) { mmrerror(s_mmrContext, "rewind"); } }
void SimpleAudioEngine::resumeBackgroundMusic() { if (s_mmrContext && mmr_speed_set(s_mmrContext, 1000) < 0) { mmrerror(s_mmrContext, "resume"); } s_playStatus = PLAYING; }
void SimpleAudioEngine::pauseBackgroundMusic() { if (s_mmrContext && mmr_speed_set(s_mmrContext, 0) < 0) { mmrerror(s_mmrContext, "pause"); } s_playStatus = PAUSED; }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (0 != strcmp(s_currentBackgroundStr.c_str(), pszFilePath)) { stopBackgroundMusic(true); } else { if (s_playStatus == PAUSED) resumeBackgroundMusic(); else rewindBackgroundMusic(); } if (!s_isBackgroundInitialized) preloadBackgroundMusic(pszFilePath); if (bLoop) { // set it up to loop strm_dict_t *dictionary = strm_dict_new(); s_repeatDictionary = strm_dict_set(dictionary, "repeat", "all"); if (mmr_input_parameters(s_mmrContext, s_repeatDictionary) != 0) { mmrerror(s_mmrContext, "input parameters (loop)"); return; } } if (s_hasMMRError || !s_mmrContext) return; if (mmr_play(s_mmrContext) < 0) { mmrerror(s_mmrContext, "mmr_play"); s_hasMMRError = true; } if (!s_hasMMRError) s_playStatus = PLAYING; }
int ZaMp3::playloop(const char * url) { const char *mmrname = NULL; const char *ctxtname = "testplayer"; const char *audioout = NULL; const char *inputtype = "autolist"; //int final_return_code = EXIT_FAILURE; mode_t mode = S_IRUSR | S_IXUSR; int audio_oid; // output ID strm_dict_t *aoparams = NULL; // output parameters mmr_connection_t *connection; mmr_context_t *ctxt; audioout = DEFAULT_AUDIO_OUT; strm_dict_t *aiparams = strm_dict_new(); aiparams = strm_dict_set(aiparams, "repeat", "all"); getcwd(cwd, PATH_MAX); rc = snprintf(inputurl, PATH_MAX, "file://%s%s", cwd, url); if ( ( connection = mmr_connect( mmrname ) ) == NULL ) { perror( "mmr_connect" ); } else if ( ( ctxt = mmr_context_create( connection, ctxtname, 0, mode ) ) == NULL ) { perror( ctxtname ); } else if ( audioout && ( audio_oid = mmr_output_attach( ctxt, audioout, "audio" ) ) < 0 ) { mmrerror( ctxt, audioout ); } else if ( aoparams && mmr_output_parameters( ctxt, audio_oid, aoparams ) ) { mmrerror( ctxt, "output parameters (audio)" ); } else if ( mmr_input_attach( ctxt, inputurl, inputtype ) < 0 ) { mmrerror( ctxt, inputurl ); } else if ( aiparams && mmr_input_parameters( ctxt, aiparams ) ) { // NEW mmrerror( ctxt, "input parameters (audio)" ); // NEW } else if ( mmr_play( ctxt ) < 0 ) { mmrerror( ctxt, "mmr_play" ); } return 0; }
int ZaMp3::play(const char * url) { const char *mmrname = NULL; const char *audioout = NULL; const char *inputtype = "track"; //int final_return_code = EXIT_FAILURE; mode_t mode = S_IRUSR | S_IXUSR; int audio_oid; // output ID strm_dict_t *aoparams = NULL; // output parameters if (!ctxt) { audioout = DEFAULT_AUDIO_OUT; getcwd(cwd, PATH_MAX); rc = snprintf(inputurl, PATH_MAX, "file://%s%s", cwd, url); if (( connection = mmr_connect( mmrname ) ) == NULL ) perror( "mmr_connect" ); else if ( ( ctxt = mmr_context_create( connection, ctxtname, 0, mode ) ) == NULL ) { perror( "mmr_create" ); perror( ctxtname ); } else if ( audioout && ( audio_oid = mmr_output_attach( ctxt, audioout, "audio" ) ) < 0 ) { mmrerror( ctxt, audioout ); } else if ( aoparams && mmr_output_parameters( ctxt, audio_oid, aoparams ) ) { mmrerror( ctxt, "output parameters (audio)" ); } else if ( mmr_input_attach( ctxt, inputurl, inputtype ) < 0 ) { mmrerror( ctxt, inputurl ); } else if ( mmr_play( ctxt ) < 0 ) { mmrerror( ctxt, "mmr_play" ); } } else { mmr_stop(ctxt); //mmr_input_attach( ctxt, inputurl, inputtype ); mmr_play( ctxt ); } return 0; }
static void setBackgroundVolume(float volume) { if (!s_isBackgroundInitialized) { return; } char volume_str[128]; // set it up the background volume strm_dict_t *dictionary = strm_dict_new(); sprintf(volume_str, "%d", (int)(volume * 100) ); s_volumeDictionary = strm_dict_set(dictionary, "volume", volume_str); if (mmr_output_parameters(s_mmrContext, s_audioOid, s_volumeDictionary) != 0) { mmrerror(s_mmrContext, "output parameters"); return; } }
int main( int argc, char **argv ) { const char *mmrname = NULL; const char *ctxtname = "testplayer"; const char *audioout = NULL; const char *inputtype = "track"; char cwd[PATH_MAX]; char inputurl[PATH_MAX]; int rc; int final_return_code = EXIT_FAILURE; int exit_application = 0; mmr_connection_t *connection = NULL; mmr_context_t *ctxt = NULL; /* * Before we can listen for events from the BlackBerry(R) 10 OS platform * services, we need to initialize the BPS infrastructure */ bps_initialize(); if (setup_screen() != EXIT_SUCCESS) { printf("Unable to set up the screen. Exiting."); return 0; } /* * Once the BPS infrastructure has been initialized we can register for * events from the various BlackBerry(R) 10 OS platform services. The * Navigator service manages and delivers application life cycle and * visibility events. * For this sample, we request Navigator events so we can track when * the system is terminating the application (NAVIGATOR_EXIT event). * This allows us to clean up application resources. */ navigator_request_events(0); dialog_request_events(0); /* * Create and display the dialog. */ create_dialog(); getcwd(cwd, PATH_MAX); rc = snprintf(inputurl, PATH_MAX, "file://%s%s", cwd, WAV_RELATIVE_PATH); if (rc > PATH_MAX - 1) { show_dialog_message("File name and path too long"); goto fail; } mode_t mode = S_IRUSR | S_IXUSR; int audio_oid; // output ID strm_dict_t *aoparams = NULL; // output parameters audioout = DEFAULT_AUDIO_OUT; if ( ( connection = mmr_connect( mmrname ) ) == NULL ) { snprintf(msg, MSG_SIZE, "mmr_connect: %s", strerror(errno)); show_dialog_message(msg); } else if ( ( ctxt = mmr_context_create( connection, ctxtname, 0, mode ) ) == NULL ) { snprintf(msg, MSG_SIZE, "%s: %s", ctxtname, strerror(errno)); show_dialog_message(msg); } else if ( audioout && ( audio_oid = mmr_output_attach( ctxt, audioout, "audio" ) ) < 0 ) { mmrerror( ctxt, audioout ); } else if ( aoparams && mmr_output_parameters( ctxt, audio_oid, aoparams ) ) { mmrerror( ctxt, "output parameters (audio)" ); } else if ( mmr_input_attach( ctxt, inputurl, inputtype ) < 0 ) { mmrerror( ctxt, inputurl ); } else if ( mmr_play( ctxt ) < 0 ) { mmrerror( ctxt, "mmr_play" ); } else if (BPS_SUCCESS != bps_add_sigevent_handler( &mmr_sigevent, mmr_sigevent_handler, ctxt ) ) { snprintf( msg, MSG_SIZE, "bps_add_sigevent_handler() failure %s", strerror( errno ) ); show_dialog_message( msg ); } else if ( drain_and_arm_mmr_events ( ctxt ) ) { snprintf( msg, MSG_SIZE, "drain_and_arm_mmr_events() failure %s", strerror( errno ) ); show_dialog_message( msg ); } else { show_dialog_message( "Playing Audio\n" ); final_return_code = EXIT_SUCCESS; } fail: /* * Process Navigator events until we receive a NAVIGATOR_EXIT event. */ while (!exit_application) { /* * Using a negative timeout (-1) in the call to bps_get_event(...) * ensures that we don't busy wait by blocking until an event is * available. */ bps_event_t *event = NULL; bps_get_event(&event, -1); if (event) { /* * If it is a NAVIGATOR_EXIT event then set the exit_application * flag so the application will stop processing events, clean up * and exit */ if (bps_event_get_domain(event) == navigator_get_domain()) { if (NAVIGATOR_EXIT == bps_event_get_code(event)) { if (final_return_code == EXIT_SUCCESS) { mmr_stop( ctxt ); // Not really necessary -- mmr_input_detach() would take care of this mmr_input_detach( ctxt ); // Not really necessary -- mmr_context_destroy() would take care of this mmr_context_destroy( ctxt ); // Not really necessary -- mmr_disconnect() would take care of this mmr_disconnect( connection ); // Not really necessary -- exiting would take care of this } exit_application = 1; } } else if (bps_event_get_domain(event) == dialog_get_domain()) { if (DIALOG_RESPONSE == bps_event_get_code(event)) { handle_dialog_response_events(event); } } } } /* * Destroy the dialog, if it exists and cleanup screen resources. */ destroy_dialog(); cleanup_screen(); /* * Clean up the BPS infrastructure and exit */ bps_shutdown(); return final_return_code; }