I ad using below code to download an AMR file from web and playing by AVAudioPlayer
but all times I get the unrecongnize selector sent to instance
error.
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]
}
Edit
Based on @Michael advise I change 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 the sound but when I use http://maindomain.com/mysound.amr
it is not playing the sound.
Check the following points:
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.AVAudioPlayer
, 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]);
}
else
{
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
}
}
- (void)playAudio
{
[self.audioPlayer play];
}