0 1 00:00:00,360 --> 00:00:07,950 In the last lesson, we got our skeleton project off of GitHub and we managed to link up an IBAction 1 2 00:00:08,010 --> 00:00:15,210 with a single button on our Main.storyboard and that's the first button, this red button here, so 2 3 00:00:15,210 --> 00:00:22,050 that whenever it's pressed the words "I got pressed" get printed into the debug console. 3 4 00:00:22,170 --> 00:00:29,850 We're now pretty certain that our IBAction is linked to that red button and we can trigger some bit 4 5 00:00:29,850 --> 00:00:33,450 of functionality to happen when we press on the button. 5 6 00:00:33,450 --> 00:00:40,740 Now, of course, the actual functionality we want is to make it play a Xylophone note, but we don't know 6 7 00:00:40,740 --> 00:00:42,080 how to do that yet. 7 8 00:00:42,090 --> 00:00:45,750 Now, I could simply tell you, "Well, here's step A, 8 9 00:00:45,750 --> 00:00:52,240 this is what you do," but it doesn't answer the question of, "Well, how did I know how to do it?" 9 10 00:00:52,290 --> 00:00:58,590 Instead, I want to show you how we can use the Application Programming Interface Documentation which 10 11 00:00:58,590 --> 00:01:06,390 is quite a mouthful, but essentially, just refers to the components that Apple have already built into 11 12 00:01:06,410 --> 00:01:13,920 iOS that we can simply tap into and use, because Application Programming Interface takes like a whole 12 13 00:01:13,920 --> 00:01:15,120 second to say 13 14 00:01:15,130 --> 00:01:18,390 that people often refer to it as the API. 14 15 00:01:18,390 --> 00:01:26,250 So the API for your TV would be something like turning the volume up and down or switching the TV on 15 16 00:01:26,250 --> 00:01:28,620 and off or changing the channel number. 16 17 00:01:28,770 --> 00:01:35,480 So the API docs for your TV would then be the instruction manual which you probably throw away. 17 18 00:01:35,640 --> 00:01:42,450 But luckily for us, the Apple API Docs are hosted online and you can find them at developer.apple.com 18 19 00:01:42,450 --> 00:01:46,830 and then in documentation. 19 20 00:01:46,830 --> 00:01:51,210 So this is basically the instruction manual for programming 20 21 00:01:51,210 --> 00:01:56,250 iOS apps. Nut just like most manuals, they're so information-dense. 21 22 00:01:56,460 --> 00:02:04,080 And if you dig into each of these, you'll find that they have endless links after links after links and 22 23 00:02:04,350 --> 00:02:09,650 it's really hard figuring out exactly what you need to do from the documentation. 23 24 00:02:09,810 --> 00:02:14,910 Just like when my mom has a problem with her TV, she'll always call up tech support which is, basically, 24 25 00:02:14,910 --> 00:02:20,410 me, instead of reading the actual instruction manual for her TV. 25 26 00:02:20,430 --> 00:02:25,560 Well, programmers also don't usually start with the API Documentation, 26 27 00:02:25,560 --> 00:02:33,480 instead, we tend to use the programmer's tech support which is StackOverflow. Now StackOverflow is basically 27 28 00:02:33,510 --> 00:02:37,460 a Q-and-A website like Yahoo Answers or Quora, 28 29 00:02:37,530 --> 00:02:44,070 but for people who code. So let's say I had a question and because programmers are programmers, there 29 30 00:02:44,070 --> 00:02:47,720 are some crazy questions on here and some brilliant answers. 30 31 00:02:47,850 --> 00:02:55,770 If you ever wanted to know what is the most efficient way of pairing socks from a pile, well, you could 31 32 00:02:55,950 --> 00:03:02,080 search on StackOverflow and you'll see that somebody has asked this question six years ago with three 32 33 00:03:02,100 --> 00:03:03,210 thousand upvotes. 33 34 00:03:03,220 --> 00:03:09,270 That was a really popular question and we have a bunch of answers, in fact, two whole pages of answers 34 35 00:03:09,270 --> 00:03:15,450 here. And the top answer got a lot of upvotes, as well as the checkmark which means that the original 35 36 00:03:15,450 --> 00:03:19,650 person who asked the question has accepted this as the answer. 36 37 00:03:20,280 --> 00:03:25,650 So if you ever want to know how to sort your socks in the fastest way possible, well, apparently, you could 37 38 00:03:25,650 --> 00:03:31,890 use a hashing algorithm. Now because there are literally millions of things that you can make your iPhone 38 39 00:03:31,980 --> 00:03:32,810 app do, 39 40 00:03:32,940 --> 00:03:38,760 it will be impossible to remember all the names of those things that you need to use or the exact code 40 41 00:03:38,760 --> 00:03:40,410 that you would need to write, 41 42 00:03:40,410 --> 00:03:46,170 even things like how to change an image in an image view. You don't need to memorize the code ImageView.image. 42 43 00:03:46,200 --> 00:03:50,060 You could just look it up. And in the answer, 43 44 00:03:50,070 --> 00:03:56,010 people often provide a code snippet where they show you the answer or how to achieve what you want to 44 45 00:03:56,010 --> 00:03:56,310 do. 45 46 00:03:56,340 --> 00:03:59,640 So in this case, here's your ImageViewOutlet.image 46 47 00:03:59,640 --> 00:04:05,760 = your Image, and then we would be able to set the image view. Whenever you want to solve a 47 48 00:04:05,760 --> 00:04:10,360 problem in programming or when you want your code to do something specific, 48 49 00:04:10,440 --> 00:04:16,050 then I recommend following this five-step process which we're going to go through by trying to figure 49 50 00:04:16,050 --> 00:04:19,180 out how we can get our app to play sound. 50 51 00:04:19,200 --> 00:04:21,140 Step 1 involves Google. 51 52 00:04:21,180 --> 00:04:23,870 Step 2 involves going to StackOverflow. 52 53 00:04:23,880 --> 00:04:24,930 Step 3, 53 54 00:04:24,930 --> 00:04:26,900 implementing the code that you find there. 54 55 00:04:26,910 --> 00:04:30,450 Step 4, looking up what the code means in the docs. 55 56 00:04:30,450 --> 00:04:33,550 And step 5, customizing the code for your needs. 56 57 00:04:33,720 --> 00:04:35,840 And we're gonna go through this step by step. 57 58 00:04:36,120 --> 00:04:39,210 So let's start with the first one. 58 59 00:04:39,210 --> 00:04:44,910 Now, ask any experienced programmer and they will tell you that a large part of getting good at programming 59 60 00:04:45,060 --> 00:04:52,200 involves getting good at Googling. And two people with the same resources, say, a computer that's connected 60 61 00:04:52,200 --> 00:04:55,870 to the internet don't always end up with the same solutions. 61 62 00:04:56,070 --> 00:04:59,290 And some people might not even find a solution. 62 63 00:04:59,370 --> 00:05:04,740 So when you're doing mobile app programming, I recommend following this search structure. 63 64 00:05:04,740 --> 00:05:07,020 So, first, what I want my app to do. 64 65 00:05:07,410 --> 00:05:10,530 Secondly, which programming language am I using. 65 66 00:05:10,590 --> 00:05:11,260 And third, 66 67 00:05:11,310 --> 00:05:13,820 which resource do I want to come up. 67 68 00:05:14,070 --> 00:05:22,390 In our case, our search query will look something like this, Play sound using Swift on StackOverflow. 68 69 00:05:22,440 --> 00:05:25,140 So let's try it out. In Google, 69 70 00:05:25,140 --> 00:05:35,070 I'm gonna put in those exact search terms "play sound Swift using stackoverflow," and what we're looking 70 71 00:05:35,070 --> 00:05:39,290 for is a website that starts out with stackoverflow.com. 71 72 00:05:39,330 --> 00:05:46,230 So we're basically leveraging the power of Google's search engine to find the most relevant result on 72 73 00:05:46,230 --> 00:05:47,670 StackOverflow. 73 74 00:05:47,670 --> 00:05:53,730 Now you saw that we could also just search for our query in StackOverflow itself, but I find that Google 74 75 00:05:53,730 --> 00:05:58,410 is far better at search algorithms than any other company. 75 76 00:05:58,410 --> 00:06:07,920 Google looks at how often a search term leads to a click of a link, and then whether if the person stays 76 77 00:06:07,920 --> 00:06:13,130 there, which means that they found the right resource for that search term, or whether if they head back 77 78 00:06:13,140 --> 00:06:17,930 which means that they didn't find the right one and they have to look through the list. 78 79 00:06:18,060 --> 00:06:22,650 So step one, basically, ensures that you get a more relevant result. 79 80 00:06:22,650 --> 00:06:29,280 And once we found the StackOverflow answer that seems to most closely match what we want to do, in this 80 81 00:06:29,280 --> 00:06:32,790 case, the question is how to play a sound using Swift? 81 82 00:06:32,790 --> 00:06:35,730 That sounds pretty close to what we want to achieve, right? 82 83 00:06:36,210 --> 00:06:42,360 Well, then we can go ahead and head over to StackOverflow and take a look at the question and answer 83 84 00:06:42,360 --> 00:06:42,890 there. 84 85 00:06:42,900 --> 00:06:46,830 This takes us onto step two, exploring the question-answer 85 86 00:06:46,860 --> 00:06:54,510 on StackOverflow itself. And in this case, it's a good idea to take a look at the question to assess 86 87 00:06:54,510 --> 00:07:01,200 whether if your own query is similar to what this person is asking for. In this case, they want to play 87 88 00:07:01,200 --> 00:07:08,130 a sound using Swift and they had some code that worked previously, but it doesn't work in newer versions 88 89 00:07:08,130 --> 00:07:08,880 of Swift. 89 90 00:07:08,970 --> 00:07:11,220 So they want to know how do you do it now. 90 91 00:07:11,850 --> 00:07:18,300 So if we scroll down, you can see that there are total of 13 answers that have been provided. And people 91 92 00:07:18,300 --> 00:07:25,170 will often compete for highly voted questions because it means that their answers will get more exposure 92 93 00:07:25,500 --> 00:07:28,510 and they're more likely to collect reputation points. 93 94 00:07:28,620 --> 00:07:34,680 So every time you upvote somebody's answer or accept their answer as the correct answer, they get 94 95 00:07:34,680 --> 00:07:37,740 reputation points which you can see down here. 95 96 00:07:38,400 --> 00:07:44,520 And this is kind of like a game fight system to incentivize programmers to help each other, so that they 96 97 00:07:44,520 --> 00:07:51,030 can gain reputation. And as you get better in your skills, it's a good idea to pass it forward, and help 97 98 00:07:51,030 --> 00:07:58,140 people with what you know. And, also, when you go to job interviews as a programmer, by mentioning a really 98 99 00:07:58,140 --> 00:08:00,870 high reputation on StackOverflow, 99 100 00:08:00,870 --> 00:08:02,700 it also counts for a lot. 100 101 00:08:02,700 --> 00:08:04,930 So it helps to help others. 101 102 00:08:05,010 --> 00:08:10,320 Now, over here in the accepted answer, so the one with the green tick, you can see that they've provided 102 103 00:08:10,320 --> 00:08:17,340 code snippets for how to implement this functionality of playing sound using various different versions 103 104 00:08:17,340 --> 00:08:18,160 of Swift. 104 105 00:08:18,390 --> 00:08:22,080 So it's Swift 2.3, 3, and 4. 105 106 00:08:22,080 --> 00:08:30,280 I recommend looking for the latest version and anything from Swift 4 onward, so Swift 4, 106 107 00:08:30,290 --> 00:08:31,120 5.2. 107 108 00:08:31,140 --> 00:08:32,940 These are all pretty similar. 108 109 00:08:33,240 --> 00:08:37,700 As long as you see something that is Swift 4 or Plus, then you should be all right 109 110 00:08:37,710 --> 00:08:44,210 implementing the code. The order of the answers are presented in a specific way. 110 111 00:08:44,250 --> 00:08:47,970 The first one that you will see is often the accepted answer. 111 112 00:08:48,000 --> 00:08:50,220 So the one with the green tick on it. 112 113 00:08:50,550 --> 00:08:55,290 And if you scroll down, you'll see the answers ordered by the number of upvotes, 113 114 00:08:55,320 --> 00:09:01,380 at least this is the default order system. Now, because everything in development and programming moves 114 115 00:09:01,380 --> 00:09:02,730 on so quickly, 115 116 00:09:02,940 --> 00:09:10,410 just because the person asking the question accepted this answer back in, say, 2013, it doesn't mean that 116 117 00:09:10,410 --> 00:09:13,220 it's still the best answer. 117 118 00:09:13,260 --> 00:09:19,440 I always recommend students to take a look, not just at the top answer, but the top three to see if there's 118 119 00:09:19,440 --> 00:09:22,930 something that's more relevant to today. 119 120 00:09:22,980 --> 00:09:30,480 So now that we've found our answer on StackOverflow, it's time to go to step 3 and implement the sample 120 121 00:09:30,480 --> 00:09:32,570 code in our app. 121 122 00:09:32,610 --> 00:09:36,180 In this case, I'm actually going to use the top answer. 122 123 00:09:36,180 --> 00:09:42,240 Not only was it accepted, but it's also got a whole bunch of different versions and it seems to be the 123 124 00:09:42,240 --> 00:09:46,710 most compatible with where we are today. 124 125 00:09:46,770 --> 00:09:52,800 Let's try and use this code snippet to implement it in our code and try to get our app to be able to 125 126 00:09:52,800 --> 00:09:54,240 play sound. 126 127 00:09:54,240 --> 00:09:56,940 So the first line of code I see here is this 127 128 00:09:56,940 --> 00:09:59,430 import AVFoundation. 128 129 00:09:59,430 --> 00:10:04,950 I'm going to put that into our project just below where we are importing UIKit. 129 130 00:10:04,950 --> 00:10:10,950 I'm going to import the AVFoundation module. 130 131 00:10:10,950 --> 00:10:17,760 Now, these modules are essentially the libraries of code that Apple is written to allow us to interact 131 132 00:10:17,760 --> 00:10:22,410 with different parts of the hardware and to perform different tasks. 132 133 00:10:22,440 --> 00:10:29,040 So in this case, the AVFoundation refers to the audiovisual foundational module. 133 134 00:10:29,040 --> 00:10:35,160 Now that we've added the capability of interacting with the audio hardware, which is what this line does, 134 135 00:10:35,660 --> 00:10:38,970 it's time to actually set up our audio player. 135 136 00:10:38,970 --> 00:10:43,790 Notice how here we're creating a variable code player 136 137 00:10:44,400 --> 00:10:49,530 and we're setting it to be a AVAudioPlayer 137 138 00:10:49,530 --> 00:10:50,320 in our code. 138 139 00:10:50,380 --> 00:10:56,590 So we're gonna create a variable code player and then we're going to add a colon to specify the data 139 140 00:10:56,590 --> 00:11:01,660 type of our player which is gonna be AVAudioPlayer, 140 141 00:11:02,450 --> 00:11:05,590 and then we add a question mark at the end. 141 142 00:11:05,590 --> 00:11:08,380 Now, we're going to ignore this for now. 142 143 00:11:08,410 --> 00:11:13,900 I'm going to explain it in a huge amount of detail later on. But for now, let's focus on trying to get 143 144 00:11:13,930 --> 00:11:16,900 our sound playing ability to work first. 144 145 00:11:17,110 --> 00:11:22,440 The next thing I'm going to do is I'm actually just going to copy this entire thing wholesale, 145 146 00:11:22,480 --> 00:11:30,130 the rest of the code that's in the code snippet, and simply paste it into our project just above the 146 147 00:11:30,130 --> 00:11:31,280 last closing brace, 147 148 00:11:31,300 --> 00:11:33,180 so right about here. 148 149 00:11:33,190 --> 00:11:35,100 Now, once I pasted it in here, 149 150 00:11:35,110 --> 00:11:40,360 now we're going to have to do a little bit of modification because we don't actually have a resource 150 151 00:11:40,360 --> 00:11:42,280 code sound name, 151 152 00:11:42,370 --> 00:11:50,080 and with the extension of mp3 because we have sounds that are named like this C.wave, D.wave. 152 153 00:11:50,170 --> 00:11:51,970 So we have to change these. 153 154 00:11:52,030 --> 00:11:58,450 So I'm going to change this to a capital C, which looks exactly the same as the sound file, and I'm going 154 155 00:11:58,450 --> 00:12:03,600 to change the extension to wave which is W-A-V, not W-A-V-E. 155 156 00:12:04,300 --> 00:12:13,360 So now I've modified the sample code in the StackOverflow code snippet to work with our resource 156 157 00:12:13,390 --> 00:12:22,450 and I'm going to leave everything else as it is. But because this play sound lists in a separate block of 157 158 00:12:22,450 --> 00:12:30,070 code, I'm going to trigger it inside all keyPressed IBAction. So I'm going to delete that print statement 158 159 00:12:30,280 --> 00:12:39,670 and instead, call this playSound functionality, so that when we press the red key, this block gets triggered 159 160 00:12:39,790 --> 00:12:46,240 and it looks for this thing called playSound. It finds it right here, and then it executes all the code 160 161 00:12:46,240 --> 00:12:47,500 in this block. 161 162 00:12:47,500 --> 00:12:52,180 So let's hit run and see if it works. All right, 162 163 00:12:52,210 --> 00:13:02,290 so here's my app and I'm gonna press on the top button, and you can hear that a sound is being played. 163 164 00:13:02,590 --> 00:13:08,950 So now that we've achieved our first three steps, we've managed to implement the solution we found from 164 165 00:13:08,950 --> 00:13:10,310 StackOverflow. 165 166 00:13:10,330 --> 00:13:17,320 We're now ready to head over to step four which is to look at the Apple API Docs to understand what 166 167 00:13:17,320 --> 00:13:23,650 the code does. I'm firstly going to delete all of the existing comments 167 168 00:13:23,650 --> 00:13:31,630 that the person who provided the solution has put in there so that we only have the code, and then 168 169 00:13:31,690 --> 00:13:38,560 I'm gonna head over to the API Documentation, and you can find that by heading over to 169 170 00:13:38,590 --> 00:13:41,920 developerapple.com/documentation. 170 171 00:13:42,040 --> 00:13:50,770 I'm going to search for that thing that we imported which was called AVFoundation to see what it does. 171 172 00:13:50,770 --> 00:13:58,270 And the first result is the AVFoundation module that comes up. And this, as you can see, allows us to 172 173 00:13:58,270 --> 00:14:00,430 work with audiovisual assets. 173 174 00:14:00,430 --> 00:14:08,190 So, for example, our sound file to control cameras, to process audio, and to configure audio interactions. 174 175 00:14:08,800 --> 00:14:14,650 So it seems to do a lot of things. And you can see that the documentation is split into different topics: 175 176 00:14:15,010 --> 00:14:22,900 Playback and Editing, Media capture, Audio, et cetera. If we read through this, you can see that there's one part 176 177 00:14:23,050 --> 00:14:31,810 that allows us to play, record, mix, and process audio. Playing or audio is pretty much what we wanted to 177 178 00:14:31,810 --> 00:14:32,550 do, right? 178 179 00:14:32,560 --> 00:14:38,620 So if we click through on this link, you can see that it gives you more details on how to play sound 179 180 00:14:38,710 --> 00:14:40,780 using AVFoundation. 180 181 00:14:41,050 --> 00:14:50,760 And it says that for playback of a single track, use AVAudioPlayer. And if we take a look at our code, 181 182 00:14:51,120 --> 00:14:58,230 well, the AVAudioPlayer is exactly what we're using to play our sound. 182 183 00:14:58,280 --> 00:15:04,220 Now, if we move down in our code, we can see that we've got this thing called a Bundle. 183 184 00:15:04,220 --> 00:15:06,640 Now what exactly is a bundle? 184 185 00:15:06,680 --> 00:15:14,060 So let's try and understand that by looking inside the documentation and searching for a Bundle. 185 186 00:15:14,120 --> 00:15:20,240 Now if you click on the first link with the capital B for Bundle, then you can see that it's simply a 186 187 00:15:20,240 --> 00:15:30,170 representation of the code and resources stored on the disk. And in our case, we're using the main Bundle 187 188 00:15:30,620 --> 00:15:40,240 to locate our sound file resource. So if we ignore all the sort of programming keywords in pink, you can 188 189 00:15:40,240 --> 00:15:47,190 see that we're essentially creating this constant code URL which is looking for a resource code 189 190 00:15:47,230 --> 00:15:51,480 C.wave in all resources, 190 191 00:15:51,520 --> 00:15:58,070 so the disk area allocated for our app. And then the next thing in our code that we're using, the next 191 192 00:15:58,070 --> 00:16:02,780 sort of word that's really unfamiliar, is the AVAudiosession. 192 193 00:16:02,780 --> 00:16:11,060 I'm going to copy this and look for it inside the docs by, again, going to the search bar and typing it. 193 194 00:16:12,560 --> 00:16:18,350 And once we're here, then we're going to look for this thing called setCategory. What does setCategory actually do? 194 195 00:16:18,350 --> 00:16:26,540 On this page AVAudiosession, I'm going to hit command F to find this set category. And if you 195 196 00:16:26,540 --> 00:16:30,470 look through, you'll see where it tells us all about it. 196 197 00:16:30,860 --> 00:16:37,280 So here, we could click through to this link and learn more about this setCategory and what it does. 197 198 00:16:38,310 --> 00:16:45,330 Here we have a bit of discussion on how it works and you can find out that there's actually a whole 198 199 00:16:45,330 --> 00:16:48,750 bunch of categories that we might want to use. 199 200 00:16:49,020 --> 00:16:52,770 And the one that we're using is something called playback. 200 201 00:16:53,460 --> 00:17:00,530 So on this page, we can see this ambient which means that the sound playback is nonprimary, 201 202 00:17:00,720 --> 00:17:05,050 so your app can be used successfully with the sound turned off. 202 203 00:17:05,220 --> 00:17:10,410 So then there's other types like solo ambient or record playback. 203 204 00:17:10,410 --> 00:17:16,020 And this is one that we're using which is saying that the category for playing recorded music or other 204 205 00:17:16,020 --> 00:17:23,420 sounds are central to the successful use of your app. Given that it's a musical app, then playing all sounds, 205 206 00:17:23,430 --> 00:17:28,030 playing those notes is pretty central to the success of our app. 206 207 00:17:28,050 --> 00:17:32,180 It's not just like background music in a game with some sound effects. 207 208 00:17:32,430 --> 00:17:35,790 It's actually really important for that sound to come through. 208 209 00:17:35,790 --> 00:17:43,200 So by setting in that category, even if the user had their phone on silent, we will still be able to get 209 210 00:17:43,200 --> 00:17:47,880 our sound played because we're saying it is that important. 210 211 00:17:47,880 --> 00:17:54,180 And if you read through the documentation, the entire documentation on AVAaudioSession, which is what 211 212 00:17:54,180 --> 00:18:01,680 I did, then you'll find that there's this paragraph that explains to you that when in iOS, when the device 212 213 00:18:01,680 --> 00:18:09,270 is locked, the apps audio is silenced. In iOS setting, the Ring/Silent switch to silent, silences any 213 214 00:18:09,270 --> 00:18:11,360 audio being played by the app. 214 215 00:18:11,370 --> 00:18:18,540 So this is the default audio session. And we had to use this line of code to change that default audio 215 216 00:18:18,540 --> 00:18:27,210 session to a new category so that we can actually get our playback happening even if the phone is in 216 217 00:18:27,210 --> 00:18:28,290 silent mode. 217 218 00:18:29,270 --> 00:18:35,900 So notice how we're always researching the parts of the code that are sort of purple in color. 218 219 00:18:35,900 --> 00:18:42,380 Well, that's because by the end of the course, you will know full well what each of the pink parts do 219 220 00:18:42,440 --> 00:18:49,850 because these are these swift programming concepts. But you won't know all of the purple parts because 220 221 00:18:49,850 --> 00:18:54,980 these come from Apple's Application Programming Interface Documentation. 221 222 00:18:54,980 --> 00:19:01,700 So there are a myriad of things that you can do in there, anywhere from playing video to recording sound, 222 223 00:19:02,070 --> 00:19:05,660 and it's not possible to cover all of the things that you can do. 223 224 00:19:05,660 --> 00:19:13,430 But once you've understood how the programming works and you know how to search and find the answers 224 225 00:19:13,430 --> 00:19:20,120 to your problems, then you'll be able to research through the code just as we're doing right now. If you 225 226 00:19:20,120 --> 00:19:27,530 continue through this process of researching what each of these things do by using the documentation 226 227 00:19:27,530 --> 00:19:32,530 from Apple, then you'll get a pretty good idea of what our code is doing right here. 227 228 00:19:32,600 --> 00:19:40,220 We're basically grabbing hold of this C.wav file in our app resources right here, and then we're 228 229 00:19:40,220 --> 00:19:46,990 making sure that our sound gets played even if the phone is on silent. 229 230 00:19:47,300 --> 00:19:52,670 And then we tell our audio player that this is the file that we want to play C.wav, 230 231 00:19:53,240 --> 00:20:00,140 and then we actually play it. And if there were any errors, then we actually catch those areas and print 231 232 00:20:00,140 --> 00:20:04,430 it out. Now, this is a good time for a quick tip. 232 233 00:20:04,450 --> 00:20:11,200 Now, when you search for things in the Apple Developer Documentation, you get the full documentation and 233 234 00:20:11,320 --> 00:20:13,000 it is large. 234 235 00:20:13,000 --> 00:20:19,270 Very often when I'm using something new that I've never come across before, I tend to try and read the 235 236 00:20:19,270 --> 00:20:26,230 entire page on it and look through the links and try to get a overview of what I'm doing with my code. 236 237 00:20:26,710 --> 00:20:30,720 But when you're just trying to look up things quickly to see what they do, 237 238 00:20:30,880 --> 00:20:38,410 you can also simply hold down the option key on your keyboard and click on various things in your code. 238 239 00:20:38,410 --> 00:20:43,060 So as soon as I've got my option key held down, I've got this crosshair that shows up, 239 240 00:20:43,180 --> 00:20:51,500 and now if I hover over things such as my AVAudioPlayer or my Bundle, or AVudioSession, 240 241 00:20:51,550 --> 00:20:53,710 so basically anything that's not programming, 241 242 00:20:53,710 --> 00:21:00,250 so not the pink words which are to do with swift programming keywords, but the purple parts which come 242 243 00:21:00,250 --> 00:21:06,600 from Apple's APIs, then we can click on it and it will show us the quick documentation. 243 244 00:21:06,610 --> 00:21:10,090 So it's kind of like a shortened version of this. 244 245 00:21:10,090 --> 00:21:17,230 It only contains the top part. And you can get a quick idea about what each part does or take a look 245 246 00:21:17,230 --> 00:21:19,610 at it in detail in the developer docs. 246 247 00:21:19,630 --> 00:21:22,400 So that's just a neat trick for you. 247 248 00:21:22,420 --> 00:21:30,540 Now, we're finally ready to move on to the last step which is to customize your code. Now reading the 248 249 00:21:30,540 --> 00:21:36,970 documentation can be quite a long and laborious process. But once you've implemented the code and gotten 249 250 00:21:36,970 --> 00:21:43,260 it to work, then the documentation can be really helpful to help you figure out what each of these moving 250 251 00:21:43,260 --> 00:21:47,180 parts are doing and what their roles are. 251 252 00:21:47,190 --> 00:21:52,410 Once you've more or less understood what each line of code is, therefore, well, then it's time to make 252 253 00:21:52,410 --> 00:21:53,240 it your own. 253 254 00:21:53,280 --> 00:21:59,250 Because not everything that's in the code is necessary and the person who wrote the highest upvoted 254 255 00:21:59,280 --> 00:22:02,570 answer is not Steve Jobs, right? 255 256 00:22:02,580 --> 00:22:08,570 So they might also be wrong or they might be doing things that you don't actually need. 256 257 00:22:08,820 --> 00:22:15,690 And this step is probably the hardest part yet because it requires a little bit more knowledge of Swift 257 258 00:22:15,750 --> 00:22:20,790 programming and you'll be able to do this yourself once you've finished the course. 258 259 00:22:20,790 --> 00:22:26,340 But for now, I'm going to vastly simplify the code, so that we deal with the harder programming concepts 259 260 00:22:26,550 --> 00:22:28,870 after we've mastered the foundation parts. 260 261 00:22:29,970 --> 00:22:37,290 If you open up the README file here, I want you to copy all the code that's inside the replacement 261 262 00:22:37,290 --> 00:22:46,290 code, and go ahead and copy it and replace everything that's currently inside your ViewController with 262 263 00:22:46,380 --> 00:22:48,030 this new code. 263 264 00:22:48,030 --> 00:22:54,390 Now this, of course, relies on the fact that your IBAction was indeed called keyPressrf and it was spelled 264 265 00:22:54,510 --> 00:22:56,430 exactly the same way. 265 266 00:22:56,520 --> 00:23:06,270 Be sure to head into the IBAction and right-click on your button just to make sure that that is exactly 266 267 00:23:06,270 --> 00:23:08,530 the same as this, 267 268 00:23:08,850 --> 00:23:16,230 so that when you run your app, it doesn't crash, and we're going to simplify all of the code that we had 268 269 00:23:16,230 --> 00:23:17,100 before 269 270 00:23:17,100 --> 00:23:19,920 down to just these few lines. 270 271 00:23:20,040 --> 00:23:22,520 Now, it's much simpler. 271 272 00:23:22,590 --> 00:23:25,620 But we've gotten a little bit less functionality than before, 272 273 00:23:25,650 --> 00:23:27,690 but it's fine for our needs. 273 274 00:23:27,690 --> 00:23:31,700 We're still using the AVFoundation library from Apple. 274 275 00:23:31,920 --> 00:23:37,830 We're still creating a new AVAudioPlayer, and then we're setting 275 276 00:23:37,830 --> 00:23:47,220 our URL to be the location of our C.wav sound file in our project, and then we put this file 276 277 00:23:47,430 --> 00:23:51,590 into our player just like loading up our CD player with a CD. 277 278 00:23:52,050 --> 00:23:55,260 And finally, we tell it to play the sound. 278 279 00:23:55,260 --> 00:24:01,290 Now, remember, because we took away the parts which allow our sound to be played when the phone is on 279 280 00:24:01,290 --> 00:24:02,240 silent, 280 281 00:24:02,280 --> 00:24:08,100 if you run this app right now and your phone is on silent on your physical device, then it will not make 281 282 00:24:08,100 --> 00:24:08,520 a sound. 282 283 00:24:09,030 --> 00:24:15,900 But as long as you switch it back onto normal, then it will be fine. And this is all of the code that 283 284 00:24:15,900 --> 00:24:24,240 we got from our searching and our documentation reading, and we now have code that we understand, as well 284 285 00:24:24,240 --> 00:24:27,150 as having implemented for our purposes. 285 286 00:24:27,150 --> 00:24:34,110 Now, all that's left to do is to understand more of the Swift programming concepts behind this code, for 286 287 00:24:34,110 --> 00:24:41,130 example, functions and what it's doing in our code. For all of that and more, I'll see you on the next lesson.