iOS的X code4 AVQueuePlayer不播放声音 [英] iOS xcode4 AVQueuePlayer not playing sound

查看:243
本文介绍了iOS的X code4 AVQueuePlayer不播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打使用AVQueuePlayer,iPhone4上一个健全的.m4a文件,但没有任何声音。
如果我尝试AudioServicesPlaySystemSound相同的文件,一切工作正常。

这里的code。

 (AVFoundation框架安装。)    #进口< AVFoundation / AVFoundation.h> - (无效)play_sound_file:(* NSString的)sound_file_m4a
{
    的printf(\\ n play_sound_file 1:'%s'的,[sound_file_m4a UTF8字符串]);
        AVPlayerItem * sound_file = [[AVPlayerItem页头] initWithURL:[NSURL URLWithString:[一个NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@M4A]]];
        AVQueuePlayer *玩家= [AVQueuePlayer queuePlayerWithItems:[NSArray的arrayWithObjects:sound_file,无]];        [播放器播放];        返回;
}

最新code作为12年8月28日的...........
(注:这两个声音文件与AudioServicesPlaySystemSound工作)

  AVPlayerItem * sound_file1 = [[AVPlayerItem页头] initWithURL:[NSURL fileURLWithPath:[一个NSBundle mainBundle] pathForResource:[[的NSString页头] initWithFormat:@别动] ofType:// @ M4A]]];AVPlayerItem * sound_file2 = [[AVPlayerItem页头] initWithURL:[NSURL fileURLWithPath:[一个NSBundle mainBundle] pathForResource:[[的NSString页头] initWithFormat:@preSS走] ofType:@M4A]]];AVQueuePlayer *玩家= [AVQueuePlayer queuePlayerWithItems:[NSArray的arrayWithObjects:sound_file1,sound_file2,无]];[播放器播放];


解决方案

我解释了如何在您的最后一个职位上我的回答评论解决这个问题。其中一个可能无法正常工作的原因是因为你传递一个项目到 arrayWithObjects:这需要一个以上的项目。这可能会导致此不工作的另一件事是,如果你试图在模拟器中运行这个,还有 AVQueuePlayer 之间的模拟器已知的兼容性问题。

还有:

替换

  AVPlayerItem * sound_file = [[AVPlayerItem页头] initWithURL:[NSURL URLWithString:[一个NSBundle mainBundle] pathForResource:sound_file_m4a ofType:@M4A]]];

通过

  AVPlayerItem * sound_file = [[AVPlayerItem页头] initWithURL:[NSURL fileURLWithPath:[一个NSBundle mainBundle] pathForResource:[[的NSString页头] initWithFormat:@sound_file_m4a] ofType:@ M4A]]];

编辑:这里有一个如何按顺序播放音频文件的例子

在你的头文件:

 #进口< AVFoundation / AVFoundation.h>AVAudioPlayer *数据;
NSArray的* arrayOfTracks;
NSUInteger currentTrackNumber;

然后在您的实现文件:

   - (无效)viewDidLoad中
{
    [超级viewDidLoad中];
    arrayOfTracks = [[NSArray的页头] initWithObjects:@1,@2,@3,零]; //不包括在此文件的扩展名
    currentTrackNumber = 0;
}
- (无效)startPlaying
{
    如果(数据){
        [数据站]
        // [数据发布]如果项目没有使用自动引用计数
        数据=零;
    }
    数据= [[AVAudioPlayer页头] initWithContentsOfURL:[NSURL fileURLWithPath:[一个NSBundle mainBundle] pathForResource:[[的NSString页头] initWithString:[arrayOfTracks objectAtIndex:currentTrackNumber] ofType:@M4A]]错误:NULL];
    data.delegate =自我;
    [数据播放];
} - (无效)audioPlayerDidFinishPlaying:(AVAudioPlayer *)成功的球员:(BOOL)标志
{
    如果(标志){
        如果(currentTrackNumber百分比抑制率arrayOfTracks计数] - 1){
            currentTrackNumber ++;
            如果(数据){
                [数据站]
                // [数据发布]如果项目没有使用自动引用计数
                数据=零;
            }
            数据= [[AVAudioPlayer页头] initWithContentsOfURL:[NSURL fileURLWithPath:[一个NSBundle mainBundle] pathForResource:[[的NSString页头] initWithString:[arrayOfTracks objectAtIndex:currentTrackNumber] ofType:@M4A]]错误:NULL];
            data.delegate =自我;
            [数据播放];
        }
    }
}

I'm trying to play a .m4a sound file using AVQueuePlayer, on iPhone4, but there is no sound. If I try AudioServicesPlaySystemSound with the same file, all works okay.

Here's the code.

(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;
}

LATEST CODE AS OF 8/28/12........... (Note: both sound files work with 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]; 

解决方案

I explained how to fix this in a comment on my answer in your last post. One of the reasons this may not be working is because you are passing one item to arrayWithObjects: which requires more than one item. Another thing that may be causing this not to work is if you are attempting to run this in the simulator, there are known compatibility issues between AVQueuePlayer and the simulator.

Also:

Replace

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

With

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

EDIT: Here's an example of how to play audio files sequentially.

In your header file:

#import <AVFoundation/AVFoundation.h>

AVAudioPlayer *data;
NSArray *arrayOfTracks;
NSUInteger currentTrackNumber;

Then in your implementation file:

- (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];
        }
    }
}

这篇关于iOS的X code4 AVQueuePlayer不播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆