2013-02-17 168 views
0
- (void)viewDidLoad { 
[super viewDidLoad]; 
[audioPlayer stop]; 
NSString *Path = [[NSBundle mainBundle] pathForResource:@"Keep the Shoes Moving" ofType: @"m4a"]; 
NSURL *URL = [[NSURL alloc] initFileURLWithPath: Path]; 
NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:&error]; 
audioPlayer.numberOfLoops = -1; 
[audioPlayer play]; 
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; } 

這會發生什麼,每次我回到這個viewController它播放另一個軌道。那麼我有兩個音軌互相播放。我將如何得到這個停止?停止播放目標c的聲音?

回答

2

將您的AVAudioPlayer實例存儲在一個屬性中,並在您希望它停止播放時將其稱爲stop方法。

+0

非常感謝您的幫助。你可以請進一步解釋,因爲我是一個開始的編碼器。我知道AVAudioPlayer是什麼,但是通過將其稱爲停止方法意味着什麼。你的意思是「[audioPlayer停止];」?如果這是你的意思,我在viewDidLoad的開頭稱之爲。我應該改變它嗎?謝謝你的幫助。 – user1807869 2013-02-17 20:45:28

+0

你有沒有設法解決這個問題?我有同樣的問題,'stop'永遠不會被調用,因爲我已經在這裏解釋它:http://stackoverflow.com/questions/20164873/avaudioplayer-is-always-nill-and-its-isplaying-property-is -always-false-the-fil 我即將聲明一個全局變量並使用它,但我很樂意爲此找到更好的解決方案。 – Neeku 2013-11-25 16:08:24