ios 使用 AVPlayer 流式传输 mp3 音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13131177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Streaming mp3 audio with AVPlayer
提问by derekahc
I'm hearing some conflicting reports about this. What I'm trying to do is stream an mp3 file from a URL. I've done hours of research, but I cannot find any good guides on how to do this, or even what kind of audio player I should use.
我听到一些关于此的相互矛盾的报道。我想要做的是从 URL 流式传输 mp3 文件。我已经做了数小时的研究,但我找不到任何关于如何做到这一点的好指南,甚至找不到我应该使用什么样的音频播放器。
Some friends tell me that AVPlayer can stream mp3, but the Apple documentation says it can't. I've poured over Matt Gallagher's audio streamer (http://www.cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html), but that code was made a good while ago, and I'm new enough to this that it's hard to work through the autoreleases and retains and all that.
有些朋友告诉我 AVPlayer 可以流式传输 mp3,但 Apple 文档说它不能。我已经倾倒了 Matt Gallagher 的音频流媒体 (http://www.cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html),但该代码是很久以前制作的,并且我对此很陌生,很难通过自动发布和保留以及所有这些来工作。
The audio I'm trying to stream is a fairly large mp3 file from a libsyn server, with a URL of format..
我尝试流式传输的音频是来自 libsyn 服务器的相当大的 mp3 文件,其 URL 格式为 ..
http://traffic.libsyn.com/podcastname/episode.mp3
All I need to do is grab it and start playing, with the ability to pause and scrub. So first things first, CAN AVPlayer stream mp3's? And if so, does anybody have any guides or code they can point me to? And if not, is there any kind of audio player class that can stream audio?
我需要做的就是抓住它并开始播放,并具有暂停和擦洗的能力。那么首先,可以 AVPlayer 流 mp3 吗?如果是这样,有没有人可以指点我的任何指南或代码?如果没有,是否有任何类型的音频播放器类可以流式传输音频?
I've tried creating an AVPlayerItem, initialized with the URL, then adding it to an AVPlayer, but I'm getting a ton of Error Loading... and Symbol Not Found... errors. I'd appreciate any information on this, thank you!
我已经尝试创建一个 AVPlayerItem,用 URL 初始化,然后将它添加到一个 AVPlayer,但我收到了大量错误加载...和符号未找到...错误。我很感激这方面的任何信息,谢谢!
回答by Sumit Mundra
try this
尝试这个
-(void)playselectedsong{
AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
self.songPlayer = player;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[songPlayer currentItem]];
[self.songPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == songPlayer && [keyPath isEqualToString:@"status"]) {
if (songPlayer.status == AVPlayerStatusFailed) {
NSLog(@"AVPlayer Failed");
} else if (songPlayer.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayerStatusReadyToPlay");
[self.songPlayer play];
} else if (songPlayer.status == AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
// code here to play next sound file
}
回答by tumtumtum
You can also try my open source Audjustable library which supports HTTP streaming. It's based on Matt's AudioStreamer but has been tidied, optimised and updated to support multiple data sources (non HTTP) and gapless playback.
您还可以尝试我的支持 HTTP 流的开源 Audjustable 库。它基于 Matt 的 AudioStreamer,但经过整理、优化和更新,以支持多个数据源(非 HTTP)和无缝播放。
回答by Aaron Vegh
In addition to Sumit Mundra's answer, which helped me a lot, I found that this technique doesn't actually streamMP3 files from a remote server. When I implemented this, the file downloaded synchronously, blocking my UI, before playing. The way to properly stream the MP3 that I found worked very well was to point to an M3U file. This is just a text file with an .m3u extension which contains a link to the original MP3. Point Sumit's code at that file instead, and you have a stream that starts playing immediately.
除了 Sumit Mundra 的回答对我帮助很大之外,我发现这种技术实际上并没有从远程服务器流式传输MP3 文件。当我实现这个时,文件同步下载,在播放之前阻塞了我的 UI。我发现效果很好的正确流式传输 MP3 的方法是指向一个 M3U 文件。这只是一个带有 .m3u 扩展名的文本文件,其中包含指向原始 MP3 的链接。相反,将 Sumit 的代码指向该文件,您将拥有一个立即开始播放的流。
This is the place I found that information: http://www.soundabout.net/streammp3.htm
这是我找到该信息的地方:http: //www.soundabout.net/streammp3.htm
回答by dcpedit
Aaron's post about using an m3u file instead of an mp3 worked for me. I also found that AVPlayer was picky about the m3u syntax. For example, when I tried the following, I was unable to get a valid duration (it was always indefinite), and relative paths didn't work:
Aaron 关于使用 m3u 文件而不是 mp3 的帖子对我有用。我还发现 AVPlayer 对 m3u 语法很挑剔。例如,当我尝试以下操作时,我无法获得有效的持续时间(始终是不确定的),并且相对路径不起作用:
#EXTM3U
#EXTINF:71
https://test-domain.com/90c9a240-51b3-11e9-bb69-c1300ce2348f.mp3
However, after updating the m3u file to the following, both issues were resolved:
但是,将 m3u 文件更新为以下内容后,这两个问题都得到了解决:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:70
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:70.000,
8577d650-51b3-11e9-8e69-4f2b085e94aa.mp3
#EXT-X-ENDLIST
回答by Iain Smith
Matt Gallagher's AudioStreamer was updated 2 months ago https://github.com/mattgallagher/AudioStreamer/commits/master
Matt Gallagher 的 AudioStreamer 于 2 个月前更新https://github.com/mattgallagher/AudioStreamer/commits/master
But for what your looking for check out the sample code StichedStreamPlayer http://developer.apple.com/library/ios/#samplecode/StitchedStreamPlayer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010092
但是对于您要查找的内容,请查看示例代码 StichedStreamPlayer http://developer.apple.com/library/ios/#samplecode/StitchedStreamPlayer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010092
It uses an AVPlayer object and if you look at method - (IBAction)loadMovieButtonPressed:(id)sender you should be able to follow how it sets up the AVPlayer Object.
它使用一个 AVPlayer 对象,如果您查看方法 - (IBAction)loadMovieButtonPressed:(id)sender,您应该能够了解它如何设置 AVPlayer 对象。