1

我使用下面的代码从网络下载 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建议我更改了我的代码,这篇文章我将我的代码更改为:

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它时它没有播放声音。

4

1 回答 1

0

检查以下几点:

  • 不再支持 AMR(对于 ≥ iOS 4.3,请参阅Apple iOS SDK 文档中支持的音频格式)。
  • 您想使用AVPlayer(音频和视频)还是只需要音频?仅用于音频AVAudioPlayer。以下代码片段显示了如何处理它。
  • 如果你想使用AVAudioPlayer,你自己的实例是否实现了AVAudioPlayerDelegate 协议
  • 您的 self 实例是否实现了AVAudioPlayerDelegate 协议
  • 您是否添加并正确链接了 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];
    }
    
于 2014-06-08T07:31:54.840 回答