How to play a particular portion of an audio file in Cocos2d

77 Views Asked by At

I am playing an audio file named 'myTrack.mp3' with SimpleAudioEngine as:

[[SimpleAudioEngine sharedEngine]playBackgroundMusic:@"myTrack.mp3" loop:NO];

The audio if of the length of 00:17 seconds, and my plays the whole file. But now I need to play only the portion between time 00:08 and 00:14. Is it is possible in Cocos2d?

Thanks.

1

There are 1 best solutions below

3
kabarga On

I am not familiar with SimpleAudioEngine but with the standard AVQueuePlayer you can do:

1) seekToTime. e.g. ([_player seekToTime:CMTimeMakeWithSeconds(8.0, 1.0)])

2) then add time observer to the player:

 _timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0, 1.0)
                                                  queue:NULL
                                             usingBlock:^(CMTime time) {
                                                 check current duration and stop the playback. 
                                             }];