2016-05-20 42 views
-3

試圖將下面的代碼objective-c轉換爲Swift,請幫助我這麼做或幫助我使用新代碼播放多聲音***代碼幫助播放多個聲音(目標c)Swift(xcode 7)

- (IBAction)pushButton {  
     NSString *path = [[NSBundle mainBundle] pathForResouce:@"ring" ofType:@"mp3"]; 
     if(theAudio) [theAudio release]; 
     theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
     theAudio.delegate = self; 
     [theAudio play];  
} 

- (IBAction)pushButton1 { 
     NSString *path = [[NSBundle mainBundle] pathForResouce:@"amaze" ofType:@"mp3"]; 
     if(theAudio) [theAudio release]; 
     theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
     theAudio.delegate = self; 
     [theAudio play]; 
} 
+0

**這是不是一個編碼服務**。至少在你身邊顯示出一些努力! –

+0

我從過去2周(每天5小時)開始就是爲了讓它發揮作用。可以這樣做 – Shahdeep

+0

你做了什麼?發佈! –

回答

0

斯威夫特版本: -

let path : String = NSBundle.mainBundle().pathForResource("ring", ofType: "mp3")! 
if(theAudio){ 
    theAudio.release() 
} 
theAudio = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:path), error: &err) 
audioPlayer.delegate = self 
audioPlayer.play() 


func pushButton1(){ 
    let path : String = NSBundle.mainBundle().pathForResource("amaze", ofType: "mp3")! 
    if(theAudio){ 
     theAudio.release() 
    } 
    theAudio = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath:path), error: &err) 
    audioPlayer.delegate = self 
    audioPlayer.play() 

}