简体   繁体   English

使用AVPlayer播放多种音乐

[英]Streaming multiple music with AVPlayer

I have an app that I want to make which requires streaming audio files from web server. 我有一个要制作的应用程序,需要从Web服务器流式传输音频文件。 I use AVPlayer as the player. 我使用AVPlayer作为播放器。 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 . 我有一个用于流传输的时间范围的滑块(黑色的)和另一个用于AVPlayer.currentTime滑块。 I have two audio music streamed and their music durations are added together which is now 8:46. 我有两个音频音乐流,并且它们的音乐时长加在一起现在是8:46。 My first music has 6 minutes duration and my second music has 1:46. 我的第一首音乐时长为6分钟,第二首音乐时长为1:46。 As you can see in the above photo, my streamed time ranges slider indicates that AVAsset has completely streamed the first music. 如上图所示,我的流式传输时间范围滑块指示AVAsset已完全流式传输了第一首音乐。 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. 它只是停止并且滑块值恢复为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. 我要完成的是,当第一个项目结束时, AVPlayer会加载另一个播放器项目,这就是第二个音乐。 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. 要一个接一个地加载音频文件,可以使用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. 希望这对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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