在我的 ViewController、Inbox 中,我想从我的 App Delegate 调用方法 playAudio()。
这是方法。
func playAudio(){
let nowPlaying = MPNowPlayingInfoCenter.defaultCenter()
let albumArtWork = MPMediaItemArtwork(image: UIImage(named: "testImage.jpg")!)
nowPlaying.nowPlayingInfo = [MPMediaItemPropertyTitle:"Sonnnngggg",
MPMediaItemPropertyArtist:"Arrrtttiiiissstttt",
MPMediaItemPropertyArtwork:albumArtWork]
audioPlayer.play()
}
我想从我的 App Delegate 中调用它...
if event!.subtype == UIEventSubtype.RemoteControlPlay {
print("received remote play")
//audioPlayer.play()
Inbox.playAudio()
}
一个问题是,因为我有一个audioPlayer 和我的Inbox ViewController 中的所有内容,并且它可能已经在播放音频,所以创建一个Inbox 实例来调用playAudio() 是没有意义的。因此,如果 Swift 中有任何解决方法,请告诉我。
谢谢, 利亚姆
最佳答案
在 AppDelegate 中更改为:
if event!.subtype == UIEventSubtype.RemoteControlPlay {
print("received remote play")
//audioPlayer.play()
NSNotificationCenter.defaultCenter().postNotificationName("RemotePlayPressed", object: self)
}
然后在收件箱 View Controller 的 viewDidLoad 添加:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playAudio", name: "RemotePlayPressed", object: nil)
同时补充:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
到收件箱 View Controller 。
关于ios - 从 App Delegate 调用 ViewController 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711585/