Ios 使用AVAudioPlayer从internet播放音频
我正在实现AVAudioPlayer来播放音频,它在播放本地存储在PC中的文件时工作得非常好 但是,当我在互联网上给出某个音频文件的url时,它失败了。 下面是代码的样子:Ios 使用AVAudioPlayer从internet播放音频,ios,objective-c,streaming,avaudioplayer,Ios,Objective C,Streaming,Avaudioplayer,我正在实现AVAudioPlayer来播放音频,它在播放本地存储在PC中的文件时工作得非常好 但是,当我在互联网上给出某个音频文件的url时,它失败了。 下面是代码的样子: NSString *url = [[NSString alloc] init]; url = @"http://files.website.net/audio/files/audioFile.mp3"; NSURL *fileURL = [[NSURL alloc] initWithString: url]; A
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];
有人能指出问题所在以及可以采取的措施吗?谢谢 我在AVAudioPlayer上尝试了其他方法initWithData,而不是initWithContentsOfURL。首先尝试将MP3文件转换为NSData,然后播放此数据
看看我的代码。这是苹果的文档所说的:
AVAudioPlayer
类不支持基于HTTP URL的流式音频。与initWithContentsOfURL:
一起使用的URL必须是文件URL(File://
)。即本地路径。使用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];
使用
AVPlayer
并监视其状态以开始播放
这里有一个可行的例子,希望能有所帮助
@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
@实现音频流
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文
{
如果(上下文==PlayerStatusContext){
AVPlayer*播放器=(AVPlayer*)对象;
交换机([打印机状态]){
案例AVPlayerStatusReadyToPlay:
NSLog(@“玩家状态准备好玩”);
[thePlayer play];
打破
案例AVPlayerStatus失败:
NSLog(@“玩家状态失败”);
打破
违约:
打破
}
返回;
}else if(context==ItemStatusContext){
AVPlayerItem*播放程序=(AVPlayerItem*)对象;
开关([thePlayerItem状态]){
案例AVPlayerItemStatusReadyToPlay:
NSLog(@“玩家物品准备就绪”);
打破
案例AVPlayerItemStatus失败:
NSLog(@“玩家项目失败”);
打破
违约:
打破
}
返回;
}
[super observeValueForKeyPath:对象的关键路径:对象更改:更改上下文:上下文];
}
-(void)播放音频流
{
NSURL*audioUrl=[NSURL URLWithString:@“您的音乐流”];
AVURLAsset*audioAsset=[AVURLAsset-assetwithur:audioUrl];
AVPlayerItem*audioPlayerItem=[AVPlayerItem playerItemWithAsset:audioAsset];
[audioPlayerItem addObserver:self-forKeyPath:@“状态”选项:0上下文:ItemStatusContext];
self.player=[AVPlayer playerWithPlayerItem:audioPlayerItem];
[self.player addObserver:self forKeyPath:@“状态”选项:0上下文:PlayerStatusContext];
}
@结束
您是否尝试提供错误对象以查看其是否包含错误描述?没有,但应用程序也不会崩溃…只是视图出现,什么也没有发生。我花了很长时间尝试解决此问题,直到在此处找到您的答案。谢谢网络陌生人。这帮了大忙。