{"id":6766,"date":"2025-06-02T12:27:58","date_gmt":"2025-06-05T05:20:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6766"},"modified":"2025-06-05T12:27:58","modified_gmt":"2025-06-05T05:20:58","slug":"https-badgameshow-com-steven-59","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-59\/","title":{"rendered":"\u30102025 \u6700\u65b0\u7248 Swift \u97f3\u6a02\u64ad\u653e\u5668\u958b\u767c\u6559\u5b78 \ud83c\udfa7\u3011"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u97f3\u6a02\u64ad\u653e\u5668, iOS \u958b\u767c, AVFoundation, Swift \u6559\u5b78\"><\/p>\n<h2>2025 \u6700\u65b0\u7248 Swift \u97f3\u6a02\u64ad\u653e\u5668\u958b\u767c\u6559\u5b78 \ud83c\udfa7<\/h2>\n<p>\u96a8\u8457 Swift \u5728 iOS \u958b\u767c\u4e2d\u7684\u6301\u7e8c\u6d41\u884c\uff0c\u958b\u767c\u97f3\u6a02\u64ad\u653e\u5668\u7684\u9700\u6c42\u4e5f\u65e5\u76ca\u589e\u52a0\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u69cb\u5efa\u4e00\u500b\u529f\u80fd\u5b8c\u5584\u7684\u97f3\u6a02\u64ad\u653e\u5668\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff0c\u4ee5\u53ca\u5be6\u4f5c\u904e\u7a0b\u4e2d\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h3>\u5b89\u88dd AVFoundation \u6846\u67b6<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd <strong>AVFoundation<\/strong> \u6846\u67b6\u3002\u9019\u662f\u4e00\u500b\u5f37\u5927\u7684\u97f3\u983b\u64ad\u653e\u548c\u7de8\u8f2f\u6846\u67b6\uff0c\u53ef\u4ee5\u8b93\u6211\u5011\u7c21\u5316\u97f3\u6a02\u64ad\u653e\u5668\u7684\u958b\u767c\u904e\u7a0b\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">import AVFoundation\n<\/code><\/pre>\n<h3>\u5b9a\u7fa9\u97f3\u6a02\u64ad\u653e\u5668\u7269\u4ef6<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e00\u500b <strong>AVAudioPlayer<\/strong> \u7269\u4ef6\uff0c\u7528\u65bc\u8ca0\u8cac\u64ad\u653e\u97f3\u6a02\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">var audioPlayer: AVAudioPlayer?\n<\/code><\/pre>\n<h3>\u64b0\u5beb\u64ad\u653e\u97f3\u6a02\u7684\u51fd\u5f0f<\/h3>\n<p>\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f\u4f86\u64ad\u653e\u97f3\u6a02\u3002\u5728\u9019\u88e1\uff0c\u6211\u5011\u5c07\u6aa2\u67e5\u97f3\u6a02\u6a94\u6848\u662f\u5426\u5b58\u5728\u4e26\u8655\u7406\u53ef\u80fd\u7684\u932f\u8aa4\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func playMusic(fileName: String) {\n    guard let url = Bundle.main.url(forResource: fileName, withExtension: nil) else {\n        print(\"\u97f3\u6a02\u6a94\u6848\u672a\u627e\u5230\")\n        return\n    }\n\n    do {\n        audioPlayer = try AVAudioPlayer(contentsOf: url)\n        audioPlayer?.play()\n    } catch {\n        print(\"\u64ad\u653e\u97f3\u6a02\u6642\u767c\u751f\u932f\u8aa4: \\(error.localizedDescription)\")\n    }\n}\n<\/code><\/pre>\n<h3>\u4f7f\u7528\u7bc4\u4f8b<\/h3>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u64ad\u653e\u6307\u5b9a\u7684\u97f3\u6a02\u6a94\u6848\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">playMusic(fileName: \"myMusic.mp3\")\n<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u4f8b\u5982\u6a94\u6848\u8def\u5f91\u4e0d\u6b63\u78ba\u6216\u97f3\u983b\u683c\u5f0f\u4e0d\u652f\u63f4\u3002\u78ba\u4fdd\u97f3\u6a02\u6a94\u6848\u5df2\u6b63\u78ba\u52a0\u5165\u5230\u5c08\u6848\u4e2d\uff0c\u4e26\u6aa2\u67e5\u5176\u6a94\u540d\u53ca\u526f\u6a94\u540d\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u9664\u4e86\u57fa\u672c\u64ad\u653e\u529f\u80fd\uff0c\u60a8\u9084\u53ef\u4ee5\u70ba\u64ad\u653e\u5668\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\uff0c\u4f8b\u5982\u64ad\u653e\u5217\u8868\u3001\u97f3\u91cf\u63a7\u5236\u3001\u97f3\u6a02\u9032\u5ea6\u689d\u7b49\uff0c\u4ee5\u8c50\u5bcc\u7528\u6236\u9ad4\u9a57\u3002\u9019\u4e9b\u64f4\u5c55\u529f\u80fd\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u7684\u5438\u5f15\u529b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u97f3\u6a02\u64ad\u653e\u5668-\ud83c\udfa7_1675395369.188205.webp\" alt=\"Swift \u97f3\u6a02\u64ad\u653e\u5668 \ud83c\udfa7\" title=\"Swift \u97f3\u6a02\u64ad\u653e\u5668, Swift \u7a0b\u5f0f\u8a9e\u6cd5, Swift methods(), \u97f3\u6a02\u64ad\u653e\u5668\u958b\u767c, Swift \u97f3\u6a02\u64ad\u653e\u5668\u529f\u80fd\" width=\"1200\" height=\"628\"\/><\/p>\n<h3>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h3>\n<h4>Q1: \u5982\u4f55\u5728 Swift \u4e2d\u64ad\u653e\u4e0d\u540c\u683c\u5f0f\u7684\u97f3\u6a02\u6a94\u6848\uff1f<\/h4>\n<p>A: AVAudioPlayer \u652f\u63f4\u591a\u7a2e\u97f3\u983b\u683c\u5f0f\uff0c\u5305\u62ec MP3\u3001M4A \u548c WAV\u3002\u53ea\u9700\u78ba\u4fdd\u6a94\u6848\u683c\u5f0f\u6b63\u78ba\uff0c\u4e26\u5728\u64ad\u653e\u6642\u63d0\u4f9b\u6b63\u78ba\u7684\u6a94\u6848\u540d\u548c\u526f\u6a94\u540d\u3002<\/p>\n<h4>Q2: \u5982\u4f55\u5be6\u73fe\u97f3\u6a02\u64ad\u653e\u7684\u66ab\u505c\u548c\u505c\u6b62\u529f\u80fd\uff1f<\/h4>\n<p>A: \u60a8\u53ef\u4ee5\u5728 <code>AVAudioPlayer<\/code> \u7269\u4ef6\u4e0a\u4f7f\u7528 <code>pause()<\/code> \u548c <code>stop()<\/code> \u65b9\u6cd5\u4f86\u63a7\u5236\u97f3\u6a02\u7684\u64ad\u653e\u72c0\u614b\u3002<\/p>\n<h4>Q3: \u5982\u4f55\u5728\u64ad\u653e\u5668\u4e2d\u6dfb\u52a0\u97f3\u91cf\u63a7\u5236\uff1f<\/h4>\n<p>A: \u60a8\u53ef\u4ee5\u4f7f\u7528 <code>audioPlayer.volume<\/code> \u5c6c\u6027\u4f86\u8abf\u6574\u97f3\u91cf\uff0c\u7bc4\u570d\u5f9e 0.0\uff08\u975c\u97f3\uff09\u5230 1.0\uff08\u6700\u5927\u97f3\u91cf\uff09\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u5b78\u7fd2Swift\u97f3\u6a02\u64ad\u653e\u5668\ud83c\udfa7\u7684\u7a0b\u5f0f\u8a9e\u6cd5\u529f\u80fd\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u4f86\u958b\u767c\u97f3\u6a02\u64ad\u653e\u5668\uff0c\u4e26\u77ad\u89e3Swift\u4e2d\u7684methods()\u51fd\u6578\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":6765,"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-6766","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-\u97f3\u6a02\u64ad\u653e\u5668-\ud83c\udfa7_1675395369.188205.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1L8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6766","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=6766"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6766\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6765"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}