iphone - iOS xcode4 AVQueuePlayer 不播放声音

标签 iphone ios audio 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"]]];

编辑:下面是如何按顺序播放音频文件的示例。

在你的头文件中:
#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];
        }
    }
}

关于iphone - iOS xcode4 AVQueuePlayer 不播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160774/

相关文章:

iphone - 将应用程序的当前版本与应用程序商店中的最新版本进行比较的最简单方法是什么?

iphone - 如何在数据导入 CoreData 时显示 progressBar?

ios - 尽管没有错误或没有崩溃或卡住,但 Segue 未加载

audio - 声音没有播放两次

android - 在Android中处理AudioManager的最佳方法

iphone - 将 NSDate 转换为 NSString

ios - 如何在 xcode 8 中创建没有开发者帐户的 ipa 文件

objective-c - 将 CAF 音频容器更改为 mp4?

JavaScript - 如何选择音频播放?

iphone - 为什么 Xcode 不显示 UIWebView 的智能感知