{"id":7254,"date":"2025-06-03T11:59:43","date_gmt":"2025-06-05T04:52:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7254"},"modified":"2025-06-05T11:59:43","modified_gmt":"2025-06-05T04:52:42","slug":"https-badgameshow-com-steven-299","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-299\/","title":{"rendered":"Swift \u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u6559\u5b78 \ud83c\udfb5"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift,\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c,AVFoundation,AVAudioPlayer\"><\/p>\n<h1>Swift \u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u6559\u5b78 \ud83c\udfb5<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u7b49\u61c9\u7528\u7a0b\u5f0f\u3002\u5728 Swift \u4e2d\uff0c\u958b\u767c\u8005\u4e0d\u50c5\u53ef\u4ee5\u5be6\u73fe\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\uff0c\u9084\u80fd\u900f\u904e\u6700\u65b0\u7684\u6700\u4f73\u5be6\u8e10\u4f86\u63d0\u5347\u61c9\u7528\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>\u5982\u4f55\u5728 Swift \u4e2d\u5be6\u73fe\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\uff1f<\/h2>\n<p>\u8981\u5728 Swift \u4e2d\u5be6\u73fe\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\uff0c\u958b\u767c\u8005\u9700\u8981\u4f7f\u7528 <strong>AVFoundation<\/strong> \u6846\u67b6\u3002\u9019\u500b\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u7a2e\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u64ad\u653e\u97f3\u6a02\uff0c\u4e26\u4e14\u53ef\u4ee5\u8a2d\u5b9a\u64ad\u653e\u6a21\u5f0f\uff0c\u4f8b\u5982\u5faa\u74b0\u64ad\u653e\u3001\u96a8\u6a5f\u64ad\u653e\u7b49\u3002<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u5c07\u97f3\u6a02\u6587\u4ef6\u52a0\u5165\u5c08\u6848<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u7684\u97f3\u6a02\u6587\u4ef6\uff08\u4f8b\u5982 <strong>music.mp3<\/strong>\uff09\u5df2\u7d93\u6dfb\u52a0\u5230 Xcode \u5c08\u6848\u4e2d\uff0c\u4e26\u4e14\u5728\u300cBuild Phases\u300d\u7684\u300cCopy Bundle Resources\u300d\u4e2d\u9032\u884c\u4e86\u914d\u7f6e\u3002<\/p>\n<h3>\u6b65\u9a5f 2\uff1a\u8a2d\u7f6e\u64ad\u653e\u5668<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5728 <strong>AppDelegate.swift<\/strong> \u4e2d\u52a0\u5165\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import AVFoundation\n\nvar audioPlayer: AVAudioPlayer?\n\nfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {\n    \/\/ \u5c07\u97f3\u6a02\u6587\u4ef6\u52a0\u5165\u5230\u5c08\u6848\u4e2d\n    guard let audioFilePath = Bundle.main.path(forResource: \"music\", ofType: \"mp3\") else {\n        print(\"\u97f3\u6a02\u6587\u4ef6\u672a\u627e\u5230\")\n        return false\n    }\n    let audioFileUrl = URL(fileURLWithPath: audioFilePath)\n\n    \/\/ \u5efa\u7acb\u64ad\u653e\u5668\n    do {\n        audioPlayer = try AVAudioPlayer(contentsOf: audioFileUrl)\n        audioPlayer?.numberOfLoops = -1 \/\/ \u5faa\u74b0\u64ad\u653e\n        audioPlayer?.play()\n    } catch {\n        print(\"\u7121\u6cd5\u555f\u52d5\u64ad\u653e\u5668\uff1a\\(error.localizedDescription)\")\n    }\n\n    return true\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u5c07\u97f3\u6a02\u6587\u4ef6\u8f09\u5165\u5c08\u6848\u4e2d\uff0c\u4e26\u5efa\u7acb\u4e00\u500b\u64ad\u653e\u5668\uff0c\u8a2d\u5b9a\u64ad\u653e\u6a21\u5f0f\u70ba\u5faa\u74b0\u64ad\u653e\uff0c\u6700\u5f8c\u555f\u52d5\u64ad\u653e\u5668\u4ee5\u5be6\u73fe\u80cc\u666f\u97f3\u6a02\u64ad\u653e\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>\u97f3\u6a02\u6587\u4ef6\u672a\u627e\u5230\uff1a<\/strong>\u78ba\u4fdd\u97f3\u6a02\u6587\u4ef6\u540d\u7a31\u6b63\u78ba\uff0c\u4e14\u5df2\u6dfb\u52a0\u5230\u5c08\u6848\u4e2d\u3002<\/li>\n<li><strong>\u64ad\u653e\u5668\u7121\u6cd5\u555f\u52d5\uff1a<\/strong>\u6aa2\u67e5\u97f3\u6a02\u6587\u4ef6\u683c\u5f0f\u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u4fdd\u64ad\u653e\u5668\u521d\u59cb\u5316\u6642\u672a\u62cb\u51fa\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u80cc\u666f\u64ad\u653e\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5ef6\u4f38\u61c9\u7528\uff1a<\/p>\n<ul>\n<li>\u81ea\u52d5\u6839\u64da\u7528\u6236\u884c\u70ba\u8abf\u6574\u97f3\u91cf\u3002<\/li>\n<li>\u7d50\u5408\u5176\u4ed6\u97f3\u6548\u5be6\u73fe\u66f4\u8c50\u5bcc\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/li>\n<li>\u4f7f\u7528 <strong>AVAudioSession<\/strong> \u914d\u7f6e\u97f3\u983b\u6703\u8a71\uff0c\u4ee5\u652f\u6301\u80cc\u666f\u64ad\u653e\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c-\ud83c\udfb5_1675409633.740428.webp\" alt=\"Swift \u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c \ud83c\udfb5\" title=\"Swift \u80cc\u666f\u64ad\u653e\u97f3\u6a02\u3001AVFoundation\u6846\u67b6\u3001AVAudioPlayer\u3001Swift\u97f3\u6a02\u6548\u679c\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\uff0c\u53ea\u9700\u8981\u4f7f\u7528 <strong>AVFoundation<\/strong> \u6846\u67b6\uff0c\u5c07\u97f3\u6a02\u6587\u4ef6\u52a0\u5165\u5230\u5c08\u6848\u4e2d\uff0c\u7136\u5f8c\u5efa\u7acb\u4e00\u500b\u64ad\u653e\u5668\uff0c\u4e26\u8a2d\u5b9a\u64ad\u653e\u6a21\u5f0f\uff0c\u6700\u5f8c\u555f\u52d5\u64ad\u653e\u5668\u5373\u53ef\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728 Swift \u4e2d\u505c\u6b62\u97f3\u6a02\u64ad\u653e\uff1f<\/h3>\n<p>A1: \u53ef\u4ee5\u76f4\u63a5\u547c\u53eb <code>audioPlayer?.stop()<\/code> \u65b9\u6cd5\u4f86\u505c\u6b62\u97f3\u6a02\u64ad\u653e\u3002<\/p>\n<h3>Q2: AVAudioPlayer \u652f\u6301\u54ea\u4e9b\u97f3\u6a02\u683c\u5f0f\uff1f<\/h3>\n<p>A2: AVAudioPlayer \u652f\u6301\u591a\u7a2e\u97f3\u983b\u683c\u5f0f\uff0c\u5305\u62ec MP3\u3001WAV\u3001AAC \u7b49\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u63a7\u5236\u80cc\u666f\u97f3\u6a02\u7684\u97f3\u91cf\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\u900f\u904e\u8a2d\u5b9a <code>audioPlayer?.volume<\/code> \u5c6c\u6027\u4f86\u8abf\u6574\u97f3\u91cf\u5927\u5c0f\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528AVFoundation\u6846\u67b6\uff0c\u5be6\u73fe\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\uff0c\u4e26\u8a73\u7d30\u4ecb\u7d39AVAudioPlayer\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728Swift\u4e2d\u5be6\u73fe\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c\u3002<\/p>\n","protected":false},"author":1,"featured_media":7253,"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-7254","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-\u80cc\u666f\u64ad\u653e\u97f3\u6a02\u6548\u679c-\ud83c\udfb5_1675409633.740428.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1T0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7254","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=7254"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7254\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7253"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}