ios 使用 AVAudioPlayer 播放来自互联网的音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3635792/
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
play audio from internet using AVAudioPlayer
提问by Bangdel
I am implementing AVAudioPlayer to play audio and it works perfectly well while playing files locally stored in the PC.
我正在实现 AVAudioPlayer 来播放音频,它在播放本地存储在 PC 中的文件时运行良好。
But when i give the url of some audio file over the internet, it fails sadly. Here's what the code looks like:
但是当我通过互联网提供一些音频文件的网址时,它很遗憾地失败了。代码如下所示:
NSString *url = [[NSString alloc] init];
url = @"http://files.website.net/audio/files/audioFile.mp3";
NSURL *fileURL = [[NSURL alloc] initWithString: url];
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
Could anybody please point out the problem and what could be done?
Thanks!
任何人都可以指出问题以及可以做些什么?
谢谢!
采纳答案by Borut Tomazin
回答by Gautam Jain
Use AVPlayer to stream audio/video based on http url's. It will work fine. AVAudioPlayer is for local files. Here's the code
使用 AVPlayer 根据 http url 流式传输音频/视频。它会正常工作。AVAudioPlayer 用于本地文件。这是代码
NSURL *url = [NSURL URLWithString:url];
self.avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
self.playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
self.audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[self.audioPlayer play];
回答by leviathan
This is what the Apple docs say:
这是苹果文档所说的:
The AVAudioPlayer
class does not provide support for streaming audio based on HTTP URL's. The URL used with initWithContentsOfURL:
must be a File URL (file://
). That is, a local path.
本AVAudioPlayer
类不基于HTTP URL的音频流提供支持。使用的 URLinitWithContentsOfURL:
必须是文件 URL ( file://
)。即本地路径。
回答by alijandro
Use AVPlayer
and monitor its status to start playback.
使用AVPlayer
并监控其状态以开始播放。
Here is a workable example, hope it would be helpful.
这是一个可行的例子,希望它会有所帮助。
@implementation AudioStream
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (context == PlayerStatusContext) {
AVPlayer *thePlayer = (AVPlayer *)object;
switch ([thePlayer status]) {
case AVPlayerStatusReadyToPlay:
NSLog(@"player status ready to play");
[thePlayer play];
break;
case AVPlayerStatusFailed:
NSLog(@"player status failed");
break;
default:
break;
}
return;
} else if (context == ItemStatusContext) {
AVPlayerItem *thePlayerItem = (AVPlayerItem *)object;
switch ([thePlayerItem status]) {
case AVPlayerItemStatusReadyToPlay:
NSLog(@"player item ready to play");
break;
case AVPlayerItemStatusFailed:
NSLog(@"player item failed");
break;
default:
break;
}
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)playAudioStream
{
NSURL *audioUrl = [NSURL URLWithString:@"your_stream_url"];
AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioUrl];
AVPlayerItem *audioPlayerItem = [AVPlayerItem playerItemWithAsset:audioAsset];
[audioPlayerItem addObserver:self forKeyPath:@"status" options:0 context:ItemStatusContext];
self.player = [AVPlayer playerWithPlayerItem:audioPlayerItem];
[self.player addObserver:self forKeyPath:@"status" options:0 context:PlayerStatusContext];
}
@end