{"id":2462,"date":"2021-04-23T21:50:24","date_gmt":"2021-04-23T13:50:24","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2462"},"modified":"2021-04-23T21:50:24","modified_gmt":"2021-04-23T13:50:24","slug":"swift-%e6%92%ad%e6%94%be%e5%a3%b0%e9%9f%b3","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2462","title":{"rendered":"Swift \u64ad\u653e\u58f0\u97f3"},"content":{"rendered":"\n<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728Swift\u4e2d\uff0c\u64ad\u653e\u58f0\u97f3\uff0c\u6709\u53ef\u80fd\u662f\u7cfb\u7edf\u7684\u58f0\u97f3\uff0c\u6709\u53ef\u80fd\u662f\u81ea\u5b9a\u4e49\u7684\u58f0\u97f3\u3002\u8fd9\u91cc\u9700\u8981\u501f\u52a9AVFoundation\u7684AVAudioPlayer\u3002<\/p>\n\n\n\n<ul><li>\u4e0b\u9762\u662fSample\u7684\u4ee3\u7801\u3002<\/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 Foundation\nimport MapKit\nimport MediaPlayer\nimport AVFoundation\nimport UIKit\nimport AudioToolbox\n\nclass AudioPlayer: NSObject, AVAudioPlayerDelegate {\n    \/\/ \u5355\u4f8b\u6a21\u5f0f\n    static let instance = AudioPlayer()\n    var isSoundPlaying = false\n    static var audioPlayer: AVAudioPlayer = {\n        return AVAudioPlayer()\n    }()\n    \n    \/\/ \u203b\u64ad\u653e\u81ea\u5b9a\u4e49\u7684\u58f0\u97f3\uff0c\u9700\u8981\u628a\u58f0\u97f3\u6587\u4ef6\u62f7\u8d1d\u5230\u5de5\u7a0b\u76ee\u5f55\u4e2d\uff0c\u5e76\u4e14\u6307\u5b9a\u8fd9\u4e2a\u76ee\u5f55\u548c\u58f0\u97f3\u6587\u4ef6\u7684\u540d\u5b57\u3002\n    func play(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            \/\/ \u203bAVAudioPlayerDelegate\u4ee3\u7406\n            AudioPlayer.audioPlayer.delegate = self\n            AudioPlayer.audioPlayer.play()\n        } catch {\n            \n        }\n    }\n    \n    \/\/ \u203b\u64ad\u653e\u7cfb\u7edf\u7684\u58f0\u97f3 \u9700\u8981\u77e5\u9053\u7cfb\u7edf\u58f0\u97f3\u7684ID \u53ef\u4ee5\u53bb\u7f51\u7ad9\u67e5\u8be2\u3002\n    \/\/ \u203bhttps:\/\/iphonedev.wiki\/index.php\/AudioServices\n    func play(id: UInt32) {\n        AudioServicesPlaySystemSound(id)\n    }\n    \n    func stop() {\n        AudioPlayer.audioPlayer.stop()\n    }\n    \n    func pause() {\n        AudioPlayer.audioPlayer.pause()\n    }\n    \n    func resume() {\n        AudioPlayer.audioPlayer.play()\n    }\n    \n    func currentTime() -> Int {\n        return Int(AudioPlayer.audioPlayer.currentTime)\n    }\n    \n    \/\/ \u203b\u5f53\u97f3\u9891\u6587\u4ef6\u64ad\u653e\u5b8c\u6210\u65f6\uff0c\u4f1a\u89e6\u53d1\u8fd9\u4e2a\u65b9\u6cd5\u3002\n    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {\n        AudioPlayer.audioPlayer.stop()\n        NotificationCenter.default.post(name: .needToStop, object: nil, userInfo: nil)\n    }\n}<\/pre>\n\n\n\n<ul><li>\u81ea\u5b9a\u4e49\u7684\u58f0\u97f3\u6587\u4ef6\u9700\u8981\u62f7\u8d1d\u5230\u5de5\u7a0b\u91cc\u9762<\/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\/2021\/04\/image-114.png\" alt=\"\" class=\"wp-image-2464\" width=\"431\" height=\"203\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-114.png 582w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-114-300x141.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-114-230x108.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-114-350x165.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-114-480x226.png 480w\" sizes=\"(max-width: 431px) 100vw, 431px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u64ad\u653e\u7cfb\u7edf\u58f0\u97f3\u9700\u8981\u7528id\uff0c\u53ef\u4ee5\u4ece\u7f51\u7ad9\uff08https:\/\/iphonedev.wiki\/index.php\/AudioServices\uff09\u67e5\u8be2\u58f0\u97f3\u6587\u4ef6\u7684id\u3002<\/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\/2021\/04\/image-116-1024x571.png\" alt=\"\" class=\"wp-image-2467\" width=\"543\" height=\"303\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-1024x571.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-300x167.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-768x428.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-1536x857.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-830x463.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-230x128.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-350x195.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116-480x268.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-116.png 1900w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u53ef\u4ee5\u4ece\u7f51\u7ad9\uff08<a href=\"https:\/\/www.zedge.net\/find\/synth\"><\/a><a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/www.zedge.net\/find\/synth\">https:\/\/www.zedge.net\/find\/synth<\/a>&nbsp;\uff09\u4e0b\u8f7d\u5404\u79cd\u58f0\u97f3\u6587\u4ef6\u3002<\/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\/2021\/04\/image-120-1024x733.png\" alt=\"\" class=\"wp-image-2472\" width=\"551\" height=\"394\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120-1024x733.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120-300x215.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120-768x549.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120-830x594.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120-230x165.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120-350x250.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120-480x343.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/04\/image-120.png 1420w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728Swift\u4e2d\uff0c\u64ad\u653e\u58f0\u97f3\uff0c\u6709\u53ef\u80fd\u662f\u7cfb\u7edf\u7684\u58f0\u97f3\uff0c\u6709\u53ef\u80fd\u662f\u81ea\u5b9a\u4e49\u7684\u58f0\u97f3\u3002\u8fd9\u91cc\u9700\u8981\u501f\u52a9AVFounda [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2462"}],"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=2462"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2462\/revisions"}],"predecessor-version":[{"id":2473,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2462\/revisions\/2473"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}