Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see at least one issue with your code, with the line </p> <pre><code>NSURL *fileURL = [NSURL URLWithString:filePath]; </code></pre> <p>It tries to create an URL with a local file path. It will probably return nil. You should instead use:</p> <pre><code>NSURL *fileURL = [NSURL fileURLWithPath:filePath]; </code></pre> <p>Now, even with this, your code does not run (I tried it and got the same error you got). I have not used AVAudioPlayer very much myself so I do not know why your specific error happens.</p> <p>However, I did try a very small example using your code and I was able to play the MP3 correctly. Instead of downloading the data to a file and loading AVAudioPlayer from a file, you can use the initWithData constructor of AVAudioPlayer:</p> <pre><code>AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfURL:audioURL] error:&amp;error]; </code></pre> <p>This works fine on my side, assuming that audioURL is valid. I pointed to a mp4 on my server for my test.</p> <p>So rewriting your code with this initWithData gives:</p> <pre><code>- (IBAction)play:(UIButton *)sender { dispatch_queue_t downloadQueue = dispatch_queue_create("audio data downloader", NULL); dispatch_async(downloadQueue, ^{ NSURL *audioURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/%@.mp3", FONYK_FILES_URL, [self.voicenote valueForKeyPath:@"Fonyker.fonykid"], [self.voicenote valueForKeyPath:@"Voicenote.vnid"]]]; NSData *audioData = [NSData dataWithContentsOfURL:audioURL]; dispatch_async(dispatch_get_main_queue(), ^{ NSError *error = nil; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&amp;error]; NSLog(@"%@", error); audioPlayer.delegate = self; [audioPlayer play]; }); }); } </code></pre> <p>Just as a last note: You are downloading the entire file before you start playing, which may take a while and consume a lot of heap (or storage if you were to save it on a file)</p> <p>If you want to stream, you can use a MPMoviePlayerController, but you do not attach the view to your view hierarchy. This way, you will only have audio and will not change any visual of your application.</p> <p>An example would be:</p> <pre><code>NSURL *audioURL = [NSURL URLWithString:@"MY_MP3_URL"]; MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:audioURL]; [player play]; </code></pre> <p>Again, on my side, in the Simulator, it works fine.</p> <p>Do not forget to release the player. You can also register delegates for important notifications.</p> <p>Details here: <a href="http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html" rel="nofollow">http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload