如何在iPhone中使用URL链接播放音乐文件? [英] How to play a music file using URL link in iPhone?

查看:191
本文介绍了如何在iPhone中使用URL链接播放音乐文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPhone中使用URL Link播放音乐文件。但是,当我使用下面的代码时,我收到错误,我没有得到我出错的地方。任何人都可以纠正我吗?

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?

-(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];
}


推荐答案

这应该有效:

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];

这篇关于如何在iPhone中使用URL链接播放音乐文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆