NSString *songNameEscaped = [songName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *songURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://somerootpath/Music/", songNameEscaped]];
NSLog(@"songURL = %@", songURL);
NSError *avPlayerError = nil;
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:&avPlayerError];
if (avPlayerError)
{
NSLog(@"Error: %@", [avPlayerError description]);
}
else
{
[avPlayer play];
}如果我复制来自NSLog的NSLog(@"songURL = %@", songURL);输出并将其粘贴到safari中,则Quicktime插件不会播放文件,因此我知道URL是有效的。我尝试过使用.m4a和.mp3文件,并尝试从songName中删除空格,但不管我总是收到什么错误:
Error Domain=NSOSStatusErrorDomain Code=-43“操作无法完成(OSStatus错误-43.)”。
但它们只是在.m4a中创建的标准.mp3 / iTunes文件。
发布于 2012-03-07 05:10:22
显然,AVAudioPlayer不支持流媒体。苹果为此建议使用AVPlayer,尽管它并不能像寻找当前时间和持续时间那样传达信息。
发布于 2013-04-19 11:12:29
Error -43表示无法找到数据。这可能是因为url实际上格式错误,或者因为服务器不支持流。您应该尝试使用NSData对象预加载歌曲数据。
NSString *songNameEscaped = [songName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *songURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://somerootpath/Music/", songNameEscaped]];
NSLog(@"songURL = %@", songURL);
NSError *avPlayerError = nil;
NSData* songData = [NSData dataWithContentsOfURL:songURL error:&avPlayerError];
if (songData)
{
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithData error:&avPlayerError];
if (avPlayer)
{
[avPlayer prepareToPlay];
[avPlayer play];
}
else
{
NSLog(@"Error initializing data for AVAudioPlayer. Possibly an Unsupported Format");
NSLog(@"Error: %@", [avPlayerError description]);
}
}
else
{
NSLog(@"Error initializing data for AVAudioPlayer. Possibly Malformed URL");
NSLog(@"Error: %@", [avPlayerError description]);
}发布于 2012-02-27 09:40:09
我昨天也遇到了同样的问题。原来我的网址是错的。我这里有你这样的东西:
NSURL *songURLID = [NSURL URLWithString:[NSString stringWithFormat:@"%@%d", @"http://somerootpath/Music/", songNameEscaped]];
NSLog(@"songURL = **%d**", songURL**ID**);但是,我的songURL是NSString型的。我的NSLog正确地写了它,但是当我把%d放在url中时,结果是错误的。我建议你再看看你的AVAudioPlayer网址。
尝试类似描述的here来查看玩家url。
https://stackoverflow.com/questions/9462728
复制相似问题