AVAudioPlayer does not play AMR files

I use the code below to download an AMR file from the Internet and play using AVAudioPlayer , but I get an unrecongnize selector sent to instance error all the time.

This method starts the download and playback:

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

Edit

Based on @Michael's advice, I am changing my code and this post . I changed my code to this:

 NSURL *fileURL = [[NSURL alloc] initWithString: @"http://www.domain1.com/mysound.mp3"]; //Initialize the AVAudioPlayer. audioPlayer= [AVPlayer playerWithURL:fileURL]; [audioPlayer play]; 

Now it played sound, but when I use http://maindomain.com/mysound.amr it does not play sound.

+1
source share
1 answer

Check out 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 AVPlayer (audio and video) or just want audio? Use only AVAudioPlayer for audio. The following code snippet shows how to handle it.
  • If you want to use AVAudioPlayer , AVAudioPlayer your self instance implement the AVAudioPlayerDelegate Protocol ?
  • Does your self instance create the AVAudioPlayerDelegate protocol ?
  • Have you added and correctly linked the AVAudioFoundation structure ( #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]); } else { self.audioPlayer.delegate = self; [self.audioPlayer prepareToPlay]; } } - (void)playAudio { [self.audioPlayer play]; } 
0
source

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


All Articles