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

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

关于iphone - 如何在 iPhone 中使用 URL 链接播放音乐文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11716244/

相关文章:

ios - 如何使用 swift 列出属于 iOS 组的联系人

iphone - 在您的应用程序中显示 iphone 应用程序设置

objective-c - 如何在 UIPopoverController 上显示 UIView

iphone - 如何创建 "0000001"类型数字格式?

ios - 在处理 Swift 对象和数组时,retainership 真的很重要吗?

php - PHP服务器上的Apple推送通知问题

java - 适用于 Android 和 iPhone 的问卷

iphone - 如何在启动时使用黑色半透明状态栏 - info.plist 键/值似乎不起作用

iphone - 可以在 EAGLLayer 之上使用 Quartz 2D 绘图吗?

ios - 有没有办法使用 sysctl 获取 iPhone 型号?