如何在iOS iPhone应用程序上以编程方式轻松回放.m4a文件

如何在iOS iPhone应用程序上以编程方式轻松回放.m4a文件,iphone,ios,audio,xcode4,Iphone,Ios,Audio,Xcode4,如何连续播放一个以上的.m4文件 目前,我的函数开始播放一个,使用AudioServicesPlaySystemSound等,使用AudioServicesAndSystemSoundCompletion选择回调,然后返回。声音结束后,回调允许我的软件继续 只要有背对背的声音,这就需要大量的编码 我的软件是否可以启动.m4a播放,并以某种方式等待其完成,等待其回调设置标志?我可以使用pthread软件进行多任务暂停的等待循环吗 这是我到目前为止的代码 //恢复游戏状态以在播放声音文件后继续游戏。

如何连续播放一个以上的.m4文件

目前,我的函数开始播放一个,使用AudioServicesPlaySystemSound等,使用AudioServicesAndSystemSoundCompletion选择回调,然后返回。声音结束后,回调允许我的软件继续

只要有背对背的声音,这就需要大量的编码

我的软件是否可以启动.m4a播放,并以某种方式等待其完成,等待其回调设置标志?我可以使用pthread软件进行多任务暂停的等待循环吗

这是我到目前为止的代码

//恢复游戏状态以在播放声音文件后继续游戏。 int game_state_后_声音; 无效播放\u声音\u文件\u完成 SystemSoundID ssID, 无效调用\u类 { 打印\n播放声音\u文件\u完成。; //声音现在已经完成,所以恢复游戏状态并从停止的位置继续管理游戏: 游戏状态=声音后的游戏状态; [GeControllercalling_类管理_游戏]; }

**********************************正在进行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;
}

这可以很容易地在AVQueuePlayer中实现。你不需要处理完成或任何事情,你只需要给它你想按顺序播放的AVPlayerItems,然后调用play。下面是一个例子:

#import <AVFoundation/AVFfoundation.h>


//AVPlayerItem *item1 = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"myFileName" ofType:@"m4a"]]];
作为补充,这也可以通过使用MPMediaPickerController的didPickMediaItems委托方法排队的MPMusicLayerController从用户iPod库中的项目实现


我无法使AVQueuePlayer解决方案工作

伟大的解决方案: 对于要播放的每个声音,旋转一个线程等待,然后锁定互斥锁,然后执行AudioServicesPlaySystemSound。只需为每个连续播放的声音启动一个线程,并使用pthread mutex延迟每个连续线程,直到AudioServicesAndSystemSoundCompletion函数解锁该互斥锁。像冠军一样工作


这是21世纪的嵌入式编程:抛出线程的问题

问题:如何指定要播放的.m4a文件?[自我玩家玩];等待所有人完成播放?@DouglasK.Bell我已经编辑了我的帖子,展示了如何从应用程序包中检索m4a并播放它。使用此代码,您只需为所有要使用的m4a创建AVPlayerItems,将它们添加到队列播放器的数组中,然后当您调用play时,队列播放器将开始播放第一部分,然后继续播放队列中的每首歌曲。我得到了AVQueuePlayer的未知类型,即使我将AVFoundation类添加到了我的xcode4目标中。嗯。我忘了进口。抱歉。@DouglasK.Bell您是否已将框架导入控制器?将导入添加到头文件的顶部将消除此错误。注意:我正在使用iPhone语音备忘录创建.m4a声音。使用AudioServicesPlaySystemSound方法可以播放这些内容。
#import <AVFoundation/AVFfoundation.h>


//AVPlayerItem *item1 = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"myFileName" ofType:@"m4a"]]];
AVPlayerItem *item1 = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:@"2"] ofType:@"mp3"]]];

AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:item1, nil]];

[player play];
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    MPMusicPlayerController *player = [MPMusicPlayerController applicationMusicPlayer];
    [player setQueueWithItemCollection:mediaItemCollection];
    [player play];
}