我知道很多人之前已经问过这个问题,但我不知道为什么这个问题在我的x-code和objective c的应用程序中没有用呢!
基本上,我想通过网址在应用程序中播放音频。只需使用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];
}
它出现以下错误:
错误域= NSOSStatusErrorDomain代码= -43“无法完成操作。(OSStatus错误-43。)
当我谷歌时,我找不到任何东西。 有任何想法吗?我应该尝试其他音频流吗?或者有没有人有一个有效的例子?是因为我使用的是iPhone模拟器而不是真正的iPhone吗?
编辑:
请注意 - 当我说网址时,我指的是来自实际网站,例如“http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8” 我不想先下载这首歌,我希望能够在下载时播放它!感谢
答案 0 :(得分:7)
使用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
}
答案 1 :(得分:4)
非常简单使用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];
写下面的委托方法:
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
[audioPlayer stop];
NSLog(@"Finished Playing");
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
NSLog(@"Error occured");
}
答案 2 :(得分:2)
You may find a live test stream at here
要播放文件:
[player play];
以下是从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];