我广告使用以下代码从网上下载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
时,它没有播放声音。
答案 0 :(得分:0)
检查以下几点:
AVPlayer
(音频和视频)还是只想要音频?仅用于音频AVAudioPlayer
。以下代码段显示了如何处理它。AVAudioPlayer
,您的自我实例是否会实现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];
}