iOS xcode4 AVQueuePlayer不能播放声音

我试图在iPhone4上使用AVQueuePlayer播放.m4a声音文件,但是没有声音。 如果我用相同的文件尝试AudioServicesPlaySystemSound,所有的作品没关系。

这是代码。

(AVFoundation framework is installed.) #import <AVFoundation/AVFoundation.h> -(void) play_sound_file: (NSString *) sound_file_m4a { printf("\n play_sound_file 1: '%s' ", [sound_file_m4a UTF8String] ); AVPlayerItem *sound_file = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@"m4a"]]]; AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:sound_file, nil]]; [player play]; return; } 

最新的代码截至2012年8月28日………..(注意:两个声音文件均可与AudioServicesPlaySystemSound配合使用)

 AVPlayerItem *sound_file1 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"Hold still"] ofType:@"m4a"]]]; AVPlayerItem *sound_file2 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"Press go"] ofType:@"m4a"]]]; AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems: [NSArray arrayWithObjects:sound_file1, sound_file2, nil]]; [player play]; 

我在上一篇文章中解释了如何解决这个问题。 这可能无法正常工作的原因之一是因为您要将一个项目传递给arrayWithObjects:这需要多个项目。 另一件可能导致这种情况不起作用的原因是,如果你正试图在模拟器中运行它,那么AVQueuePlayer和模拟器之间就存在已知的兼容性问题。

也:

更换

 AVPlayerItem *sound_file = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@"m4a"]]]; 

 AVPlayerItem *sound_file = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"sound_file_m4a"] ofType:@"m4a"]]]; 

编辑:这是一个如何顺序播放audio文件的例子。

在你的头文件中:

 #import <AVFoundation/AVFoundation.h> AVAudioPlayer *data; NSArray *arrayOfTracks; NSUInteger currentTrackNumber; 

然后在你的实现文件中:

 - (void)viewDidLoad { [super viewDidLoad]; arrayOfTracks = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];//Do NOT include file extension here currentTrackNumber = 0; } - (void)startPlaying { if (data) { [data stop]; //[data release]; if project isn't using Automatic Reference Counting data = nil; } data = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[arrayOfTracks objectAtIndex:currentTrackNumber]] ofType:@"m4a"]] error:NULL]; data.delegate = self; [data play]; } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { if (flag) { if (currentTrackNumber < [arrayOfTracks count] - 1) { currentTrackNumber ++; if (data) { [data stop]; //[data release]; if project isn't using Automatic Reference Counting data = nil; } data = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[arrayOfTracks objectAtIndex:currentTrackNumber]] ofType:@"m4a"]] error:NULL]; data.delegate = self; [data play]; } } }