【问题标题】:AVAudioPlayer do not play AMR filesAVAudioPlayer 不播放 AMR 文件
【发布时间】:2014-06-08 07:02:13
【问题描述】:

我使用以下代码从网络下载 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 时它没有播放声音。

【问题讨论】:

  • 不要将AVAudioPlayerAVPlayer 混淆!后者没有delegate 的setter 方法。

标签: ios objective-c avaudioplayer amr


【解决方案1】:

检查以下几点:

  • 不再支持 AMR(对于 ≥ iOS 4.3,请参阅 supported audio formats in the Apple iOS SDK Documentation)。
  • 您想使用AVPlayer(音频和视频)还是只需要音频?仅用于音频AVAudioPlayer。下面的代码 sn -p 展示了如何处理它。
  • 如果你想使用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];
    }
    

【讨论】:

  • 不再支持(≥ iOS 4.3)AMR。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多