2016-01-13 72 views
0

我把歌曲變成一個表中的所有觀點繼續比賽,當我在詳細頁中工作中發揮的歌曲,當我回來的歌曲停,我想在所有視圖如何使音樂在

繼續發揮

的ViewController

if let audio=NSBundle.mainBundle().pathForResource(navigationItem.title , ofType: "mp3") { 
    let url=NSURL (fileURLWithPath: audio) 

    do { 
     player = try AVAudioPlayer (contentsOfURL: url) 
    } 

    catch{"erorr"}  

    player.play()    
} else { 
    print("erorr") 
} 

tableViewController

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.performSegueWithIdentifier("indeaa", sender: tableView) 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "indeaa" { 
     let vc = segue.destinationViewController as UIViewController 
     let indexpath:NSIndexPath = tableaa.indexPathForSelectedRow! 
     vc.title = arr[indexpath.row] 
    } 
} 
+0

因此,爲了澄清,您有視圖A繼續查看B.視圖B播放音頻,並且當您導航回視圖A時,音頻停止。那是對的嗎? – kpsharp

+0

是........... –

+0

查看下面的@toast的答案。這是你想要的。 – kpsharp

回答

1

的玩家可能得到釋放的觀點。您需要讓您的播放器可以訪問所有細節視圖並引用該單個對象。如果你正在尋找一個簡單的解決方案,只需創建一個單他們都可以參考:

class AudioPlayer{ 

    static let sharedInstance = new AudioPlayer(); 

    var player : AVAudioPlayer 

    func playAudio(url : NSURL){ 

    //see if player is playing, stop and release, setup new player 

    } 

} 
從您的詳細信息視圖

然後就叫

AudioPlayer.sharedInstance.playAudio(url) 

注意這不是測試 - 但只是顯示了基本的想法。