{"id":7627,"date":"2025-05-30T13:52:02","date_gmt":"2025-06-05T06:35:01","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7627"},"modified":"2025-06-05T13:52:02","modified_gmt":"2025-06-05T06:35:01","slug":"https-badgameshow-com-steven-465","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-465\/","title":{"rendered":"Swift \u591a\u5a92\u9ad4\u8655\u7406\uff1a2025 \u5e74\u6700\u65b0\u97f3\u983b\u8207\u8996\u983b\u64ad\u653e\u53ca\u7de8\u8f2f\u6307\u5357"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u591a\u5a92\u9ad4\u8655\u7406, \u97f3\u983b, \u8996\u983b, \u64ad\u653e, \u7de8\u8f2f\"><\/p>\n<h1>Swift \u591a\u5a92\u9ad4\u8655\u7406\uff1a2025 \u5e74\u6700\u65b0\u97f3\u983b\u8207\u8996\u983b\u64ad\u653e\u53ca\u7de8\u8f2f\u6307\u5357<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u7528\u65bc\u958b\u767c iOS \u548c macOS \u61c9\u7528\u7a0b\u5e8f\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u5275\u5efa\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8655\u7406\u591a\u5a92\u9ad4\uff0c\u7279\u5225\u662f\u97f3\u983b\u548c\u8996\u983b\u7684\u64ad\u653e\u8207\u7de8\u8f2f\uff0c\u4e26\u4ecb\u7d39 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528 Swift \u64ad\u653e\u97f3\u983b\u548c\u8996\u983b<\/h2>\n<p>Swift \u63d0\u4f9b\u4e86\u4e00\u500b\u540d\u70ba <strong>AVFoundation<\/strong> \u7684\u6846\u67b6\uff0c\u4f7f\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u64ad\u653e\u97f3\u983b\u548c\u8996\u983b\u3002AVFoundation \u63d0\u4f9b\u4e86\u4e00\u500b\u985e\u5225 <strong>AVPlayer<\/strong>\uff0c\u53ef\u7528\u65bc\u64ad\u653e\u97f3\u983b\u548c\u8996\u983b\u3002<\/p>\n<p>\u8981\u4f7f\u7528 AVPlayer \u64ad\u653e\u97f3\u983b\u548c\u8996\u983b\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500b AVPlayer \u5be6\u4f8b\uff0c\u4e26\u5c07\u97f3\u983b\u6216\u8996\u983b\u7684 URL \u50b3\u5165 AVPlayer \u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let player = AVPlayer(url: audioURL)<\/code><\/pre>\n<p>\u63a5\u8457\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>play()<\/strong> \u65b9\u6cd5\u4f86\u64ad\u653e\u97f3\u983b\u6216\u8996\u983b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">player.play()<\/code><\/pre>\n<p>AVPlayer \u9084\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u7684\u65b9\u6cd5\u4f86\u63a7\u5236\u97f3\u983b\u6216\u8996\u983b\u7684\u64ad\u653e\uff0c\u4f8b\u5982 <strong>pause()<\/strong>\u3001<strong>stop()<\/strong> \u548c <strong>seek(to:)<\/strong> \u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let player = AVPlayer(url: audioURL)\nplayer.play() \/\/ \u958b\u59cb\u64ad\u653e\n\/\/ \u505c\u6b62\u64ad\u653e\nplayer.pause()\n\/\/ \u5feb\u9032\u5230 10 \u79d2\nplayer.seek(to: CMTime(seconds: 10, preferredTimescale: 1))<\/code><\/pre>\n<h2>\u4f7f\u7528 Swift \u7de8\u8f2f\u97f3\u983b\u548c\u8996\u983b<\/h2>\n<p>AVFoundation \u9084\u63d0\u4f9b\u4e86\u4e00\u500b\u540d\u70ba <strong>AVMutableComposition<\/strong> \u7684\u985e\u5225\uff0c\u7528\u65bc\u5275\u5efa\u548c\u7de8\u8f2f\u97f3\u983b\u548c\u8996\u983b\u3002\u8981\u4f7f\u7528 AVMutableComposition \u7de8\u8f2f\u9019\u4e9b\u5a92\u9ad4\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500b AVMutableComposition \u5be6\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let composition = AVMutableComposition()<\/code><\/pre>\n<p>\u63a5\u8457\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>insertTimeRange(_:of:at:)<\/strong> \u65b9\u6cd5\u5c07\u97f3\u983b\u6216\u8996\u983b\u63d2\u5165\u5230 AVMutableComposition \u4e2d\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let track = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)\ntry? track?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration), of: videoAsset.tracks(withMediaType: .video)[0], at: CMTime.zero)<\/code><\/pre>\n<p>AVMutableComposition \u9084\u63d0\u4f9b\u5176\u4ed6\u7de8\u8f2f\u529f\u80fd\uff0c\u4f8b\u5982 <strong>removeTimeRange(_:) <\/strong>\u3001<strong>scaleTimeRange(_:toDuration:)<\/strong> \u548c <strong>splitTimeRange(_:atTime:)<\/strong>\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 AVMutableComposition \u7684\u4e00\u500b\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let composition = AVMutableComposition()\nlet audioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)\ntry? audioTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: audioAsset.duration), of: audioAsset.tracks(withMediaType: .audio)[0], at: CMTime.zero)<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 AVFoundation \u9032\u884c\u591a\u5a92\u9ad4\u8655\u7406\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u932f\u8aa4\u6392\u9664\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li><strong>\u97f3\u983b\/\u8996\u983b\u7121\u6cd5\u64ad\u653e\uff1a<\/strong>\u8acb\u6aa2\u67e5 URL \u662f\u5426\u6b63\u78ba\uff0c\u4e26\u78ba\u8a8d\u97f3\u983b\u6216\u8996\u983b\u683c\u5f0f\u662f\u5426\u53d7\u652f\u6301\u3002<\/li>\n<li><strong>\u7de8\u8f2f\u5931\u6557\uff1a<\/strong>\u78ba\u4fdd\u4f7f\u7528\u7684\u97f3\u983b\u548c\u8996\u983b\u8cc7\u6e90\u53ef\u7528\u4e14\u683c\u5f0f\u6b63\u78ba\uff0c\u4e26\u6aa2\u67e5\u662f\u5426\u6709\u6b0a\u9650\u8a2a\u554f\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>AVFoundation \u4e0d\u50c5\u50c5\u7528\u65bc\u64ad\u653e\u548c\u7de8\u8f2f\uff0c\u9084\u53ef\u4ee5\u7528\u4f86\u5be6\u73fe\u66f4\u9ad8\u7d1a\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u9304\u88fd\u97f3\u983b\u3001\u8996\u983b\u8f38\u51fa\u548c\u6c34\u5370\u6dfb\u52a0\u7b49\u3002\u9019\u4e9b\u529f\u80fd\u53ef\u4ee5\u7528\u65bc\u5275\u5efa\u66f4\u8c50\u5bcc\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8655\u7406\u591a\u5a92\u9ad4\uff0c\u7279\u5225\u662f\u97f3\u983b\u548c\u8996\u983b\u7684\u64ad\u653e\u8207\u7de8\u8f2f\u3002\u901a\u904e\u4f7f\u7528 <strong>AVFoundation<\/strong> \u6846\u67b6\u4e2d\u7684 <strong>AVPlayer<\/strong> \u548c <strong>AVMutableComposition<\/strong> \u985e\u5225\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210\u591a\u5a92\u9ad4\u529f\u80fd\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>1. AVFoundation \u662f\u5426\u652f\u6301\u6240\u6709\u97f3\u983b\u548c\u8996\u983b\u683c\u5f0f\uff1f<\/h3>\n<p>\u4e0d\uff0cAVFoundation \u4e3b\u8981\u652f\u6301 Apple \u7522\u54c1\u6240\u5e38\u7528\u7684\u683c\u5f0f\uff0c\u5982 .mp4\u3001.mov\u3001.m4a \u7b49\uff0c\u5177\u9ad4\u652f\u6301\u7684\u683c\u5f0f\u53ef\u4ee5\u53c3\u8003\u5b98\u65b9\u6587\u6a94\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406 AVPlayer \u7684\u64ad\u653e\u72c0\u614b\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 KVO\uff08Key-Value Observing\uff09\u4f86\u76e3\u807d AVPlayer \u7684 <strong>status<\/strong> \u5c6c\u6027\uff0c\u9019\u6a23\u53ef\u4ee5\u6839\u64da\u64ad\u653e\u72c0\u614b\u505a\u51fa\u76f8\u61c9\u7684\u8655\u7406\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728 Swift \u4e2d\u8655\u7406\u97f3\u983b\u8207\u8996\u983b\u7684\u540c\u6b65\u554f\u984c\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5229\u7528 AVMutableComposition \u4e2d\u7684\u97f3\u8ecc\u8207\u8996\u983b\u8ecc\u9053\u9032\u884c\u7cbe\u78ba\u7684\u6642\u9593\u7ba1\u7406\uff0c\u78ba\u4fdd\u4e8c\u8005\u7684\u64ad\u653e\u6642\u9593\u76f8\u4e92\u5c0d\u61c9\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u591a\u5a92\u9ad4\u8655\u7406-\u97f3\u983b\u548c\u8996\u983b\u7684\u64ad\u653e\u548c\u7de8\u8f2f_1675478320.626666.webp\" alt=\"Swift \u591a\u5a92\u9ad4\u8655\u7406 - \u97f3\u983b\u548c\u8996\u983b\u7684\u64ad\u653e\u548c\u7de8\u8f2f\" title=\"Swift \u591a\u5a92\u9ad4\u8655\u7406\u3001\u97f3\u983b\u64ad\u653e\u3001\u8996\u983b\u7de8\u8f2f\u3001Swift \u51fd\u6578\u3001Swift \u65b9\u6cd5\" width=\"1200\" height=\"628\"\/><br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2d\u7684\u591a\u5a92\u9ad4\u8655\u7406\uff0c\u5305\u62ec\u97f3\u983b\u548c\u8996\u983b\u7684\u64ad\u653e\u548c\u7de8\u8f2f\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Swift\u7684\u51fd\u6578\u548c\u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e9b\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":7626,"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-7627","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-\u591a\u5a92\u9ad4\u8655\u7406-\u97f3\u983b\u548c\u8996\u983b\u7684\u64ad\u653e\u548c\u7de8\u8f2f_1675478320.626666.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Z1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7627","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=7627"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7627\/revisions"}],"predecessor-version":[{"id":13156,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7627\/revisions\/13156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7626"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}