如何在iPhone中使用URL链接播放音乐文件?
我想在iPhone中使用URL链接播放音乐文件。但是当我使用下面的代码时,我得到了错误,我没有得到我要出错的地方。有人能纠正我吗如何在iPhone中使用URL链接播放音乐文件?,iphone,objective-c,ios,xcode,avfoundation,Iphone,Objective C,Ios,Xcode,Avfoundation,我想在iPhone中使用URL链接播放音乐文件。但是当我使用下面的代码时,我得到了错误,我没有得到我要出错的地方。有人能纠正我吗 -(void)viewDidLoad { [super viewDidLoad]; NSString* resourcePath = @"http://192.167.1.104:8888/SHREYA.mp3"; //your url NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URL
-(void)viewDidLoad
{
[super viewDidLoad];
NSString* resourcePath = @"http://192.167.1.104:8888/SHREYA.mp3"; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:_objectData error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
目前的答复:
现在考虑链接。它的解决方案是链接
早先的答复//You have to pass NSURL not NSData
NSString* resourcePath = @"http://192.167.1.104:8888/SHREYA.mp3"; //your url
NSURL *url = [NSURL alloc]initWithString:resourcePath];
audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];
audioPlayer.delegate = self;
[url release];
[audioPlayer prepareToPlay];
[audioPlayer play];
这应该起作用:
NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>];
// You may find a test stream at <http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8>.
self.playerItem = [AVPlayerItem playerItemWithURL:url];
//(optional) [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:<#Live stream URL#>];
//(optional) [player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];
NSURL*url=[NSURL-URLWithString:@“];
//您可以在中找到测试流。
self.playerItem=[AVPlayerItem playerItemWithURL:url];
//(可选)[playerItem addObserver:self-forKeyPath:@“状态”选项:0上下文:&ItemStatusContext];
self.player=[AVPlayer playerWithPlayerItem:playerItem];
self.player=[AVPlayer playerWithURL:];
//(可选)[player addObserver:self-forKeyPath:@“状态”选项:0上下文:&PlayerStatusContext];
使用以下代码播放音乐:
[self.player play];
试试我的代码:
NSURL *filepath = [NSURL fileURLWithPath:audioFilePath];
[yourMediaPlayer setContentURL:filepath];
//set player.
yourMediaPlayer.controlStyle = MPMovieControlStyleNone;
yourMediaPlayer.currentPlaybackTime = 0;
yourMediaPlayer.shouldAutoplay = FALSE;
[yourMediaPlayer play];
^-^,我使用带有上述代码的MPMoviePlayerController。请尝试以下操作:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString* resourcePath = @"your url"; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
我想你是在发邮件
audioPlayer.numberOfLoops = -1;
要接收NSInvalidArgumentException,请将其更改为
audioPlayer.numberOfLoops = 1;
并检查苹果的文档@H2CO3运行时AudioPlayer[1012:10703]期间出错***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[NSConcreteData baseURL]:无法识别的选择器发送到实例0x7345540'***First throw call stackYou正试图将NSData传递给NSURL,这会导致错误。“initWithContentsOfURL:_objectData”@iPhoneDeveloper ok我已更正,但错误仍然是AudioPlayer[1143:10703]error Domain=nsossStatUserRorDomain Code=-43”操作无法完成。(OSStatus错误-43。)“试试这个:NSData*songFile=[[NSData alloc]initWithContentsOfURL:songCacheURL选项:NSDataReadingMappedIfSafe错误:&error1];self.audioPlayer=[[AVAudioPlayer alloc]initWithData:songFile错误:&error2];@iPhoneDeveloper不,它不会工作很多错误发生谢谢你,但错误仍然是audioPlayer[1125:10703]错误域=NSOSStatusErrorDomain代码=-43“该操作无法完成。(OSStatus错误-43。)"答案已编辑使用initWithURL而不是InitWithContentsOfUrl否仍然无效上一个错误正在重复***由于未捕获的异常而终止应用程序上面的代码中的“NSInvalidArgumentException”播放器是AVPlayer的对象。playerITem呢?Bcoz我收到了这么多错误。self.playerITem是AVPlayerItem和self.player的对象是AVPlayerhaving错误的对象,具有未声明的&ItemsStatuscontext和PlayerstatusContext[playerItem addObserver:self forKeyPath:@“status”选项:0上下文:&ItemStatusContext];[player addObserver:self-forKeyPath:@“状态”选项:0上下文:&PlayerStatusContext];用于告诉您可以对给定函数执行任何选择器。。你不能使用它们…我用这个代码得到了它。我只是有一个问题,为什么我们要通过调用一个构造函数传递一个playerItem来设置一次AVPlayer,然后再调用另一次传递NSURL。我用swift写了我的,只是好奇为什么两者都需要。