首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioPlayer不播放AMR文件

AVAudioPlayer不播放AMR文件
EN

Stack Overflow用户
提问于 2014-06-08 07:02:13
回答 1查看 2.3K关注 0票数 1

我广告使用下面的代码从网络下载一个AMR文件,并由AVAudioPlayer播放,但我总是得到unrecongnize selector sent to instance错误。

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

代码语言:javascript
复制
- (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建议我更改代码,帖子将代码更改为:

代码语言:javascript
复制
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时,它不是在播放声音。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-08 07:31:54

检查以下几点:

  • AMR不再受支持(关于iOS 4.3,请参见Apple iOS SDK文档中支持的音频格式)。
  • 您想使用AVPlayer (音频和视频)还是只想要音频?仅用于音频AVAudioPlayer。下面的代码片段演示如何处理它。
  • 如果您想使用AVAudioPlayer,那么您自己的实例是否实现了AVAudioPlayerDelegate协议
  • 您自己的实例实现了AVAudioPlayerDelegate协议吗?
  • 您是否正确地添加和链接了AVAudioFoundation框架(#import <AVFoundation/AVFoundation.h>)?
  • (ViewDidLoad){ viewDidLoad;NSURL *url = [NSURL fileURLWithPath:[NSBundle mainBundle pathForResource:@“音频”ofType:@"m4a"]];NSError *error = noErr;self.audioPlayer = [AVAudioPlayer alloc initWithContentsOfURL:url error:& error ];如果(错误){ NSLog(@"Error in audioPlayer:%@",error localizedDescription);}NSBundle{ self.audioPlayer.delegate = self;self.audioPlayer prepareToPlay;}- (void)playAudio { self.audioPlayer play;}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24103790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档