ios 如何在 iPhone 中使用 URL 链接播放音乐文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11716244/
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 play a music file using URL link in iPhone?
提问by Abhilash
I want to play a music file using URL Link in the iPhone. But when I use the below code I am getting error I am not getting where I am going wrong. Can anyone Correct me?
我想在 iPhone 中使用 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];
}
回答by iPhone Developer
This should work:
这应该有效:
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];
Use the following code to play the music:
使用以下代码播放音乐:
[self.player play];
[self.player play];
回答by Aklesh Rathaur
Try the following:
请尝试以下操作:
-(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];
回答by Deepak Samuel Rajan
回答by Paresh Navadiya
Current Answer:
当前答案:
Now consider Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43link. its solution is avplayerlink
现在考虑为什么 AVAudioPlayer initWithContentsOfURL 总是失败,错误代码=-43链接。它的解决方案是avplayer链接
Earlier Answer
较早的回答
//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];
回答by enjoy-writing
Try my code:
试试我的代码:
NSURL *filepath = [NSURL fileURLWithPath:audioFilePath];
[yourMediaPlayer setContentURL:filepath];
//set player.
yourMediaPlayer.controlStyle = MPMovieControlStyleNone;
yourMediaPlayer.currentPlaybackTime = 0;
yourMediaPlayer.shouldAutoplay = FALSE;
[yourMediaPlayer play];
^-^,I use MPMoviePlayerController with above code.
^-^,我在上面的代码中使用了 MPMoviePlayerController。