ios - 在 swift 2.0 中调用另一个 UIViewController 的方法

标签 ios iphone swift

我有一个播放歌曲的应用程序,我有一个外部类,它有一个 playSound 函数和所有需要的变量:

class Song: NSObject {

    var audioPlayer: AVAudioPlayer!
    var deviceCurrentTime: NSTimeInterval!

    func playSong() {

        let sound = NSBundle.mainBundle().pathForResource("song", ofType: "mp3")
        let soundData = NSData(contentsOfFile: sound!)
        //  self.audioPlayer = AVAudioPlayer(data: soundData!) throws

        self.audioPlayer = nil
        do {
            self.audioPlayer = try AVAudioPlayer(data: soundData!)
        }
        catch {
            print("Handle \(error) here")
        }

        let delay:NSTimeInterval = 0.0//100ms
        var playtime:NSTimeInterval
        playtime = audioPlayer.deviceCurrentTime + delay

        self.audioPlayer?.playAtTime(playtime)
        self.audioPlayer?.play()


    }

然后在我的 mainViewController 的 ViewDidLoad 中,我想播放歌曲并从我的 Song 对象访问该方法:

    super.viewDidLoad()

    let vc: NSObject = Song()
    if let myVc = vc as? Song {
        myVc.playSong()
    }

但这并没有播放这首歌...如果我将所有变量从 Song 放到 mainViewControler 中也使用 playSong 方法并在 viewDidLoad 中说 playSong(),它会起作用,但我想将它放在外部类中。

有什么帮助吗? :)

最佳答案

您可以创建一个全局方法。创建一个空的 swift 文件并在其中添加以下代码:

import Foundation
import AVFoundation


var backgroundMusicPlayer = AVAudioPlayer()

func playBackgroundMusic(filename: String) {
    let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
    guard let newURL = url else {
        print("Could not find file: \(filename)")
        return
    }
    do {
        backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: newURL)
        backgroundMusicPlayer.numberOfLoops = -1
        backgroundMusicPlayer.prepareToPlay()
        backgroundMusicPlayer.play()
    } catch let error as NSError {
        print(error.description)
    }
}

现在,当你想播放任何歌曲时,只需这样调用该方法即可:

playBackgroundMusic("yourSong.mp3")

如果你想和你的类(class)一起去,那么你可以这样做:

import Foundation
import AVFoundation

var audioPlayer: AVAudioPlayer!
var deviceCurrentTime: NSTimeInterval!

class Song: NSObject {

    func playSong() {

        let sound = NSBundle.mainBundle().pathForResource("We_Are_One_Ole_Ola_", ofType: "mp3")
        let soundData = NSData(contentsOfFile: sound!)
        //  self.audioPlayer = AVAudioPlayer(data: soundData!) throws

        audioPlayer = nil
        do {
            audioPlayer = try AVAudioPlayer(data: soundData!)
        }
        catch {
            print("Handle \(error) here")
        }

        let delay:NSTimeInterval = 0.0//100ms
        var playtime:NSTimeInterval
        playtime = audioPlayer.deviceCurrentTime + delay

        audioPlayer?.playAtTime(playtime)
        audioPlayer?.play()

    }
}

在其他类中你可以这样使用它:

override func viewDidLoad() {
    super.viewDidLoad()

    let temp = Song()
    temp.playSong()
}

关于ios - 在 swift 2.0 中调用另一个 UIViewController 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052617/

相关文章:

ios - 自定义维度在 Google Analytics 中不起作用

ios - SecItemUpdate() 不断返回 errSecParam,我不明白为什么

php - 如何将字符串和视频发布到 PHP 服务器?

iphone - 应用程序在我的帐户下启动后,我可以将其转移到另一个帐户吗?

iphone - 禁用 tableHeaderView(不要与节标题混淆)滚动

ios - 无法为自定义栏按钮设置标题?

ios - 多种颜色的 CPT 散点图

iphone - 值得注意的字体在 iOS 4.3 中引入并在 < 4.3 上回退到其他字体

ios - xCode Cocoapods 无法构建项目 ios(9.3)

ios - 在选择 CollectionViewCell 时将信息发送到详细 View