{"id":6073,"date":"2022-06-09T15:59:54","date_gmt":"2022-06-09T07:59:54","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6073"},"modified":"2022-06-09T16:00:40","modified_gmt":"2022-06-09T08:00:40","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%a82%ef%bc%88%e5%90%8e%e5%8f%b0%e6%92%ad%e6%94%be%e3%80%81%e6%93%8d%e4%bd%9c%e3%80%81","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6073","title":{"rendered":"Swift &#8211; \u4f7f\u7528AVPlayer\u5236\u4f5c\u4e00\u4e2a\u97f3\u4e50\u64ad\u653e\u56682\uff08\u540e\u53f0\u64ad\u653e\u3001\u64cd\u4f5c\u3001\u56fe\u7247\u663e\u793a\uff09"},"content":{"rendered":"\n<p>\u5728\u524d\u6587\u4e2d\uff0c\u6211\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 <strong>AVFoundation<\/strong> \u6846\u67b6\u6765\u5236\u4f5c\u4e00\u4e2a\u7b80\u5355\u7684\u97f3\u9891\u64ad\u653e\u5668\uff08<a rel=\"noreferrer noopener\" href=\"https:\/\/www.hangge.com\/blog\/cache\/detail_1668.html\" target=\"_blank\">\u70b9\u51fb\u67e5\u770b<\/a>\uff09\u3002\u4f46\u8fd9\u4e2a\u64ad\u653e\u5668\u4e0d\u652f\u6301\u540e\u53f0\u64ad\u653e\uff0c\u7a0b\u5e8f\u9000\u5230\u540e\u53f0\u65f6\u97f3\u4e50\u5c31\u4f1a\u505c\u6b62\u64ad\u653e\u3002 \u672c\u6587\u63a5\u7740\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u540e\u53f0\u64ad\u653e\u529f\u80fd\u3002<br><\/p>\n\n\n\n<p><strong>1\uff0c\u6548\u679c\u56fe<\/strong><\/p>\n\n\n\n<ul><li>\uff081\uff09\u8fd0\u884c\u7a0b\u5e8f\u5e76\u64ad\u653e\u97f3\u4e50\u3002\u8fd9\u65f6\u6211\u4eec\u8fd4\u56de\u684c\u9762\u6216\u8005\u5173\u95ed\u5c4f\u5e55\uff0c\u4f1a\u53d1\u73b0\u97f3\u4e50\u4ecd\u7136\u5728\u64ad\u653e\u3002<\/li><li>\uff082\uff09\u5728\u9501\u5c4f\u754c\u9762\u4e0a\uff0c\u4f1a\u663e\u793a\u5f53\u524d\u7684\u6b4c\u66f2\u4fe1\u606f\u3001\u4e13\u8f91\u56fe\u7247\u3001\u5f53\u524d\u8fdb\u5ea6\u7b49\u3002\u540c\u65f6\u8fd8\u63d0\u4f9b\u76f8\u5173\u7684\u63a7\u5236\u6309\u94ae\u4f9b\u6211\u4eec\u4f7f\u7528\u3002<\/li><li>\uff083\uff09\u540c\u6837\u7684\uff0c\u5728\u4e0a\u62c9\u7684\u97f3\u4e50\u63a7\u5236\u9762\u677f\u4e2d\uff0c\u4e5f\u4f1a\u663e\u793a\u76f8\u5173\u4fe1\u606f\uff0c\u5e76\u5141\u8bb8\u6211\u4eec\u8fdb\u884c\u76f8\u5173\u64cd\u4f5c\u3002<br><\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-1024x507.png\" alt=\"\" class=\"wp-image-6074\" width=\"578\" height=\"285\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-1024x507.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-300x149.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-768x380.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-830x411.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-350x173.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16-480x238.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-16.png 1430w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2\uff0c\u5b9e\u73b0\u6b65\u9aa4<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u4e3a\u4e86\u8ba9\u64ad\u653e\u5668\u80fd\u5728\u540e\u53f0\u6301\u7eed\u64ad\u653e\uff0c\u6211\u4eec\u9700\u8981\u5c06 <strong>Targets<\/strong> -&gt; <strong>Capabilities<\/strong> -&gt;<strong>BackgroundModes<\/strong> \u8bbe\u4e3a <strong>ON<\/strong>\uff0c\u540c\u65f6\u52fe\u9009\u201c<strong>Audio, AirPlay, and Picture in Picture<\/strong>\u201d\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-1024x510.png\" alt=\"\" class=\"wp-image-6075\" width=\"527\" height=\"262\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-1024x510.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-300x149.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-768x382.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-830x413.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-230x115.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-350x174.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17-480x239.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/06\/\u56fe\u7247-17.png 1418w\" sizes=\"(max-width: 527px) 100vw, 527px\" \/><\/figure><\/div>\n\n\n\n<p>\uff082\uff09\u540c\u65f6\u8fd8\u8981\u5728 <strong>AppDelegate.swift<\/strong> \u4e2d\u6ce8\u518c\u540e\u53f0\u64ad\u653e\u3002<\/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 \n@UIApplicationMain\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n \n    var window: UIWindow?\n \n \n    func application(_ application: UIApplication, didFinishLaunchingWithOptions\n        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {\n         \n        \/\/ \u6ce8\u518c\u540e\u53f0\u64ad\u653e\n        let session = AVAudioSession.sharedInstance()\n        do {\n            try session.setActive(true)\n            try session.setCategory(AVAudioSessionCategoryPlayback)\n        } catch {\n            print(error)\n        }\n         \n        return true\n    }\n \n    func applicationWillResignActive(_ application: UIApplication) {\n    }\n \n    func applicationDidEnterBackground(_ application: UIApplication) {\n    }\n \n    func applicationWillEnterForeground(_ application: UIApplication) {\n    }\n \n    func applicationDidBecomeActive(_ application: UIApplication) {\n    }\n \n    func applicationWillTerminate(_ application: UIApplication) {\n    }\n}<\/pre>\n\n\n\n<p>\uff083\uff09<strong>ViewController.swift<\/strong>\uff08\u4e3b\u89c6\u56fe\u4ee3\u7801\uff0c\u9ec4\u8272\u90e8\u5206\u4e3a\u65b0\u589e\u7684\u4ee3\u7801\uff09<\/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\nimport MediaPlayer\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:\/\/mxd.766.com\/sdo\/music\/data\/3\/m10.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 &amp;&amp; self.player?.rate != 0{\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                \/\/\u8bbe\u7f6e\u540e\u53f0\u64ad\u653e\u663e\u793a\u4fe1\u606f\n                self.setInfoCenterCredentials(playbackState: 1)\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            \/\/\u540e\u53f0\u64ad\u653e\u663e\u793a\u4fe1\u606f\u8fdb\u5ea6\u505c\u6b62\n            setInfoCenterCredentials(playbackState: 0)\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\u76f8\u5173\u901a\u77e5\u76d1\u542c\n    override func viewWillAppear(_ animated: Bool) {\n        \/\/\u64ad\u653e\u5b8c\u6bd5\n        NotificationCenter.default.addObserver(self, selector: #selector(finishedPlaying),\n            name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)\n         \n        \/\/\u544a\u8bc9\u7cfb\u7edf\u63a5\u53d7\u8fdc\u7a0b\u54cd\u5e94\u4e8b\u4ef6\uff0c\u5e76\u6ce8\u518c\u6210\u4e3a\u7b2c\u4e00\u54cd\u5e94\u8005\n        UIApplication.shared.beginReceivingRemoteControlEvents()\n        self.becomeFirstResponder()\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        \/\/\u505c\u6b62\u63a5\u53d7\u8fdc\u7a0b\u54cd\u5e94\u4e8b\u4ef6\n        UIApplication.shared.endReceivingRemoteControlEvents()\n        self.resignFirstResponder()\n    }\n     \n    \/\/\u6b4c\u66f2\u64ad\u653e\u5b8c\u6bd5\n    func finishedPlaying(myNotification:NSNotification) {\n        print(\"\u64ad\u653e\u5b8c\u6bd5!\")\n        let stopedPlayerItem: AVPlayerItem = myNotification.object as! AVPlayerItem\n        stopedPlayerItem.seek(to: kCMTimeZero)\n    }\n     \n    \/\/\u662f\u5426\u80fd\u6210\u4e3a\u7b2c\u4e00\u54cd\u5e94\u5bf9\u8c61\n    override var canBecomeFirstResponder: Bool {\n        return true\n    }\n     \n    \/\/ \u8bbe\u7f6e\u540e\u53f0\u64ad\u653e\u663e\u793a\u4fe1\u606f\n    func setInfoCenterCredentials(playbackState: Int) {\n        let mpic = MPNowPlayingInfoCenter.default()\n         \n        \/\/\u4e13\u8f91\u5c01\u9762\n        let mySize = CGSize(width: 400, height: 400)\n        let albumArt = MPMediaItemArtwork(boundsSize:mySize) { sz in\n            return UIImage(named: \"cover\")!\n        }\n         \n        \/\/\u83b7\u53d6\u8fdb\u5ea6\n        let postion = Double(self.playbackSlider!.value)\n        let duration = Double(self.playbackSlider!.maximumValue)\n         \n        mpic.nowPlayingInfo = [MPMediaItemPropertyTitle: \"\u6211\u662f\u6b4c\u66f2\u6807\u9898\",\n                               MPMediaItemPropertyArtist: \"hangge.com\",\n                               MPMediaItemPropertyArtwork: albumArt,\n                               MPNowPlayingInfoPropertyElapsedPlaybackTime: postion,\n                               MPMediaItemPropertyPlaybackDuration: duration,\n                               MPNowPlayingInfoPropertyPlaybackRate: playbackState]\n    }\n     \n    \/\/\u540e\u53f0\u64cd\u4f5c\n    override func remoteControlReceived(with event: UIEvent?) {\n        guard let event = event else {\n            print(\"no event\\n\")\n            return\n        }\n         \n        if event.type == UIEventType.remoteControl {\n            switch event.subtype {\n            case .remoteControlTogglePlayPause:\n                print(\"\u6682\u505c\/\u64ad\u653e\")\n            case .remoteControlPreviousTrack:\n                print(\"\u4e0a\u4e00\u9996\")\n            case .remoteControlNextTrack:\n                print(\"\u4e0b\u4e00\u9996\")\n            case .remoteControlPlay:\n                print(\"\u64ad\u653e\")\n                player!.play()\n            case .remoteControlPause:\n                print(\"\u6682\u505c\")\n                player!.pause()\n                \/\/\u540e\u53f0\u64ad\u653e\u663e\u793a\u4fe1\u606f\u8fdb\u5ea6\u505c\u6b62\n                setInfoCenterCredentials(playbackState: 0)\n            default:\n                break\n            }\n        }\n    }\n \n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u6587\u4e2d\uff0c\u6211\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528 AVFoundation \u6846\u67b6\u6765\u5236\u4f5c\u4e00\u4e2a\u7b80\u5355\u7684\u97f3\u9891\u64ad\u653e\u5668\uff08\u70b9\u51fb\u67e5\u770b\uff09\u3002\u4f46\u8fd9\u4e2a\u64ad\u653e\u5668 [&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\/6073"}],"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=6073"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6073\/revisions"}],"predecessor-version":[{"id":6078,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6073\/revisions\/6078"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}