Playing audio pieces

Hello guys I have a small question, I was trying to figure out how to play a certain fragment of a music, into my game, for example, I have one song with 3 minutes, and I need to play the fragment between 1:50 - 1:63, is this possible into cocos? Thanks!!!

Using SimpleAudioEngine I don’t believe that it is.

Try cricket audio engine.

Here’s cricket’s website in case you want to experiment with that option as well:
http://www.crickettechnology.com/


Unfortunately seeking and play management is one area where the API is somewhat limited. This is partially due to some aspects of the engine require polling for the audio source state to make sure a behavior will apply.

Here’s a naive test added to cpp-test that seems to work and I’ll probably wrap into a test and submit as PR for discussion of improving and enhancing the API now that preload has been implemented.

AudioEngine::stopAll();

// todo: make api to set time with timeout (don't poll state forever)
// todo: integrate into implementation to pool and mix
auto audioFile = "audio/LuckyDay.mp3";
AudioEngine::preload(audioFile, [this,audioFile](bool isSuccess){
    if(! isSuccess) { CCLOG("failed to preload!"); return; }

    auto audioId = AudioEngine::play2d(audioFile);
    if(audioId != AudioEngine::INVALID_AUDIO_ID) {

        auto start = 30.f; // seconds
        auto end = 45.f; // seconds

        this->schedule([=](float) {

            // gotta wait 'til playing :(
            auto state = AudioEngine::getState(audioId);
            if(state == AudioEngine::AudioState::PLAYING)
            {
                AudioEngine::setCurrentTime(audioId, start);

                // create a callback or track time in update method
                // (ideally pool this with a mixer)
                auto duration = end - start;

                this->scheduleOnce([=](float) {
                    AudioEngine::stop(audioId);
                }, duration, "stupid_unique_key_required_1");

                this->scheduleOnce([=](float) {
                    AudioEngine::setCurrentTime(audioId, start);
                }, 0, "stupid_unique_key_required_2");

                this->schedule([=](float) {
                    auto timestamp = AudioEngine::getCurrentTime(audioId);
                    CCLOG("aid: %d, timestamp: %f", audioId, timestamp);
                }, .2f, "stupid_unique_update_key_2");

                // remove self
                this->unschedule("stupid_unique_update_key_1");
            }
            else
            {
                CCLOGWARN("STATE still not PLAYING!");
            }
        }, "stupid_unique_update_key_1");
    }
    else
    {
        // should handle gracefully, but alert in debug to start
        CCASSERT(false, "err failed to play");
    }
});

Thanks for your help friends, I’ll try using this =D

Thanks alot! It works.

I use @stevetranby code, but I have a problem. Audio play a bit from beginning, then play pieces 1:20~2:30. The audio thread may already start to play track before scheduler calls the update function. It look like a bug (AudioEngine bug - setCurrentTime error). I don’t have a good idea. Please help me. Thanks!