{"id":6070,"date":"2022-06-09T15:54:43","date_gmt":"2022-06-09T07:54:43","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6070"},"modified":"2022-06-09T15:54:43","modified_gmt":"2022-06-09T07:54:43","slug":"swift-%e4%bd%bf%e7%94%a8avplayer%e5%88%b6%e4%bd%9c%e4%b8%80%e4%b8%aa%e9%9f%b3%e4%b9%90%e6%92%ad%e6%94%be%e5%99%a81%ef%bc%88%e5%b8%a6%e6%92%ad%e6%94%be%e6%97%b6%e9%97%b4%e5%92%8c%e6%92%ad%e6%94%be","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6070","title":{"rendered":"Swift &#8211; \u4f7f\u7528AVPlayer\u5236\u4f5c\u4e00\u4e2a\u97f3\u4e50\u64ad\u653e\u56681\uff08\u5e26\u64ad\u653e\u65f6\u95f4\u548c\u64ad\u653e\u8fdb\u5ea6\uff09"},"content":{"rendered":"\n<p>\u8fc7\u53bb\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong> Media Player<\/strong> \u6846\u67b6 <strong>MPMoviePlayerController <\/strong>\u6765\u64ad\u653e\u89c6\u9891\u3001\u97f3\u9891\u3002\u4f46\u81ea<strong> iOS9.0 <\/strong>\u8d77\uff0c\u8fd9\u4e2a\u4fbf\u88ab\u5e9f\u9664\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u4fbf\u662f <strong>AVFoundation <\/strong>\u6846\u67b6\u7684 <strong>AVPlayer<\/strong>\u3002<br><\/p>\n\n\n\n<p>\u8fc7\u53bb\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong> Media Player<\/strong> \u6846\u67b6 <strong>MPMoviePlayerController <\/strong>\u6765\u64ad\u653e\u89c6\u9891\u3001\u97f3\u9891\u3002\u4f46\u81ea<strong> iOS9.0 <\/strong>\u8d77\uff0c\u8fd9\u4e2a\u4fbf\u88ab\u5e9f\u9664\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u4fbf\u662f <strong>AVFoundation <\/strong>\u6846\u67b6\u7684 <strong>AVPlayer<\/strong>\u3002<br><\/p>\n\n\n\n<p><strong>1\uff0cAVPlayer\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<ul><li>\uff081\uff09<strong>AVPlayer <\/strong>\u53ef\u4ee5\u7528\u6765\u64ad\u653e\u89c6\u9891\uff0c\u4e5f\u53ef\u4ee5\u64ad\u653e\u4efb\u4f55 <strong>iOS <\/strong>\u652f\u6301\u7684\u97f3\u9891\u3002 <\/li><li>\uff082\uff09<strong>AVPlayer <\/strong>\u65e2\u53ef\u4ee5\u64ad\u653e\u672c\u5730\u97f3\u9891\uff0c\u53ef\u4ee5\u64ad\u653e\u7f51\u7edc\u97f3\u9891\uff08\u5728\u7ebf\u97f3\u9891\uff09\u3002 <\/li><li>\uff083\uff09\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u64ad\u653e\u8fdc\u7a0b\u97f3\u9891\uff0c<strong>AVPlayer <\/strong>\u540c\u6837\u662f\u5168\u90e8\u52a0\u8f7d\u5230\u672c\u5730\u540e\u624d\u5f00\u59cb\u64ad\u653e\uff0c\u800c\u4e0d\u662f\u4ee5\u6d41\u5a92\u4f53\u7684\u5f62\u5f0f\u64ad\u653e\u3002<\/li><\/ul>\n\n\n\n<p><strong>2\uff0c\u6548\u679c\u56fe<\/strong><\/p>\n\n\n\n<ul><li>\uff081\uff09\u4e0b\u9762\u4f7f\u7528\u00a0<strong>AVPlayer <\/strong>\u5236\u4f5c\u4e00\u4e2a\u97f3\u4e50\u64ad\u653e\u5668\u3002 <\/li><li>\uff082\uff09\u70b9\u51fb\u6309\u94ae\u53ef\u4ee5\u65f6\u97f3\u4e50\u5728\u201c<strong>\u64ad\u653e<\/strong>\u201d\u548c\u201c<strong>\u6682\u505c<\/strong>\u201d\u4e24\u4e2a\u72b6\u6001\u95f4\u5207\u6362\u3002 <\/li><li>\uff083\uff09\u64ad\u653e\u8fc7\u7a0b\u4e2d\u8fdb\u5ea6\u6761\u548c\u65c1\u8fb9\u7684\u6807\u7b7e\u4f1a\u5b9e\u65f6\u663e\u793a\u5f53\u524d\u7684\u8fdb\u5ea6\u3002 <\/li><li>\uff084\uff09\u8fdb\u5ea6\u6761\u6ed1\u5757\u53ef\u4ee5\u81ea\u7531\u62d6\u52a8\uff0c\u5e76\u64ad\u653e\u5bf9\u5e94\u65f6\u95f4\u70b9\u7684\u97f3\u4e50\u3002<br><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-15.png\" alt=\"\" class=\"wp-image-6071\" width=\"447\" height=\"272\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-15.png 654w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-15-300x183.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-15-230x140.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-15-350x213.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-15-480x292.png 480w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3\uff0c\u6837\u4f8b\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\nimport AVFoundation\n \nclass ViewController: UIViewController {\n     \n    \/\/\u64ad\u653e\u6309\u94ae\n    @IBOutlet weak var playButton: UIButton!\n     \n    \/\/\u53ef\u62d6\u52a8\u7684\u8fdb\u5ea6\u6761\n    @IBOutlet weak var playbackSlider: UISlider!\n     \n    \/\/\u5f53\u524d\u64ad\u653e\u65f6\u95f4\u6807\u7b7e\n    @IBOutlet weak var playTime: UILabel!\n     \n    \/\/\u64ad\u653e\u5668\u76f8\u5173\n    var playerItem:AVPlayerItem?\n    var player:AVPlayer?\n     \n    override func viewDidLoad() {\n        super.viewDidLoad()\n         \n        \/\/\u521d\u59cb\u5316\u64ad\u653e\u5668\n        let url = URL(string: \"http:\/\/www.hangge.com\/music.mp3\")\n        playerItem = AVPlayerItem(url: url!)\n        player = AVPlayer(playerItem: playerItem!)\n         \n        \/\/\u8bbe\u7f6e\u8fdb\u5ea6\u6761\u76f8\u5173\u5c5e\u6027\n        let duration : CMTime = playerItem!.asset.duration\n        let seconds : Float64 = CMTimeGetSeconds(duration)\n        playbackSlider!.minimumValue = 0\n        playbackSlider!.maximumValue = Float(seconds)\n        playbackSlider!.isContinuous = false\n         \n        \/\/\u64ad\u653e\u8fc7\u7a0b\u4e2d\u52a8\u6001\u6539\u53d8\u8fdb\u5ea6\u6761\u503c\u548c\u65f6\u95f4\u6807\u7b7e\n        player!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1),\n                                        queue: DispatchQueue.main) { (CMTime) -> Void in\n            if self.player!.currentItem?.status == .readyToPlay {\n                \/\/\u66f4\u65b0\u8fdb\u5ea6\u6761\u8fdb\u5ea6\u503c\n                let currentTime = CMTimeGetSeconds(self.player!.currentTime())\n                self.playbackSlider!.value = Float(currentTime)\n                 \n                \/\/\u4e00\u4e2a\u5c0f\u7b97\u6cd5\uff0c\u6765\u5b9e\u73b000\uff1a00\u8fd9\u79cd\u683c\u5f0f\u7684\u64ad\u653e\u65f6\u95f4\n                let all:Int=Int(currentTime)\n                let m:Int=all % 60\n                let f:Int=Int(all\/60)\n                var time:String=\"\"\n                if f&lt;10{\n                    time=\"0\\(f):\"\n                }else {\n                    time=\"\\(f)\"\n                }\n                if m&lt;10{\n                    time+=\"0\\(m)\"\n                }else {\n                    time+=\"\\(m)\"\n                }\n                \/\/\u66f4\u65b0\u64ad\u653e\u65f6\u95f4\n                self.playTime!.text=time\n            }\n        }\n    }\n     \n    \/\/\u64ad\u653e\u6309\u94ae\u70b9\u51fb\n    @IBAction func playButtonTapped(_ sender: Any) {\n        \/\/\u6839\u636erate\u5c5e\u6027\u5224\u65ad\u5f53\u5929\u662f\u5426\u5728\u64ad\u653e\n        if player?.rate == 0 {\n            player!.play()\n            playButton.setTitle(\"\u6682\u505c\", for: .normal)\n        } else {\n            player!.pause()\n            playButton.setTitle(\"\u64ad\u653e\", for: .normal)\n        }\n    }\n     \n    \/\/\u62d6\u52a8\u8fdb\u5ea6\u6761\u6539\u53d8\u503c\u65f6\u89e6\u53d1\n    @IBAction func playbackSliderValueChanged(_ sender: Any) {\n        let seconds : Int64 = Int64(playbackSlider.value)\n        let targetTime:CMTime = CMTimeMake(seconds, 1)\n        \/\/\u64ad\u653e\u5668\u5b9a\u4f4d\u5230\u5bf9\u5e94\u7684\u4f4d\u7f6e\n        player!.seek(to: targetTime)\n        \/\/\u5982\u679c\u5f53\u524d\u65f6\u6682\u505c\u72b6\u6001\uff0c\u5219\u81ea\u52a8\u64ad\u653e\n        if player!.rate == 0\n        {\n            player?.play()\n            playButton.setTitle(\"\u6682\u505c\", for: .normal)\n        }\n    }\n     \n    \/\/\u9875\u9762\u663e\u793a\u65f6\u6dfb\u52a0\u6b4c\u66f2\u64ad\u653e\u7ed3\u675f\u901a\u77e5\u76d1\u542c\n    override func viewWillAppear(_ animated: Bool) {\n        NotificationCenter.default.addObserver(self, selector: #selector(finishedPlaying),\n               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)\n    }\n     \n    \/\/\u9875\u9762\u6d88\u5931\u65f6\u53d6\u6d88\u6b4c\u66f2\u64ad\u653e\u7ed3\u675f\u901a\u77e5\u76d1\u542c\n    override func viewWillDisappear(_ animated: Bool) {\n        NotificationCenter.default.removeObserver(self)\n    }\n     \n    \/\/\u6b4c\u66f2\u64ad\u653e\u5b8c\u6bd5\n    @objc func finishedPlaying(myNotification:NSNotification) {\n        print(\"\u64ad\u653e\u5b8c\u6bd5!\")\n        let stopedPlayerItem = myNotification.object as! AVPlayerItem\n        stopedPlayerItem.seek(to: kCMTimeZero, completionHandler: nil)\n        playButton.setTitle(\"\u64ad\u653e\", for: .normal)\n    }\n     \n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fc7\u53bb\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Media Player \u6846\u67b6 MPMoviePlayerController \u6765\u64ad\u653e\u89c6\u9891\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6070"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6070"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6070\/revisions"}],"predecessor-version":[{"id":6072,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6070\/revisions\/6072"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}