2012-05-16 143 views
0

我似乎無法使用AVQueuePlayer播放聲音。該應用程序編譯和運行良好,但我沒有得到任何音頻時調用`imageTouched'方法。誰能幫忙?AVQueuePlayer不能播放聲音

-(void) imageTouched 
{ 
    NSString * path1 = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"]; 
    NSString * path2 = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"]; 
    NSURL *url1 = [NSURL fileURLWithPath:path1]; 
    NSURL *url2 = [NSURL fileURLWithPath:path2]; 

    AVPlayerItem *firstSound = [AVPlayerItem playerItemWithURL:url1]; 
    AVPlayerItem *secondSound = [AVPlayerItem playerItemWithURL:url2]; 

    NSArray *sounds = [NSArray arrayWithObjects:firstSound, secondSound, nil]; 
    AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:sounds]; 

    [player play]; 
    NSLog(@"%d",player.status); 

} 

編輯1:我檢查,看看是否有一個與文件名(的情況下,拼寫等)的任何問題,似乎沒有要。我已經能夠用下面的代碼播放聲音,但我真的需要得到AVQueuePlayer的工作。請幫忙!

NSString *path; 
SystemSoundID soundId; 
path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"]; 
NSURL *url = [NSURL fileURLWithPath:path]; 
AudioServicesCreateSystemSoundID((CFURLRef)objc_unretainedPointer(url), &soundId); 
AudioServicesPlaySystemSound(soundId); 
+0

檢查聲音文件是否實際包含在應用程序包中,並且文件名拼寫正確(包括大小寫)。 – hotpaw2

+0

聲音被複制到我的資源文件夾並且拼寫正確;不幸的是,這不是問題。我的nslog打印0如果這是任何幫助 – garethdn

+0

我應該也提到,我可以播放使用'AudioServicesPlaySystemSound(soundId);'但我想用'AVQueuePlayer'的罰款。我的.h文件中是否有可能丟失了某些東西? – garethdn

回答

0

我意識到這可能由現在太晚了你,但我懷疑的原因是AVQueuePlayer被立即自動釋放,所以不播放聲音。改爲嘗試[[AVQueuePlayer alloc] initWithItems:sounds]

0

我做了幾乎相同的想法,但不是在函數中聲明AVQueuePlayer *player,而是在interface中聲明它。