Search code examples
iosobjective-cswiftavaudioplayernsurl

Play sound with AVAudioPlayer and a valid URL


I have a sound file recorded on my iPad. This file is saved in the documents directory. Now I want to play this sound, but I get an error.

I want to play sound like this:

NSURL *soundFileURL = [NSURL URLWithString:ulrString];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&err];

soundFileURL is:

Printing description of soundFileURL:
/var/mobile/Applications/1824FDFA-18D7-472F-8C4D-16270372F9C9/Documents/Aufnahme-M-bi-1-7.m4a
(lldb) 

But I get an error:

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

I have also tried following path to the sound file:

@"file:///var/mobile/Applications/1824FDFA-18D7-472F-8C4D-16270372F9C9/Documents/Aufnahme-M-bi-1-7.m4a"

But this also does't work :/


Solution

  • The file is malformed. It is reporting to you a kAudioFileInvalidFileError.

    Try other format such as .caf and see if works.

    I have had problems with playing .m4a in the past for files that were not from iTunes.

    Again, try other format first to narrow down the problem. My guess is it would work when you switch to another format, because you code seems fine to me.

    Hope this helps.