录制音频流中的音频

时间:2016-05-05 13:20:33

标签: ios swift audio avfoundation avaudiorecorder

我正在构建一个播放音频流的应用。以下是我的代码。

class ViewController: UIViewController {

    private var player: AVPlayer!
    private var kvoContext: UInt8 = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        // Streaming
        let streamURL = NSURL(string: "http://adsl.ranfm.lk/")!
        player = AVPlayer(URL: streamURL)
    }

    @IBAction func didTapPlayButton(sender: UIButton) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(playerDidReachEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
        player.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: &kvoContext)

        player.play()
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if context == &kvoContext {
            print("Change at \(keyPath) for \(object)")
        }

        if player.status == .Failed {
            print("AVPlayer Failed")
        }

        if player.status == .ReadyToPlay {
            print("Ready to play")
            player.play()
        }

        if player.status == .Unknown {
            print("AVPlayer Unknown")
        }
    }

    func playerDidReachEnd(notification: NSNotification) {
        print("Reached the end of file")
    }
}

这会成功播放流。现在我想添加记录此流中音频的功能。

根据我能找到的内容,AVAudioRecorder类仅用于通过麦克风录制音频。有没有办法从流中录制音频?

0 个答案:

没有答案