(iOS) Why does AVAudioPlayer initWithContentsOfURL always fail with error code=-43

柔情痞子 提交于 2019-12-10 17:08:33

问题


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

If I copy the NSLog output from NSLog(@"songURL = %@", songURL); and paste it in safari, Quicktime plugin plays the files no problem so I know the URLs are valid. I've tried with .m4a and .mp3 files and have tried removing the spaces from songName but not matter what I always get Error:

Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)".

But they are just standard .m4a / .mp3 files created in iTunes.


回答1:


Apparently AVAudioPlayer doesn't support streaming. Apple recommends AVPlayer for this although it is not as conveinient for finding things like current time and duration .




回答2:


Error -43 indicates an inability to find the data. This could be because the url was in fact malformed, or because the server doesn't support streaming. You should try to pre-load the song data with an NSData object.

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



回答3:


I had the same problem yesterday. Turns out my url was wrong. I had something like you here:

NSURL *songURLID = [NSURL URLWithString:[NSString stringWithFormat:@"%@%d", @"http://somerootpath/Music/", songNameEscaped]];
NSLog(@"songURL = **%d**", songURL**ID**);

But, my songURL was of NSString type. My NSLog wrote it correctly but when I put %d in url it turn out wrong. I suggest you try to check out your AVAudioPlayer url again.

try something like described here to see the players url.




回答4:


You may want to try checking the targets on your audio file. I had a similar issue and fixed it by making sure that my mp4 file target membership check box was checked. I must not have checked it when I was importing the file originally.




回答5:


in Apple documentation

An instance of the AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.

Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.



来源:https://stackoverflow.com/questions/9462728/ios-why-does-avaudioplayer-initwithcontentsofurl-always-fail-with-error-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!