2015-04-22 87 views
0

有什麼方法可以停止播放音頻嗎?就像一個玩/停止類的東西?在iPhone應用程序中播放/暫停聲音

-(IBAction)PlaySound:(id)sender{ 

audioTitle = [NSString stringWithFormat:@"Audio %@", audioInteger]; 
CFBundleRef mainBundle = CFBundleGetMainBundle(); 
CFURLRef soundfileURLREF; 
soundfileURLREF = CFBundleCopyResourceURL(mainBundle, (CFStringRef) audioTitle, CFSTR ("mp3"), NULL); 
UInt32 SoundID; 
AudioServicesCreateSystemSoundID(soundfileURLREF, &SoundID); 
AudioServicesPlaySystemSound(SoundID); 

} 
+2

[停止聲音在iPhone]可能的重複(http://stackoverflow.com/questions/1512121/stop-sound-in-iphone) – Wez

+0

第一步:停止使用AudioServicesPlaySystemSound並讓自己成爲一個真正的聲音播放器(即AVAudioPlayer)。 – matt

回答

0

這是一個簡單的播放/暫停/停止使用AVAudioPlayer類的例子。

創建打在viewDidLoad中:

var audioPath = NSBundle.mainBundle().pathForResource("bach1", ofType: "mp3") 

    var error : NSError? = nil 

    player = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath!), error: &error) 

裏面播放按鈕:

player.play() 

內暫停按鈕:

player.pause() 

內停止按鈕:

player.stop() 
player.currentTime = 0; 
+0

@ BenHeyderman是否幫助你,還是你需要更多的澄清? – studev18

相關問題