English feliratok

← Creating an Instance of AVAudioPlayer - Intro to iOS App Development with Swift

Beágyazókód kérése
3 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. So what this NSBundle.mainBundle is doing is that
  2. it's returning the path to the directory where our app is located.
  3. And the string pathForResource is getting for
  4. us the path of the folder where our mp3 file is.
  5. All right, so we're about to use this file path to play sound.
  6. Although, there is this one thing that is bothering me here.
  7. Which is, what if there is a problem getting this file path?
  8. For instance, what happens if we mis-type the name of the file, or
  9. its extension.
  10. To avoid such cases, it's good to include this declaration in an if statement.
  11. There.
  12. Now if you're unable to obtain this file path for
  13. whatever reason, we can print out a helpful error message.
  14. Okay, so it's time to do step two in our pseudo code,
  15. which is to create an instance of AVAudioPlayer.
  16. To do this, I will click on the Help menu and
  17. then go to Documentation, and here, I can search for AVAudioPlayer.
  18. Now, I went through this documentation, and in here,
  19. I found a constructor that can help us make an instance of AVAudioPlayer.
  20. It takes in two arguments, one of which is called URL, which is of type NSURL.
  21. I think this is the URL to our mp3 file.
  22. Although NSURL seems new.
  23. Now, at this point,
  24. I'm starting to wonder if the file path in my code is also of this type.
  25. So I'm going to go back to my code and check.
  26. And here, if I look in the documentation for path for
  27. resource, I can see that it's returning a string.
  28. And not an NSURL.
  29. So it seems that I have to convert the string to the type NSURL.
  30. And this is where you can help me.
  31. Can you run a Google search to figure out how to convert a string to NSURL?
  32. Here are the details you will need one more time.
  33. Our code currently returns a string,
  34. although we need a NSURL to create an instance of AVAudioPlayer.
  35. Can you convert a string to NSURL?