ios 如何从 iPhone 上的在线网址播放音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15850447/
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
How to get audio to play from an online url on iPhone
提问by falky
I know it's been asked before by plenty of people, but I just have no idea why this isn't working in my application in x-code and objective c!
我知道之前有很多人问过这个问题,但我只是不知道为什么这在我的 x-code 和目标 c 应用程序中不起作用!
Basically, I want to play audio in an application from a url. Just using the testing audio from the Apple site, my code for it is:
基本上,我想从 url 在应用程序中播放音频。仅使用来自 Apple 网站的测试音频,我的代码是:
NSString *playString = @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";
//Converts songURL into a playable NSURL
NSURL *playURL = [NSURL URLWithString:playString];
AVAudioPlayer *backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error];
if (backgroundMusicPlayer == nil) {
NSLog(@"%@", [error description]);
}
else {
[backgroundMusicPlayer prepareToPlay];
[backgroundMusicPlayer play];
}
It comes up with the following error:
它出现以下错误:
Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn't be completed. (OSStatus error -43.)
错误域=NSOSStatusErrorDomain 代码=-43 “操作无法完成。(OSStatus 错误 -43。)
And I can't find anything when I google it. Any ideas? Should I try another audio stream? Or does anyone have an example that works? Is it because I am using the iPhone simulator not the actual iPhone?
当我谷歌它时,我找不到任何东西。有任何想法吗?我应该尝试另一个音频流吗?或者有人有一个有效的例子吗?是不是因为我使用的是 iPhone 模拟器而不是实际的 iPhone?
EDIT:
编辑:
Please note - When I say URL I mean from an actual website, like "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8" I don't want to have to download the song first, I want to be able to play it as it is downloading! Thanks
请注意 - 当我说 URL 时,我的意思是来自实际网站,例如“ http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8”我不想先下载这首歌,我希望能够在下载时播放它!谢谢
回答by Sumit Mundra
USe AVPlayer to play mp3 file from url
使用 AVPlayer 从 url 播放 mp3 文件
-(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];
[self.songPlayer play];
}
- (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");
} else if (songPlayer.status == AVPlayerItemStatusUnknown) {
NSLog(@"AVPlayer Unknown");
}
}
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
// code here to play next sound file
}
回答by iSpark
its very simple Use AVAudioPlayer,take object as AVAudioPlayer *audioPlayer,
它非常简单 使用 AVAudioPlayer,将对象作为 AVAudioPlayer *audioPlayer,
NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
NSData *soundData = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:NULL];
audioPlayer.delegate = self;
[audioPlayer play];
write below delegate Methods:
在委托方法下面写:
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
[audioPlayer stop];
NSLog(@"Finished Playing");
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(@"Error occured");
}
回答by 9to5ios
You may find a live test stream at here
To play a file use :
播放文件使用:
[player play];
And here is the code to play a live stream from URL:
这是从 URL 播放实时流的代码:
NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
/* [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext]; (can use it) */
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];