加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在iPhone中使用URL链接播放音乐文件?

发布时间:2020-12-15 01:45:07 所属栏目:百科 来源:网络整理
导读:我想在iPhone中使用URL Link播放音乐文件.但是,当我使用下面的代码时,我收到错误,我没有得到我出错的地方.任何人都可以纠正我吗? -(void)viewDidLoad {[super viewDidLoad];NSString* resourcePath = @"http://192.167.1.104:8888/SHREYA.mp3"; //your urlN
我想在iPhone中使用URL Link播放音乐文件.但是,当我使用下面的代码时,我收到错误,我没有得到我出错的地方.任何人都可以纠正我吗?

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

使用以下代码播放音乐:

[self.player play];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读