简体   繁体   中英

Streaming multiple music with AVPlayer

I have an app that I want to make which requires streaming audio files from web server. I use AVPlayer as the player. The problem is, some responses that I am receiving from the server has two audio files on it. And this makes the streaming hard. My audio player UI by the way is like this:

在此处输入图片说明

I have a slider for the streamed time ranges (the black one) and another slider for the AVPlayer.currentTime . I have two audio music streamed and their music durations are added together which is now 8:46. My first music has 6 minutes duration and my second music has 1:46. As you can see in the above photo, my streamed time ranges slider indicates that AVAsset has completely streamed the first music. My problem is, I can't continue streaming and playing the next music when the first one has reached it's end. It just stop and the slider value gets back to 0.

What I want to accomplish is that when the first item has reached its end, AVPlayer would load another player item and that would be the second music. Will continue to play and slider will continue to move.

Is this possible? What are your suggestions? Thanks experts.

To load audio files one after the other, you can use AVQueuePlayer.

 NSURL *song1 = [NSURL URLWithString:@"audio url1"]; 
 NSURL *song2 = [NSURL URLWithString:@"audio url2"];

 AVPlayerItem *playerItem1 = [[AVPlayerItem alloc] initWithURL:song1];
 AVPlayerItem *playerItem2 = [[AVPlayerItem alloc] initWithURL:song2];

 NSArray *songs = @[playerItem1, playerItem2];

 self.queuePlayer = [[AVQueuePlayer alloc] initWithItems:songs];
 [self.queuePlayer play];

Hope this might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM