AVAudioPlayer do not play AMR files

人盡茶涼 提交于 2019-12-23 03:38:21

问题


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.


回答1:


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]);
        }
        else 
        {
            self.audioPlayer.delegate = self;
            [self.audioPlayer prepareToPlay];
        }
    }
    
    - (void)playAudio
    {
        [self.audioPlayer play];
    }
    


来源:https://stackoverflow.com/questions/24103790/avaudioplayer-do-not-play-amr-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!