2011-03-29 34 views

回答

0

嘗試類似這樣的事情。注意-1告訴音樂無限重複:

導入此文件在你的文件的頂部:

#import <AVFoundation/AVFoundation.h> 

然後使用它:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:resourcePath ofType:@"caf"]; 

NSError *activationError = nil; 
NSError *audioPlayerInitError = nil; 
[[AVAudioSession sharedInstance] setActive: YES error:&activationError]; 

NSURL *newURL = [NSURL fileURLWithPath:soundFilePath]; 
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError]; 

[newPlayer prepareToPlay]; 
[newPlayer setVolume:.8]; 
[newPlayer setNumberOfLoops:-1]; // -1 means play indefintely 
[newPlayer setDelegate: self]; 
[newPlayer play]; 
[newPlayer release]; 

然後停止它,你會做:

if ([newPlayer isPlaying]) { 
    [newPlayer stop]; 
} 
+0

請你解釋AVAudioPlayer。我理解它是一個類的引用,但我的代碼告訴我它的未聲明。以及AVAudioSession。 – luca590 2011-03-30 15:41:32

+0

我的歉意。我忘了告訴你導入AVFoundation標頭。我編輯了我的答案。 – FreeAsInBeer 2011-03-30 15:46:37

+0

非常感謝你 – luca590 2011-03-30 15:55:22