{"id":32749,"date":"2024-11-01T09:11:16","date_gmt":"2024-11-01T09:11:16","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32749"},"modified":"2024-11-01T11:23:53","modified_gmt":"2024-11-01T11:23:53","slug":"swiftui-style-for-swift-iphone-app-development-13-play-and-record-music","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32749\/","title":{"rendered":"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this post, we will take a closer look at how to play and record music in an iPhone app using Swift and SwiftUI. This tutorial will cover various techniques needed to implement music playback and recording features, starting from the basic elements of SwiftUI. I hope you will enhance your understanding of iOS app development and acquire the ability to perform practical tasks through this course.<\/p>\n<h2>1. What is SwiftUI?<\/h2>\n<p>SwiftUI is a UI framework announced by Apple in 2019 that uses a declarative programming style. This means you can define &#8216;what&#8217; to display rather than &#8216;how&#8217; to draw the UI you want to show on the screen. This allows developers to accomplish more tasks with less code.<\/p>\n<ul>\n<li>Intuitive Syntax: SwiftUI is based on Swift syntax, making it easy to learn.<\/li>\n<li>Real-time Preview: You can see the UI you are developing in real-time through Xcode&#8217;s preview feature.<\/li>\n<li>Supported on All Apple Platforms: SwiftUI can be used on iOS, macOS, watchOS, and tvOS.<\/li>\n<\/ul>\n<h2>2. Setting Up SwiftUI<\/h2>\n<p>First, you need to set up the basic environment to use SwiftUI. Download the latest version of Xcode and create a new project. Here are the steps to create a new project:<\/p>\n<ol>\n<li>Launch Xcode.<\/li>\n<li>Select &#8220;Create a new Xcode project&#8221;.<\/li>\n<li>Choose &#8220;App&#8221; and click &#8220;Next&#8221;.<\/li>\n<li>Enter a product name and select &#8220;SwiftUI&#8221; under Interface, then click &#8220;Next&#8221;.<\/li>\n<li>Select your desired save location and click &#8220;Create&#8221;.<\/li>\n<\/ol>\n<h2>3. Implementing Music Playback Functionality<\/h2>\n<p>Now, let&#8217;s implement music playback functionality using SwiftUI. You can use the `AVFoundation` framework to play audio files. Let\u2019s proceed to the next steps:<\/p>\n<h3>3.1 Importing the AVFoundation Framework<\/h3>\n<pre><code>import AVFoundation<\/code><\/pre>\n<h3>3.2 Creating the AudioPlayer Class<\/h3>\n<p>To play music files, we create the AudioPlayer class as follows:<\/p>\n<pre><code>\n    class AudioPlayer: ObservableObject {\n        var player: AVAudioPlayer?\n        \n        func playSound(sound: String, type: String) {\n            if let url = Bundle.main.url(forResource: sound, withExtension: type) {\n                do {\n                    player = try AVAudioPlayer(contentsOf: url)\n                    player?.play()\n                } catch {\n                    print(\"Failed to initialize player: \\(error)\")\n                }\n            }\n        }\n        \n        func stopSound() {\n            player?.stop()\n        }\n    }\n    <\/code><\/pre>\n<h3>3.3 Using in SwiftUI View<\/h3>\n<pre><code>\n    struct ContentView: View {\n        @StateObject var audioPlayer = AudioPlayer()\n\n        var body: some View {\n            VStack {\n                Button(\"Play Music\") {\n                    audioPlayer.playSound(sound: \"music\", type: \"mp3\")\n                }\n                \n                Button(\"Stop Music\") {\n                    audioPlayer.stopSound()\n                }\n            }\n        }\n    }\n    <\/code><\/pre>\n<h2>4. Implementing Music Recording Functionality<\/h2>\n<p>This time, we will add music recording functionality. You will need to use `AVAudioRecorder` for music recording. Follow the steps below.<\/p>\n<h3>4.1 Creating the Recorder Class<\/h3>\n<pre><code>\n    class Recorder: ObservableObject {\n        var audioRecorder: AVAudioRecorder?\n        let audioFilename = getDocumentsDirectory().appendingPathComponent(\"recording.m4a\")\n\n        static func getDocumentsDirectory() -> URL {\n            let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)\n            return paths[0]\n        }\n\n        func startRecording() {\n            let recordSettings: [String: Any] = [\n                AVFormatIDKey: Int32(kAudioFormatAppleLossless),\n                AVSampleRateKey: 44100.0,\n                AVNumberOfChannelsKey: 2,\n                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue\n            ]\n            do {\n                audioRecorder = try AVAudioRecorder(url: audioFilename, settings: recordSettings)\n                audioRecorder?.record()\n            } catch {\n                print(\"Failed to start recording: \\(error)\")\n            }\n        }\n\n        func stopRecording() {\n            audioRecorder?.stop()\n            audioRecorder = nil\n        }\n    }\n    <\/code><\/pre>\n<h3>4.2 Using in SwiftUI View<\/h3>\n<pre><code>\n    struct RecorderView: View {\n        @StateObject var recorder = Recorder()\n\n        var body: some View {\n            VStack {\n                Button(\"Start Recording\") {\n                    recorder.startRecording()\n                }\n                \n                Button(\"Stop Recording\") {\n                    recorder.stopRecording()\n                }\n            }\n        }\n    }\n    <\/code><\/pre>\n<h2>5. Enhancing UI Design<\/h2>\n<p>To make the UI more attractive, you can utilize various views and styles in SwiftUI. Add colors and shapes to buttons, and include text to provide descriptions of the functionality.<\/p>\n<pre><code>\n    Button(action: {\n        audioPlayer.playSound(sound: \"music\", type: \"mp3\")\n    }) {\n        Text(\"Play Music\")\n            .padding()\n            .background(Color.green)\n            .foregroundColor(.white)\n            .cornerRadius(10)\n    }\n    <\/code><\/pre>\n<h2>6. Summary and Future Developments<\/h2>\n<p>In this post, we explored how to implement music playback and recording features in an iOS app using SwiftUI. I hope this tutorial has not only enhanced your understanding of object-oriented programming but also helped you develop practical app development skills. In the future, we could learn about ways to further personalize user interfaces and apply various audio filters.<\/p>\n<h2>7. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/avfoundation\">AVFoundation Documentation<\/a><\/li>\n<li><a href=\"https:\/\/developer.apple.com\/design\/human-interface-guidelines\/swiftui\/overview\/themes\/\">SwiftUI Human Interface Guidelines<\/a><\/li>\n<li><a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\">SwiftUI Documentation<\/a><\/li>\n<\/ul>\n<p>Thank you! If you have any questions or curiosities about app development, please leave a comment, and I will respond. I wish you good luck on your development journey!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this post, we will take a closer look at how to play and record music in an iPhone app using Swift and SwiftUI. This tutorial will cover various techniques needed to implement music playback and recording features, starting from the basic elements of SwiftUI. I hope you will enhance your understanding of iOS &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32749\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-32749","post","type-post","status-publish","format-standard","hentry","category-swift-iphone-app-development-swiftui"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32749\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this post, we will take a closer look at how to play and record music in an iPhone app using Swift and SwiftUI. This tutorial will cover various techniques needed to implement music playback and recording features, starting from the basic elements of SwiftUI. I hope you will enhance your understanding of iOS &hellip; \ub354 \ubcf4\uae30 &quot;SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32749\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:11:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:23:53+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32749\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32749\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music\",\"datePublished\":\"2024-11-01T09:11:16+00:00\",\"dateModified\":\"2024-11-01T11:23:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32749\/\"},\"wordCount\":462,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift iPhone app development (SwiftUI)\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32749\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32749\/\",\"name\":\"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:11:16+00:00\",\"dateModified\":\"2024-11-01T11:23:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32749\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32749\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32749\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32749\/","og_locale":"ko_KR","og_type":"article","og_title":"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this post, we will take a closer look at how to play and record music in an iPhone app using Swift and SwiftUI. This tutorial will cover various techniques needed to implement music playback and recording features, starting from the basic elements of SwiftUI. I hope you will enhance your understanding of iOS &hellip; \ub354 \ubcf4\uae30 \"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music\"","og_url":"https:\/\/atmokpo.com\/w\/32749\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:11:16+00:00","article_modified_time":"2024-11-01T11:23:53+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32749\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32749\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music","datePublished":"2024-11-01T09:11:16+00:00","dateModified":"2024-11-01T11:23:53+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32749\/"},"wordCount":462,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift iPhone app development (SwiftUI)"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32749\/","url":"https:\/\/atmokpo.com\/w\/32749\/","name":"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:11:16+00:00","dateModified":"2024-11-01T11:23:53+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32749\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32749\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32749\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"SwiftUI Style for Swift, iPhone App Development: 13 Play and Record Music"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32749"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32749\/revisions"}],"predecessor-version":[{"id":32750,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32749\/revisions\/32750"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}