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

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 output of NSLog from NSLog(@"songURL = %@", songURL); and paste it into safari, Quicktime plugin plays files without problems, so I know that the URLs are valid. I tried .m4a and .mp3 files and tried to remove spaces from songName , but it doesn't matter that I always get Error:

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

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

+4
source share
5 answers

AVAudioPlayer does not seem to support streaming. Apple recommends AVPlayer for this, although it is not so convenient for finding things like current time and duration.

+4
source

Error -43 indicates an inability to find data. This may be because the URL is actually garbled or because the server does not support streaming. You should try to preload the song data using the 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]); } 
+1
source

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 song url was like NSString. My NSLog wrote this correctly, but when I typed% d in the url, it turned out wrong. I suggest you try checking the AVAudioPlayer URL again.

try something like the one described here to see the url of the players.

0
source

You might want to check the goals in your audio file. I had a similar problem, and I fixed it, making sure the checkbox for the target membership in the mp4 file is checked. I did not have to check it when I imported the file initially.

0
source

in the Apple documentation

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

Apple recommends using this class to play sound if you are not using play sound captured from the network stream or require very low I / O latency.

-1
source

Source: https://habr.com/ru/post/921639/


All Articles