0

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]; 
4

1 に答える 1

0

前回の投稿の回答に対するコメントで、これを修正する方法を説明しました。これが機能しない理由の1つは、複数のアイテムarrayWithObjects:を必要とする1つのアイテムを渡すためです。これが機能しない原因となる可能性のあるもう1つの理由は、これをシミュレーターで実行しようとする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];
        }
    }
}
于 2012-08-28T13:55:16.997 に答える