AVAudioPlayer不播放AMR文件

时间:2014-06-08 07:02:13

标签: ios objective-c avaudioplayer amr

我广告使用以下代码从网上下载AMR文件并按AVAudioPlayer播放,但所有时间都出现unrecongnize selector sent to instance错误。

这是开始下载和播放的方法:

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

修改

基于@Michael建议我更改我的代码,post我将代码更改为:

NSURL *fileURL = [[NSURL alloc] initWithString: @"http://www.domain1.com/mysound.mp3"];

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

现在它播放了声音,但是当我使用http://maindomain.com/mysound.amr时,它没有播放声音。

1 个答案:

答案 0 :(得分:0)

检查以下几点:

  • 不再支持AMR(对于≥iOS4.3,请参阅supported audio formats in the Apple iOS SDK Documentation)。
  • 您想使用AVPlayer(音频和视频)还是只想要音频?仅用于音频AVAudioPlayer。以下代码段显示了如何处理它。
  • 如果您想使用AVAudioPlayer,您的自我实例是否会实现AVAudioPlayerDelegate Protocol
  • 您的自我实例是否实现了AVAudioPlayerDelegate Protocol
  • 您是否正确添加并链接了AVAudioFoundation框架(#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];
    }