{"id":3811,"date":"2021-08-19T16:07:21","date_gmt":"2021-08-19T08:07:21","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3811"},"modified":"2021-09-09T16:15:03","modified_gmt":"2021-09-09T08:15:03","slug":"swift-%e7%9b%91%e5%90%ac%e9%9f%b3%e9%87%8f%e6%8c%89%e9%92%ae","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3811","title":{"rendered":"Swift \u76d1\u542c\u97f3\u91cf\u6309\u94ae"},"content":{"rendered":"\n<p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728IOS\u7a0b\u5e8f\u4e2d\uff0c\u76d1\u542c\u97f3\u91cf\u6309\u4e0b\u7684\u52a8\u4f5c\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ul><li>\u5728\u540e\u53f0\u6a21\u5f0f\u4e2d\uff0c\u6253\u5f00Audio\u5f00\u5173<\/li><\/ul>\n\n\n\n<p>\u53ea\u6709\u6253\u5f00\u8fd9\u4e2a\u5f00\u5173\uff0c\u624d\u80fd\u5728\u9501\u5c4f\u6a21\u5f0f\u4e0b\uff0c\u76d1\u542c\u5230\u97f3\u91cf\u6309\u94ae<\/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\/2021\/08\/\u56fe\u7247-63-1024x606.png\" alt=\"\" class=\"wp-image-3816\" width=\"597\" height=\"353\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-1024x606.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-300x177.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-768x454.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-1536x909.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-830x491.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-230x136.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-350x207.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63-480x284.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-63.png 1670w\" sizes=\"(max-width: 597px) 100vw, 597px\" \/><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>SceneDelegate.swift \u4ee3\u7801<\/li><\/ul>\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 SwiftUI\nimport AudioToolbox\n\n\nclass SceneDelegate: UIResponder, UIWindowSceneDelegate {\n\n    var window: UIWindow?\n\n    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {\n        \/\/ Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.\n        \/\/ If using a storyboard, the `window` property will automatically be initialized and attached to the scene.\n        \/\/ This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).\n\n        \/\/ Create the SwiftUI view that provides the window contents.\n        let contentView = ContentView()\n\n        \/\/ Use a UIHostingController as window root view controller.\n        if let windowScene = scene as? UIWindowScene {\n            let window = UIWindow(windowScene: windowScene)\n            window.rootViewController = UIHostingController(rootView: contentView)\n            self.window = window\n            window.makeKeyAndVisible()\n        }\n\n        \/\/ \u8fd9\u91cc\u9762\u76d1\u542c\u97f3\u91cf\u6309\u94ae\n        NotificationCenter.default.addObserver(self, selector: #selector(self.changeVolumSlider), name: NSNotification.Name(rawValue: \"AVSystemController_SystemVolumeDidChangeNotification\"), object: nil)\n        UIApplication.shared.beginReceivingRemoteControlEvents()\n}\n    \n   \/\/ \u5f53\u97f3\u91cf\u6309\u94ae\u6309\u4e0b\u65f6\uff0c\u4f1a\u89e6\u53d1\u4e0b\u9762\u7684\u65b9\u6cd5\n    @objc func changeVolumSlider(notifi: NSNotification) {\n        \n        let reason = (notifi.userInfo?[\"AVSystemController_AudioVolumeChangeReasonNotificationParameter\"] as? String) ?? \"\"\n        if reason == \"ExplicitVolumeChange\" {\n            for checkPointInfo in checkPointInfoCommonList where checkPointInfo.noticeStatus == .fire {\n                checkPointInfo.noticeStatus = .stop\n                checkPointInfo.stopAlert()\n            }\n        }\n    }\n\nfunc sceneDidDisconnect(_ scene: UIScene) {\n    \/\/ Called as the scene is being released by the system.\n    \/\/ This occurs shortly after the scene enters the background, or when its session is discarded.\n    \/\/ Release any resources associated with this scene that can be re-created the next time the scene connects.\n    \/\/ The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).\n}\n\nfunc sceneDidBecomeActive(_ scene: UIScene) {\n    \/\/ Called when the scene has moved from an inactive state to an active state.\n    \/\/ Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.\n}\n\nfunc sceneWillResignActive(_ scene: UIScene) {\n    \/\/ Called when the scene will move from an active state to an inactive state.\n    \/\/ This may occur due to temporary interruptions (ex. an incoming phone call).\n}\n\nfunc sceneWillEnterForeground(_ scene: UIScene) {\n    \/\/ Called as the scene transitions from the background to the foreground.\n    \/\/ Use this method to undo the changes made on entering the background.\n}\n\nfunc sceneDidEnterBackground(_ scene: UIScene) {\n    \/\/ Called as the scene transitions from the foreground to the background.\n    \/\/ Use this method to save data, release shared resources, and store enough scene-specific state information\n    \/\/ to restore the scene back to its current state.\n}\n\n}\n\n\n<\/pre>\n\n\n\n<ul><li><strong>\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u53ea\u6709\u5728\u97f3\u9891\u6587\u4ef6\u64ad\u653e\u65f6\uff0c\u624d\u80fd\u5728\u9501\u5c4f\u72b6\u6001\u4e0b\u63a7\u5236\u97f3\u91cf\u3002\u53ef\u4ee5\u5f55\u5236\u4e00\u4e2a\u97f3\u9891\u6587\u4ef6\uff0c\u5faa\u73af\u64ad\u653e\u3002<\/strong><\/li><\/ul>\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=\"\">AudioPlayer\n\nimport Foundation\nimport AVFoundation\n\nclass AudioPlayer: NSObject, AVAudioPlayerDelegate {\n    \n    static let instance = AudioPlayer()\n    \n    static var audioPlayer: AVAudioPlayer = {\n        return AVAudioPlayer()\n    }()\n\n    func initAudioPlayer(songTitle: String) {\n\n        let songTitle = songTitle\n        let path = Bundle.main.path(forResource: songTitle, ofType: nil, inDirectory: \"music\")!\n        let url = URL(fileURLWithPath: path)\n        do {\n            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)\n            AudioPlayer.audioPlayer = try AVAudioPlayer(contentsOf: url)\n            AudioPlayer.audioPlayer.delegate = self\n        } catch {\n\n        }\n    }\n\n    func stop() {\n        AudioPlayer.audioPlayer.stop()\n    }\n\n    func pause() {\n        AudioPlayer.audioPlayer.pause()\n    }\n    \n    func play() {\n        AudioPlayer.audioPlayer.play()\n    }\n    \n    func currentTime() -> Int {\n        return Int(AudioPlayer.audioPlayer.currentTime)\n    }\n    \n    func getTotalTime() -> Int {\n        return Int(AudioPlayer.audioPlayer.duration)\n    }\n    \n    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {\n    }\n}\n\n\n\nAudioPlayer.instance.initAudioPlayer(songTitle: \"notice.mp3\")\nAudioPlayer.instance.play()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728IOS\u7a0b\u5e8f\u4e2d\uff0c\u76d1\u542c\u97f3\u91cf\u6309\u4e0b\u7684\u52a8\u4f5c\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u3002 \u5728\u540e\u53f0\u6a21\u5f0f\u4e2d\uff0c\u6253\u5f00Audio\u5f00\u5173 \u53ea\u6709\u6253\u5f00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3811"}],"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=3811"}],"version-history":[{"count":7,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3811\/revisions"}],"predecessor-version":[{"id":3880,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3811\/revisions\/3880"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}