{"id":3066,"date":"2017-07-17T13:48:57","date_gmt":"2017-07-17T08:18:57","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=3066"},"modified":"2017-07-17T13:59:23","modified_gmt":"2017-07-17T08:29:23","slug":"play-with-audio-in-swift-3-0","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/","title":{"rendered":"Play with Audio in Swift 3.0"},"content":{"rendered":"<p>Audio recording and playing is not a tough task in iOS but it takes time to understand and implement. There is a framework called <strong>AVFoundation\u00a0<\/strong>which will help you to record sound.<\/p>\n<h1>Steps To Record Sound:<\/h1>\n<h3><strong>1. \u00a0Import Framework AVFoundation:\u00a0<\/strong><\/h3>\n<p>To record and play audio first you need to import a framework<strong><code> AVFoundation<\/code><\/strong>. This framework will provide you classes and properties to record sound. To import this framework in your code open your project. Write the given line on the top of you class:<\/p>\n<pre class=\"lang:swift decode:true\">import AVFoundation<\/pre>\n<h3><strong>2. Create and set record session:<\/strong><\/h3>\n<p>For that you need to define two instance variable as given below:<\/p>\n<pre class=\"lang:swift decode:true \">var audioPlayer: AVAudioPlayer?\r\nvar recordSetting = Dictionary() as [String: Any]\r\n<\/pre>\n<p>Now time to initialize the settings for recording:<\/p>\n<pre class=\"lang:swift decode:true\">recordSetting[AVFormatIDKey] = NSNumber(value: kAudioFormatMPEG4AAC)\r\nrecordSetting[AVSampleRateKey] = 44100.0\r\nrecordSetting[AVNumberOfChannelsKey] = 1\r\n\r\nlet docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)\r\nlet fileURL = docDir.appendingPathComponent(\"Audio\").appendingPathExtension(\"m4a\")\r\ndo {\r\n    audioRecorder = try AVAudioRecorder(url: fileURL, settings: recordSetting)\r\n} catch {\r\n    print(error)\r\n}\r\nself.audioRecorder?.delegate = self\r\nself.audioRecorder?.isMeteringEnabled = true\r\nself.audioRecorder?.prepareToRecord()<\/pre>\n<p><strong>&gt;\u00a0<\/strong> Here \u00a0<strong><code>AVFormatIDKey<\/code><span class=\"s1\">\u00a0<\/span><\/strong><span class=\"s1\">is a format identifier. Here you define the format of recorded audio.<br \/>\n<strong>&gt; \u00a0<code>AVSampleRateKey<\/code><\/strong> is a\u00a0<\/span><span class=\"s1\">sample rate, in hertz, expressed as an <\/span><span class=\"s2\">NSNumber<\/span><span class=\"s1\"> floating point value.<br \/>\n<strong>&gt; \u00a0<code>isMeteringEnabled<\/code>\u00a0<\/strong> is a\u00a0Boolean value that indicates whether audio-level metering is enabled. By default, audio level metering is off for an audio recorder.<br \/>\n<strong>&gt; \u00a0<code>prepareToRecord()<\/code>\u00a0<\/strong> method creates an audio file and prepare the system for recording. It creates an audio file at the location specified by url in <strong><code>init(url: setting: )<\/code><\/strong> method. If a file already exists at the given location, it overwrites it.<\/span><span class=\"s1\"><br \/>\n<\/span><\/p>\n<h3><strong>3. Start Recording:<br \/>\n<\/strong><\/h3>\n<p>To start recording now you just need to get the <strong><code>AVAudioSession<\/code><\/strong>\u00a0&#8216;s shared instance and call a single method <strong><code>record()<\/code><\/strong>. But make sure you have initialize all the settings of recording.<\/p>\n<pre class=\"lang:swift decode:true\">let audioSession = AVAudioSession.sharedInstance()\r\ntry? audioSession.setActive(true)\r\ntry? audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)\r\n            \r\n\/\/ Start recording \r\nself.audioRecorder?.record()<\/pre>\n<p><strong>&gt;<\/strong>\u00a0First start a session of <strong><code>AVAudioSession<\/code><\/strong>\u00a0with shared instance.<strong><br \/>\n&gt;\u00a0<\/strong><strong><code>setActive(bool:)<\/code>\u00a0<\/strong>activates or deactivates your app session. If another audio session has higher priority than your audio session will fail.<br \/>\n<strong>&gt;\u00a0 <code>func setCategory(_ category: with option:)<\/code><\/strong>\u00a0used to set the audio category and mode which define how the application intend to use audio.<br \/>\n<strong>&gt;\u00a0<\/strong>Now, to start recording, call <strong><code>record()<\/code>\u00a0<\/strong>\u00a0method. It will start recording.<\/p>\n<h3><strong>4. \u00a0AVAudioRecorderDelegate:<\/strong><\/h3>\n<p>Here are basically four delegate methods:<\/p>\n<ul>\n<li>\n<p class=\"p1\"><strong><span class=\"s1\"><code>audioRecorderEncodeErrorDidOccur(<span class=\"s2\">_<\/span> recorder: <span class=\"s3\">AVAudioRecorder<\/span>, error: <span class=\"s3\">Error<\/span>?)<\/code><br \/>\n<\/span><\/strong><span class=\"s1\">This method will report you when any error occurred while encoding.<\/span><\/p>\n<\/li>\n<li>\n<p class=\"p1\"><strong style=\"font-size: 1rem;\"><code>audioRecorderBeginInterruption(<span class=\"s2\">_<\/span><span class=\"s1\"> recorder: <\/span><span class=\"s3\">AVAudioRecorder<\/span><span class=\"s1\">)<\/span><\/code><\/strong><span style=\"font-size: 1rem;\"><br \/>\nThis method will give you callback when recording is interrupted.<\/span><\/p>\n<\/li>\n<li>\n<p class=\"p1\"><strong style=\"font-size: 1rem;\"><span class=\"s1\"><code>audioRecorderEndInterruption(<span class=\"s2\">_<\/span> recorder: <span class=\"s3\">AVAudioRecorder<\/span>, withOptions flags: <span class=\"s3\">Int<\/span>)<\/code>\u00a0<\/span><\/strong><span class=\"s1\" style=\"font-size: 1rem;\"><br \/>\nThis method is called when the interruption has ended.<\/span><\/p>\n<\/li>\n<li>\n<p class=\"p1\"><span class=\"s1\"><code><strong>audioRecorderDidFinishRecording(<span class=\"s2\">_<\/span> recorder: <span class=\"s3\">AVAudioRecorder<\/span>, successfully flag: <span class=\"s3\">Bool<\/span>)<\/strong><\/code><br \/>\nThis method is called when the recording is finished or stoped.\u00a0<\/span><\/p>\n<\/li>\n<\/ul>\n<h3><strong>5. \u00a0Stop or Pause Recording:<\/strong><\/h3>\n<p>You can call the given method to stop recording<\/p>\n<pre class=\"lang:swift decode:true \">func stopRecording() {\r\n     audioRecorder?.stop()\r\n     let session = AVAudioSession.sharedInstance()\r\n     try? session.setActive(false)\r\n}<\/pre>\n<p>Here you can call<strong> <code>stop()<\/code><\/strong>\u00a0to stop recording and <strong><code>pause()<\/code><\/strong>\u00a0to pause recording. It you pause recording you can resume it by calling<strong> <code>record()<\/code>\u00a0<\/strong>method.<\/p>\n<h1>Play with Audio:<\/h1>\n<p>Here are some task related to play audio:<\/p>\n<ol>\n<li>Play an audio<\/li>\n<li>Pause an audio<\/li>\n<li>Stop<\/li>\n<\/ol>\n<h2>1. Play an audio:<\/h2>\n<p>To play audio there are two easy steps:<\/p>\n<ol>\n<li>Create object of <strong><code>AVAudioPlayer<\/code><\/strong> with file path<\/li>\n<li>Call<strong> <code>play()<\/code>\u00a0<\/strong>on that object<\/li>\n<\/ol>\n<pre class=\"lang:swift decode:true\">let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)\r\nlet fileURL = docDir.appendingPathComponent(\"Audio\").appendingPathExtension(\"mp3\")\r\nlet audioPlayer = try? AVAudioPlayer(contentsOf: fileURL!)\r\n\r\n\/\/ Play audio\r\naudioPlayer?.play()<\/pre>\n<p>There are three places where an audio can be found:<\/p>\n<ol>\n<li><strong>\u00a0App bundle:<\/strong> \u00a0You can add an audio in app bundle or you can read it from document directory of your app.\n<pre class=\"lang:swift decode:true\">\/\/ Url from bundle\r\nlet url = Bundle.main.url(forResource: \"MyAudio\", withExtension: \"mp3\")\r\n\r\n\/\/Url from document directry\r\nlet docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) \r\nlet fileURL = docDir.appendingPathComponent(\"Audio\").appendingPathExtension(\"mp3\")\r\n\r\n\/\/ Play audio\r\nlet audioPlayer = try? AVAudioPlayer(contentsOf: fileURL!)\r\naudioPlayer?.play()<\/pre>\n<\/li>\n<li><strong>Audio From Data :\u00a0<\/strong>You can play audio from data object. This can be used when you receive an audio as <code>Data<\/code>\u00a0object.\n<pre class=\"lang:swift decode:true \">let audioData = Data()  \/\/ your audio as Data\r\naudioPlayer = try AVAudioPlayer(data: data)\r\naudioPlayer.play()<\/pre>\n<p>You can also tell the type of file. There are two initialisers for that, one for data and other for audio file.<br \/>\n<strong>-&gt; \u00a0\u00a0<code>init<span class=\"s2\">(contentsOf url: <\/span><span class=\"s3\">URL<\/span><span class=\"s2\">, fileTypeHint utiString: <\/span><span class=\"s3\">String<\/span><span class=\"s2\">?) <\/span><span class=\"s1\">throws<\/span><\/code><span class=\"s1\" style=\"font-size: 1rem;\"><br \/>\n<\/span>-&gt; \u00a0 <code>init<span class=\"s2\">(data: <\/span><span class=\"s3\">Data<\/span><span class=\"s2\">, fileTypeHint utiString: <\/span><span class=\"s3\">String<\/span><span class=\"s2\">?) <\/span><span class=\"s1\">throws<\/span><\/code><\/strong><br \/>\nSometimes the file type can&#8217;t be determined or file is corrupted.\u00a0<strong><code><span class=\"s2\">fileTypeHint<\/span><\/code>\u00a0\u00a0<\/strong>will tells the parser what kind of data to look for. So it can be successfully parsed.<\/p>\n<p><em><strong>NOTE:-\u00a0<\/strong>In iPhone if you make your phone silent then it will mute your audio. To overcome this you just need to set category as given below:<br \/>\n<strong><code><span class=\"s1\">try<span class=\"s2\">? audioSession.<\/span><span class=\"s3\">setCategory<\/span><span class=\"s2\">(<\/span><span class=\"s3\">AVAudioSessionCategoryPlayback<\/span><span class=\"s2\">)<\/span><\/span><\/code>\u00a0<\/strong><br \/>\nThis will help you to play sound even if your iPhone is on silent mode.<br \/>\n<\/em><\/li>\n<li><strong>Recorded audio:\u00a0<\/strong>Record audio can be played as given above. For that you just need to give the path and extension of that audio file.\n<pre class=\"lang:swift decode:true \">\/\/Url from document directry\r\nlet docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) \r\nlet fileURL = docDir.appendingPathComponent(\"Audio\").appendingPathExtension(\"m4a\")\r\n<\/pre>\n<p>Now make object of <strong><code>AVAudioPlayer<\/code>\u00a0<\/strong>with that url and call play method.<\/li>\n<li><strong>From Mobile play list:\u00a0<\/strong>Audio file can be selected from your playlist. But the procedure is lil bit different. Here are the steps:<strong>&gt;\u00a0<\/strong>First you need to import a framework <strong><code>MediaPicker<\/code>,\u00a0<\/strong>this will provide you the classes and protocols to show and pick the audio file.<br \/>\n<strong>&gt;\u00a0<\/strong>Now create an object of <strong><code>MPMediaPickerController<\/code>\u00a0<\/strong>and do some initial setup. Now present this view controller.<\/p>\n<pre class=\"lang:swift decode:true \">let mediaPlayer = MPMediaPickerController(mediaTypes: .music)\r\nmediaPlayer.delegate = self\r\nmediaPlayer.allowsPickingMultipleItems = false\r\nmediaPlayer.modalPresentationStyle = .fullScreen\r\nmediaPlayer.prompt = \"Select any song from the list\"\r\nself.present(mediaPlayer, animated: true, completion: nil)<\/pre>\n<p><strong>&gt;\u00a0<\/strong>Now conform its delegate <strong><code>MPMediaPlayerControllerDelegate<\/code><\/strong> . Here are two methods which need to be implemented:<br \/>\n<strong>1. \u00a0\u00a0<code><span class=\"s1\">mediaPickerDidCancel(<\/span><span class=\"s2\">_<\/span><span class=\"s1\"> mediaPicker: <\/span><span class=\"s3\">MPMediaPickerController<\/span><span class=\"s1\">)<\/span><\/code><\/strong>\u00a0 : this method gives callback when you click on cancel button given in you media picker view controller. You can dismiss your view controller here.<br \/>\n<strong>2. \u00a0<code><span class=\"s1\">mediaPicker(<span class=\"s2\">_<\/span> mediaPicker: <span class=\"s3\">MPMediaPickerController<\/span>, didPickMediaItems mediaItemCollection: <span class=\"s3\">MPMediaItemCollection<\/span>)<\/span><\/code>\u00a0: \u00a0<\/strong>This method will give you callback when you select any audio. You can select more then one audio. Now you can do anything with that file.<\/p>\n<pre class=\"lang:swift decode:true\">func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {\r\n    dismiss(animated: true, completion: nil)\r\n}\r\n\r\nfunc mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {\r\n    let item = mediaItemCollection.items.first\r\n    do {\r\n        if let url = item?.assetURL {\r\n            audioPlayer = try AVAudioPlayer(contentsOf: url)\r\n            audioPlayer?.play()\r\n        }\r\n    } catch {\r\n        print(error)\r\n    }\r\n}<\/pre>\n<p><em><strong>NOTE:- \u00a0<\/strong><\/em><em>You can not test this on simulator. It can only be tested on device because for that you need iTunes.<\/em><\/li>\n<\/ol>\n<h2>2. Pause\/Stop an audio:<\/h2>\n<p>You just need to call n single method on your audio manager object. To pause an audio call <code>pause()<\/code>\u00a0and to stop an audio call <code>stop()<\/code><\/p>\n<h2>Other operation which can be performed on an Audio:<\/h2>\n<p>There are some properties and method which help you to increase performance of your project or matches your requirement:<\/p>\n<ul>\n<li><strong>isPlaying:\u00a0<\/strong>This property let you know if the audio is being played.<\/li>\n<li><strong>duration:\u00a0<\/strong>It is an object of <code>TimeInterval<\/code>\u00a0which tells you \u00a0the total duration of audio.<\/li>\n<li><strong>url:<\/strong> It returns the complete url of audio.<\/li>\n<li><strong>data:\u00a0<\/strong>\u00a0It gives you the data object of played audio<\/li>\n<li><strong>volume:\u00a0<\/strong>You can set the volume for your audio on the scale of 0.0 to 1.0<\/li>\n<li><strong>currentTime:\u00a0<\/strong>It will tell you the current played time of audio. You can change current time of audio to rewind or forward.<\/li>\n<li><strong>format:\u00a0<\/strong>This will tell you the format of audio.<\/li>\n<\/ul>\n<p>That&#8217;s all you need to play with audio in Swift 3.0. Enjoy learning \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Audio recording and playing is not a tough task in iOS but it takes time to understand and implement. There is a framework called AVFoundation\u00a0which will help you to record sound. Steps To Record Sound: 1. \u00a0Import Framework AVFoundation:\u00a0 To record and play audio first you need to import a framework AVFoundation. This framework will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,71],"tags":[178],"class_list":["post-3066","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-mobile","tag-swift-3-0"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Play with Audio in Swift 3.0 - InnovationM - Blog<\/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:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Play with Audio in Swift 3.0 - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Audio recording and playing is not a tough task in iOS but it takes time to understand and implement. There is a framework called AVFoundation\u00a0which will help you to record sound. Steps To Record Sound: 1. \u00a0Import Framework AVFoundation:\u00a0 To record and play audio first you need to import a framework AVFoundation. This framework will [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-17T08:18:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-17T08:29:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/07\/Audio-play-and-record-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1140\" \/>\n\t<meta property=\"og:image:height\" content=\"633\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Play with Audio in Swift 3.0\",\"datePublished\":\"2017-07-17T08:18:57+00:00\",\"dateModified\":\"2017-07-17T08:29:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/\"},\"wordCount\":903,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/07\\\/Audio-play-and-record-1.png\",\"keywords\":[\"Swift 3.0\"],\"articleSection\":[\"iOS\",\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/\",\"name\":\"Play with Audio in Swift 3.0 - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/07\\\/Audio-play-and-record-1.png\",\"datePublished\":\"2017-07-17T08:18:57+00:00\",\"dateModified\":\"2017-07-17T08:29:23+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/07\\\/Audio-play-and-record-1.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/07\\\/Audio-play-and-record-1.png\",\"width\":1140,\"height\":633},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/play-with-audio-in-swift-3-0\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Play with Audio in Swift 3.0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"AI, Software Development & Digital Engineering Insights Blog | InnovationM\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"https:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Play with Audio in Swift 3.0 - InnovationM - Blog","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:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/","og_locale":"en_US","og_type":"article","og_title":"Play with Audio in Swift 3.0 - InnovationM - Blog","og_description":"Audio recording and playing is not a tough task in iOS but it takes time to understand and implement. There is a framework called AVFoundation\u00a0which will help you to record sound. Steps To Record Sound: 1. \u00a0Import Framework AVFoundation:\u00a0 To record and play audio first you need to import a framework AVFoundation. This framework will [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/","og_site_name":"InnovationM - Blog","article_published_time":"2017-07-17T08:18:57+00:00","article_modified_time":"2017-07-17T08:29:23+00:00","og_image":[{"width":1140,"height":633,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/07\/Audio-play-and-record-1.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Play with Audio in Swift 3.0","datePublished":"2017-07-17T08:18:57+00:00","dateModified":"2017-07-17T08:29:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/"},"wordCount":903,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/07\/Audio-play-and-record-1.png","keywords":["Swift 3.0"],"articleSection":["iOS","Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/","url":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/","name":"Play with Audio in Swift 3.0 - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/07\/Audio-play-and-record-1.png","datePublished":"2017-07-17T08:18:57+00:00","dateModified":"2017-07-17T08:29:23+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/07\/Audio-play-and-record-1.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/07\/Audio-play-and-record-1.png","width":1140,"height":633},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/play-with-audio-in-swift-3-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Play with Audio in Swift 3.0"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"AI, Software Development & Digital Engineering Insights Blog | InnovationM","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["https:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/3066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=3066"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/3066\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/3483"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=3066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=3066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=3066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}