如何连续播放多个 .m4a 文件?
目前,我的函数开始播放一个,使用 AudioServicesPlaySystemSound 等,使用 AudioServicesAddSystemSoundCompletion 选择回调,然后返回。声音完成后,回调让我的软件继续。
每当有背靠背的声音时,这就是很多编码。
我的软件可以开始播放 .m4a 并以某种方式等待它完成,等待它的回调设置标志吗?我可以使用 pthread 软件进行多任务暂停的等待循环吗?
到目前为止,这是我的代码...
// 播放声音文件后恢复game_state继续游戏。int game_state_after_sound;void 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;
}