【发布时间】:2012-08-26 05:04:08
【问题描述】:
如何连续播放多个 .m4a 文件?
目前,我的函数开始播放一个,使用 AudioServicesPlaySystemSound 等,使用 AudioServicesAddSystemSoundCompletion 选择回调,然后返回。声音结束后,回调让我的软件继续运行。
每当有背靠背声音时,这需要大量编码。
我的软件可以开始播放 .m4a 文件,然后以某种方式等待它完成,等待它的回调设置标志吗?我可以使用 pthread 软件做一个多任务暂停的等待循环吗?
这是我目前的代码...
// 播放声音文件后恢复game_state继续游戏。 int game_state_after_sound; 无效 play_sound_file_done ( SystemSoundID ssID, void calling_class ) { printf("\n play_sound_file_done."); // 声音现在已经完成,所以恢复 game_state 并从中断的地方继续 manage_game: game_state = game_state_after_sound; [ (GeController)calling_class manage_game]; }
// Plays sound_file_m4a and returns after playing has completed.
-(void) play_sound_file: (NSString *) sound_file_m4a
{
printf("\n play_sound_file '%s' ", [sound_file_m4a UTF8String] );
NSString *soundPath = [[NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@"m4a"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
AudioServicesAddSystemSoundCompletion ( soundID, NULL, NULL, play_sound_file_done, (void*)self );
game_state_after_sound = game_state;
game_state = DELAY_WHILE_PLAYING_SOUND;
}
********************************** AVPlayer 工作中...
但这不会产生声音。
#import <AVFoundation/AVFfoundation.h>
-(void) play_queued_sounds: (NSString *) sound_file_m4a
{
printf("\n queue_sound_file: '%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;
}
【问题讨论】: