{"id":6876,"date":"2025-06-02T13:36:19","date_gmt":"2025-06-05T05:46:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6876"},"modified":"2025-06-05T13:36:19","modified_gmt":"2025-06-05T05:46:18","slug":"https-badgameshow-com-steven-114","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-114\/","title":{"rendered":"\u4f7f\u7528 Swift \u5be6\u73fe\u97f3\u983b\u9304\u97f3\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10 \ud83c\udfa4\ud83d\udd25"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u97f3\u983b\u9304\u97f3, AVFoundation, iOS \u958b\u767c\"><\/p>\n<h1>\u4f7f\u7528 Swift \u5be6\u73fe\u97f3\u983b\u9304\u97f3\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10 \ud83c\udfa4\ud83d\udd25<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u7528\u65bc\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u653e\u6e90\u78bc\u7a0b\u5f0f\u8a9e\u8a00\u3002\u81ea 2014 \u5e74 WWDC \u767c\u5e03\u4ee5\u4f86\uff0c\u5b83\u5df2\u6210\u70ba\u53d6\u4ee3 Objective-C \u7684\u9996\u9078\u8a9e\u8a00\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u5be6\u73fe\u97f3\u983b\u9304\u97f3\uff0c\u5305\u62ec\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e00\uff1a\u5c0e\u5165 AVFoundation \u6846\u67b6<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u6dfb\u52a0 <strong>AVFoundation<\/strong> \u6846\u67b6\u3002\u9019\u662f\u4e00\u500b\u5f37\u5927\u7684\u6846\u67b6\uff0c\u5c08\u9580\u7528\u65bc\u8655\u7406\u97f3\u983b\u548c\u8996\u983b\u7684\u64ad\u653e\u8207\u9304\u88fd\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e8c\uff1a\u8a2d\u7f6e AVAudioRecorder<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b <strong>AVAudioRecorder<\/strong> \u5c0d\u8c61\uff0c\u9019\u5c07\u7528\u65bc\u9304\u88fd\u97f3\u983b\u3002\u4ee5\u4e0b\u662f\u5275\u5efa AVAudioRecorder \u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import AVFoundation\n\n\/\/ \u78ba\u4fdd\u4f7f\u7528\u8005\u5df2\u6388\u6b0a\u9304\u97f3\nAVAudioSession.sharedInstance().requestRecordPermission { granted in\n    if granted {\n        \/\/ \u9304\u97f3\u8a2d\u5b9a\n        let audioFilename = getDocumentsDirectory().appendingPathComponent(\"recording.m4a\")\n        let settings: [String: Any] = [\n            AVFormatIDKey: Int(kAudioFormatMPEG4AAC),\n            AVSampleRateKey: 12000,\n            AVNumberOfChannelsKey: 1,\n            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue\n        ]\n        \n        do {\n            let audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)\n            audioRecorder.prepareToRecord()\n            audioRecorder.record()\n        } catch {\n            print(\"\u9304\u97f3\u5931\u6557: \\(error.localizedDescription)\")\n        }\n    } else {\n        print(\"\u9304\u97f3\u6b0a\u9650\u672a\u7372\u5f97\")\n    }\n}\n\nfunc getDocumentsDirectory() -> URL {\n    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)\n    return paths[0]\n}\n<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e09\uff1a\u958b\u59cb\u8207\u505c\u6b62\u9304\u97f3<\/h2>\n<p>\u8981\u958b\u59cb\u9304\u97f3\uff0c\u6211\u5011\u9700\u8981\u8abf\u7528 <strong>audioRecorder.record()<\/strong> \u65b9\u6cd5\uff0c\u800c\u8981\u505c\u6b62\u9304\u97f3\u5247\u9700\u8981\u8abf\u7528 <strong>audioRecorder.stop()<\/strong> \u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u76f8\u95dc\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">audioRecorder.record() \/\/ \u958b\u59cb\u9304\u97f3\n\/\/ \u9304\u97f3\u904e\u7a0b\u4e2d\uff0c\u7576\u9700\u8981\u505c\u6b62\u9304\u97f3\u6642\naudioRecorder.stop() \/\/ \u505c\u6b62\u9304\u97f3\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 AVAudioRecorder \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u9304\u97f3\u6b0a\u9650\u672a\u7372\u5f97\u6216\u8a2d\u5099\u7121\u6cd5\u4f7f\u7528\u9ea5\u514b\u98a8\u3002\u8acb\u78ba\u4fdd\u5728 Info.plist \u4e2d\u6dfb\u52a0\u76f8\u61c9\u7684\u6b0a\u9650\u63cf\u8ff0\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-xml line-numbers\"><key>NSMicrophoneUsageDescription<\/key>\n<string>\u6b64\u61c9\u7528\u9700\u8981\u4f7f\u7528\u9ea5\u514b\u98a8\u9304\u97f3<\/string>\n<\/code><\/pre>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u9304\u97f3\u529f\u80fd\uff0c\u60a8\u9084\u53ef\u4ee5\u64f4\u5c55\u61c9\u7528\uff0c\u4f8b\u5982\u6dfb\u52a0\u64ad\u653e\u9304\u97f3\u7684\u529f\u80fd\uff0c\u6216\u662f\u5c07\u9304\u97f3\u4e0a\u50b3\u81f3\u4f3a\u670d\u5668\u7b49\u3002\u9019\u4e9b\u64f4\u5c55\u529f\u80fd\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u61c9\u7528\u7684\u5be6\u7528\u6027\u8207\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5be6\u73fe\u97f3\u983b\u5f55\u97f3-\ud83c\udfa4\ud83d\udd25_1675397512.369227.webp\" alt=\"Swift \u5be6\u73fe\u97f3\u983b\u9304\u97f3 \ud83c\udfa4\ud83d\udd25\" title=\"Swift \u97f3\u983b\u9304\u97f3\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Swift \u9032\u884c\u97f3\u983b\u9304\u97f3\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u7372\u5f97\u9304\u97f3\u6b0a\u9650\uff1f<\/h3>\n<p>\u4f7f\u7528 AVAudioSession \u7684 requestRecordPermission \u65b9\u6cd5\u4f86\u8acb\u6c42\u9304\u97f3\u6b0a\u9650\uff0c\u4e26\u6839\u64da\u7528\u6236\u7684\u9078\u64c7\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<h3>2. \u9304\u97f3\u6587\u4ef6\u4fdd\u5b58\u5728\u54ea\u88e1\uff1f<\/h3>\n<p>\u9304\u97f3\u6587\u4ef6\u53ef\u4ee5\u4fdd\u5b58\u5728\u61c9\u7528\u7684 Documents \u76ee\u9304\u4e2d\uff0c\u4f7f\u7528 getDocumentsDirectory \u51fd\u6578\u7372\u53d6\u8a72\u76ee\u9304\u7684 URL\u3002<\/p>\n<h3>3. \u5982\u4f55\u64ad\u653e\u9304\u97f3\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 AVAudioPlayer \u985e\u4f86\u64ad\u653e\u9304\u97f3\uff0c\u5275\u5efa AVAudioPlayer \u5be6\u4f8b\u4e26\u52a0\u8f09\u9304\u97f3\u6587\u4ef6\u7684 URL\uff0c\u7136\u5f8c\u8abf\u7528 play() \u65b9\u6cd5\u4f86\u64ad\u653e\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u5be6\u73fe\u97f3\u983b\u5f55\u97f3\u529f\u80fd\uff0c\u4e26\u77ad\u89e3Swift\u4e2d\u7684methods()\u51fd\u6578\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u5be6\u73fe\u97f3\u983b\u5f55\u97f3\u3002<\/p>\n","protected":false},"author":1,"featured_media":6875,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6876","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5be6\u73fe\u97f3\u983b\u5f55\u97f3-\ud83c\udfa4\ud83d\udd25_1675397512.369227.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1MU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6876"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6876\/revisions"}],"predecessor-version":[{"id":13496,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6876\/revisions\/13496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6875"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}