AVAudioPlayer不播放AMR文件 [英] AVAudioPlayer do not play AMR files
问题描述
我使用以下代码从网上下载AMR文件并通过AVAudioPlayer
播放,但我一直都出现unrecongnize selector sent to instance
错误.
I ad using below code to download an AMR file from web and playing by AVAudioPlayer
but all times I get the unrecongnize selector sent to instance
error.
这是开始下载并播放的方法:
This is the method that start download and play:
- (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的建议,我更改了代码,并且此帖子我将代码更改为:
Based on @Michael advise I change my code and this post I changed my code to this:
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
时它没有播放声音.
Now it played the sound but when I use http://maindomain.com/mysound.amr
it is not playing the sound.
推荐答案
检查以下几点:
- 不再支持AMR(对于iOS 4.3或更高版本,请参见 AVAudioPlayerDelegate协议?
- 您的自我实例是否实现 AVAudioPlayerDelegate协议?
-
您是否已添加并正确链接了AVAudioFoundation框架(
#import <AVFoundation/AVFoundation.h>
)?
- AMR is no longer supported (for ≥ iOS 4.3, see supported audio formats in the Apple iOS SDK Documentation).
- Do you want to use the
AVPlayer
(audio and video) or do you want audio only? Use for Audio only theAVAudioPlayer
. The following code snippet shows how to handle it. - If you want to use
AVAudioPlayer
, does your self instance implement the AVAudioPlayerDelegate Protocol? - Does your self instance implement the AVAudioPlayerDelegate Protocol?
Have you added and linked correctly the AVAudioFoundation framework (
#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];
}
这篇关于AVAudioPlayer不播放AMR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!