带有外部 URL 的 AVAudioPlayer *.m4p [英] AVAudioPlayer with external URL to *.m4p

查看:24
本文介绍了带有外部 URL 的 AVAudioPlayer *.m4p的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下.我得到了这段代码,我猜是一个损坏的 NSURL,因为初始化后 AVAudioPlayer 为零:

My Problem is the following. I got this code and i guess a corrupt NSURL since the AVAudioPlayer is nil after initializing:

NSString *dummyURLString = @"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p";
NSError *error;
NSURL *url = [NSURL URLWithString:dummyURLString]; 
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[player play];

任何建议这里出了什么问题?

Any suggestions what is going wrong here?

& 错误显示:

The &error shows this:

Error Domain=NSOSStatusErrorDomain Code=-43 "Operation could not be completed. (OSStatus error -43.)"

推荐答案

我先尝试了这个,但得到了错误 2003334207:

I tried this first but got error 2003334207:

NSData *soundData = [NSData dataWithContentsOfURL:URL];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:soundData error:&error];

看来 AVAudioPlayer 真的想要一个文件.所以我先把数据放到一个文件里:

Seems that AVAudioPlayer really wants a file. So I put the data into a file first:

NSURL *url = [NSURL URLWithString:@"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"]; 
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                        NSUserDomainMask, YES) objectAtIndex:0] 
                        stringByAppendingPathComponent:@"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
                fileURLWithPath:filePath] error:NULL];  
NSLog(@"error %@", error);

这篇关于带有外部 URL 的 AVAudioPlayer *.m4p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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