{"id":32789,"date":"2024-11-01T09:11:34","date_gmt":"2024-11-01T09:11:34","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32789"},"modified":"2024-11-01T11:23:42","modified_gmt":"2024-11-01T11:23:42","slug":"swiftui-style-iphone-app-development-adding-playback-status-images-to-an-audio-app","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32789\/","title":{"rendered":"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App"},"content":{"rendered":"<p><body><\/p>\n<p>In recent years, SwiftUI has played a significant role in Apple&#8217;s app development ecosystem. SwiftUI provides an intuitive way to build UI with a declarative syntax, similar to Flutter. Today, we will explore how to create an audio app using SwiftUI and add images that change dynamically based on the playback state.<\/p>\n<h2>1. Basic Concepts of SwiftUI and Audio Player<\/h2>\n<p>SwiftUI is Apple&#8217;s latest UI framework designed to create UIs more simply and intuitively than previous UIKit. However, to directly handle audio, the AVFoundation framework must be used. Combining these two allows developers to create powerful multimedia-supported apps.<\/p>\n<h3>1.1 Overview of AVFoundation<\/h3>\n<p>AVFoundation is a powerful framework for handling audio and video content. It allows for easy implementation of features such as playing, pausing, and stopping audio files.<\/p>\n<h3>1.2 Overview of SwiftUI<\/h3>\n<p>SwiftUI is a framework that allows UIs to be constructed declaratively, easily reflecting gestures such as clicks and swipes to respond to state changes. A SwiftUI view maintains its own state and is updated immediately whenever that state changes.<\/p>\n<h2>2. Setting Up the Project<\/h2>\n<p>Let\u2019s set up a basic structure for a simple audio app using SwiftUI. Open Xcode and create a new SwiftUI project. Select &#8220;App&#8221; and give it an appropriate name.<\/p>\n<h3>2.1 Adding Essential Libraries<\/h3>\n<p>To use the AVFoundation framework, you need to add a description requesting permission for audio playback in the project&#8217;s <code>Info.plist<\/code> file. Add the following key-value pair:<\/p>\n<pre>\n<code>\n<key>NSMicrophoneUsageDescription<\/key>\n<string>Microphone access is required to play audio.<\/string>\n<\/code>\n<\/pre>\n<h3>2.2 Building the Basic UI<\/h3>\n<p>To build a simple UI, let&#8217;s use SwiftUI&#8217;s <code>VStack<\/code> and <code>Button<\/code>. Below is the code for the basic UI:<\/p>\n<pre>\n<code>\nstruct ContentView: View {\n    var body: some View {\n        VStack {\n            Text(\"Audio Player\")\n                .font(.largeTitle)\n                .padding()\n            \n            Button(action: {\n                \/\/ Add play function\n            }) {\n                Text(\"Play\")\n                    .font(.title)\n                    .padding()\n                    .background(Color.blue)\n                    .foregroundColor(.white)\n                    .cornerRadius(10)\n            }\n            \n            Button(action: {\n                \/\/ Add pause function\n            }) {\n                Text(\"Pause\")\n                    .font(.title)\n                    .padding()\n                    .background(Color.red)\n                    .foregroundColor(.white)\n                    .cornerRadius(10)\n            }\n        }\n    }\n}\n<\/code>\n<\/pre>\n<h2>3. Implementing Audio Playback Logic<\/h2>\n<p>We will use AVAudioPlayer to play audio files. First, import the AVFoundation framework and initialize the audio player.<\/p>\n<h3>3.1 Creating the Audio Player Class<\/h3>\n<pre>\n<code>\nimport AVFoundation\n\nclass AudioPlayer: ObservableObject {\n    var player: AVAudioPlayer?\n    @Published var isPlaying: Bool = false\n    \n    func playAudio() {\n        guard let url = Bundle.main.url(forResource: \"audio_file\", withExtension: \"mp3\") else { return }\n        \n        do {\n            player = try AVAudioPlayer(contentsOf: url)\n            player?.play()\n            isPlaying = true\n        } catch {\n            print(\"Error playing audio: \\(error.localizedDescription)\")\n        }\n    }\n    \n    func pauseAudio() {\n        player?.pause()\n        isPlaying = false;\n    }\n}\n<\/code>\n<\/pre>\n<h3>3.2 Connecting UI<\/h3>\n<p>Now we&#8217;ll connect the <code>AudioPlayer<\/code> class with the SwiftUI view to enable the play button and state functionality.<\/p>\n<pre>\n<code>\nstruct ContentView: View {\n    @ObservedObject var audioPlayer = AudioPlayer()\n    \n    var body: some View {\n        VStack {\n            Text(\"Audio Player\")\n                .font(.largeTitle)\n                .padding()\n            \n            Button(action: {\n                if audioPlayer.isPlaying {\n                    audioPlayer.pauseAudio()\n                } else {\n                    audioPlayer.playAudio()\n                }\n            }) {\n                Text(audioPlayer.isPlaying ? \"Pause\" : \"Play\")\n                    .font(.title)\n                    .padding()\n                    .background(audioPlayer.isPlaying ? Color.red : Color.blue)\n                    .foregroundColor(.white)\n                    .cornerRadius(10)\n            }\n        }\n        .onChange(of: audioPlayer.isPlaying) { newValue in\n            \/\/ Add logic to change image based on state change\n        }\n    }\n}\n<\/code>\n<\/pre>\n<h2>4. Adding Playback State Images<\/h2>\n<p>Enhance the user experience by adding images that display differently based on the audio playback state, helping users easily understand the current status visually.<\/p>\n<h3>4.1 Adding Images<\/h3>\n<p>First, you need to add images for the play and pause buttons to your project. Go to <code>Assets.xcassets<\/code> and add appropriate images. Name the two images &#8220;play&#8221; and &#8220;pause&#8221;, then link them for use in SwiftUI.<\/p>\n<h3>4.2 Managing Image States<\/h3>\n<p>To update the playback state images according to state changes, add a property in <code>ContentView<\/code> to display the images.<\/p>\n<pre>\n<code>\nstruct ContentView: View {\n    @ObservedObject var audioPlayer = AudioPlayer()\n    \n    var body: some View {\n        VStack {\n            Text(\"Audio Player\")\n                .font(.largeTitle)\n                .padding()\n            \n            Image(audioPlayer.isPlaying ? \"pause\" : \"play\")\n                .resizable()\n                .aspectRatio(contentMode: .fit)\n                .frame(width: 100, height: 100)\n                .padding()\n            \n            Button(action: {\n                if audioPlayer.isPlaying {\n                    audioPlayer.pauseAudio()\n                } else {\n                    audioPlayer.playAudio()\n                }\n            }) {\n                Text(audioPlayer.isPlaying ? \"Pause\" : \"Play\")\n                    .font(.title)\n                    .padding()\n                    .background(audioPlayer.isPlaying ? Color.red : Color.blue)\n                    .foregroundColor(.white)\n                    .cornerRadius(10)\n            }\n        }\n    }\n}\n<\/code>\n<\/pre>\n<h2>5. Conclusion<\/h2>\n<p>We have now implemented the functionality of dynamically changing images based on playback state in our simple SwiftUI-based audio app. By leveraging the powers of SwiftUI and AVFoundation, you can create an audio app with many features.<\/p>\n<p>Consider adding more features to create your own audio player. For example, you can add playlist support, display playback time, or incorporate UI elements that show song information. We hope this process has deepened your understanding of SwiftUI, and we will return with more useful information next time.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In recent years, SwiftUI has played a significant role in Apple&#8217;s app development ecosystem. SwiftUI provides an intuitive way to build UI with a declarative syntax, similar to Flutter. Today, we will explore how to create an audio app using SwiftUI and add images that change dynamically based on the playback state. 1. Basic Concepts &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32789\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App&#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-32789","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, iPhone App Development: Adding Playback Status Images to an Audio App - \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\/32789\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In recent years, SwiftUI has played a significant role in Apple&#8217;s app development ecosystem. SwiftUI provides an intuitive way to build UI with a declarative syntax, similar to Flutter. Today, we will explore how to create an audio app using SwiftUI and add images that change dynamically based on the playback state. 1. Basic Concepts &hellip; \ub354 \ubcf4\uae30 &quot;SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32789\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:11:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:23:42+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32789\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32789\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App\",\"datePublished\":\"2024-11-01T09:11:34+00:00\",\"dateModified\":\"2024-11-01T11:23:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32789\/\"},\"wordCount\":488,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift iPhone app development (SwiftUI)\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32789\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32789\/\",\"name\":\"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:11:34+00:00\",\"dateModified\":\"2024-11-01T11:23:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32789\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32789\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32789\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App\"}]},{\"@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, iPhone App Development: Adding Playback Status Images to an Audio App - \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\/32789\/","og_locale":"ko_KR","og_type":"article","og_title":"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In recent years, SwiftUI has played a significant role in Apple&#8217;s app development ecosystem. SwiftUI provides an intuitive way to build UI with a declarative syntax, similar to Flutter. Today, we will explore how to create an audio app using SwiftUI and add images that change dynamically based on the playback state. 1. Basic Concepts &hellip; \ub354 \ubcf4\uae30 \"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App\"","og_url":"https:\/\/atmokpo.com\/w\/32789\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:11:34+00:00","article_modified_time":"2024-11-01T11:23:42+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32789\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32789\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App","datePublished":"2024-11-01T09:11:34+00:00","dateModified":"2024-11-01T11:23:42+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32789\/"},"wordCount":488,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift iPhone app development (SwiftUI)"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32789\/","url":"https:\/\/atmokpo.com\/w\/32789\/","name":"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:11:34+00:00","dateModified":"2024-11-01T11:23:42+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32789\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32789\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32789\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"SwiftUI Style, iPhone App Development: Adding Playback Status Images to an Audio App"}]},{"@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\/32789","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=32789"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32789\/revisions"}],"predecessor-version":[{"id":32790,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32789\/revisions\/32790"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}