Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play multiple audio files in sequence as one

Tags:

ios

iphone

audio

I'm trying to implement an Audio Player to play multiple files as if they were a single one. However, the initial buffer time should be only the first part's duration, and the other files should be loaded in sequence.

For example:

  • File1:
    • Part1 - 0:35s
    • Part2 - 0:47s
    • Part3 - 0:07s

The File1 should be played as if it had 1:29, but we'd only wait (at most) until Part1 is loaded to start playing.

I've had a look at AVAsset, but it doesn't seem to solve the problem. I also thought of implementing it using AVAudioPlayer and doing all the logic myself.

Has anyone had this issue before?

Thanks!

like image 921
lucoceano Avatar asked Oct 20 '22 17:10

lucoceano


1 Answers

You can use AVQueuePlayer for achieving this. You need to create AVPlayerItem using your files url and then need to initialize the AVQueuePlayer using it's queuePlayerWithItems: method.

AVPlayerItem *firstItem  = [[AVPlayerItem alloc] initWithURL:firstPartURL];
AVPlayerItem *secondItem = [[AVPlayerItem alloc] initWithURL:secondPartURL];
AVPlayerItem *thirdItem  = [[AVPlayerItem alloc] initWithURL:thirdPartURL];    

NSArray *itemsToPlay     = [NSArray arrayWithObjects:firstItem, secondItem, thirdItem, nil];
AVQueuePlayer *qPlayer   = [AVQueuePlayer queuePlayerWithItems:itemsToPlay];

[qPlayer play];
like image 174
Midhun MP Avatar answered Nov 02 '22 23:11

Midhun MP