Husein Behboodi Rad Husein Behboodi Rad - 1 year ago 91
Objective-C Question

AVAudioPlayer do not play AMR files

I ad using below code to download an AMR file from web and playing by

but all times I get the
unrecongnize selector sent to instance

This is the method that start download and play:

- (IBAction)DisplayAudioPlayView:(id)sender
[self InitializePlayer];

-(void) InitializePlayer
// Get the file path to the doa audio to play.
NSString *filePath = [[ServerInteraction instance] getAudio:audioData.ID];

// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: filePath];

//Initialize the AVAudioPlayer.
audioPlayer= [AVPlayer playerWithURL:fileURL] ;
audioPlayer.delegate= self; //THIS LINE CAUSE ERROR//

// Preloads the buffer and prepares the audio for playing.
[audioPlayer prepareToPlay];
audioPlayer.currentTime = 0;

[audioPlayer play]


Based on @Michael advise I change my code and this post I changed my code to this:

NSURL *fileURL = [[NSURL alloc] initWithString: @""];

//Initialize the AVAudioPlayer.
audioPlayer= [AVPlayer playerWithURL:fileURL];
[audioPlayer play];

Now it played the sound but when I use
it is not playing the sound.

Answer Source

Check the following points:

  • AMR is no longer supported (for ≥ iOS 4.3, see supported audio formats in the Apple iOS SDK Documentation).
  • Do you want to use the AVPlayer (audio and video) or do you want audio only? Use for Audio only the AVAudioPlayer. The following code snippet shows how to handle it.
  • If you want to use AVAudioPlayer, does your self instance implement the AVAudioPlayerDelegate Protocol?
  • Does your self instance implement the AVAudioPlayerDelegate Protocol?
  • Have you added and linked correctly the AVAudioFoundation framework (#import <AVFoundation/AVFoundation.h>)?

    - (void)viewDidLoad
        [super viewDidLoad];
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"audio" ofType:@"m4a"]];
        NSError *error = noErr;
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        if (error)
            NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
            self.audioPlayer.delegate = self;
            [self.audioPlayer prepareToPlay];
    - (void)playAudio
        [self.audioPlayer play];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download