Getting Serious on the Playground

I know, let’s write a streaming web radio application!

Don’t panic; this isn’t as scary as you think. We can get this running with shockingly little code. But let’s do so in a new playground. Close the current playground window, and use File > New > Playground to create a new playground. Call it WebRadioPlayground. Keep the line that says import UIKit, but delete the var str = "Hello, playground" line, and replace it with the following:

1: import​ ​AVFoundation
2: let​ url = ​URL​(string:
3: "http://www.npr.org/streams/aac/live1_aac.pls"​)
4: let​ player = ​AVPlayer​(url: url!)
5: player.play()

Nothing will happen just yet, but hang in there…

Xcode Console Garbage

images/aside-icons/tip.png

As you start writing the code, you may see an error message like:

 2016-11-20 17:28:47.041 WebRadioPlayground[19048:8310129] Failed
 to obtain sandbox extension for ...

This is harmless and can be ignored.

The code we’ve written is in a language called Swift, introduced by Apple in 2014 for iOS and macOS development (and, later, watchOS and tvOS). It’s a flexible language that’s well suited to various styles of programming, as we’ll see throughout the book. Swift is also the only language we can use inside a playground. You can also use C, C++, or Objective-C to write apps, but we’ll only use Swift in this book.

Line 1 tells the Swift compiler that we want to use AV Foundation, a programming framework that lets us bring audio and video features to our apps. On lines 2--3, we create a URL for the station we want to play. Technically, this is an object of type URL, and not just a string. Instead, we create the URL by passing a string to its initializer, which creates and sets up the object.

Line 4 creates an AVPlayer, which is an object that can play various kinds of audio and video media. We create it with the url on the previous line, and the ! character is our assertion that the url is valid and not nil. This is actually a dangerous practice—we’re not really in a position to know whether the URL is valid—and is something we will want to fix up a little later. Finally, on line 5, we tell the player to start playing.

Be Your Own DJ

images/aside-icons/note.png

You don’t have to use our default URL; we just thought using a National Public Radio feed was a good bet for a URL that wouldn’t get us sued and wouldn’t disappear anytime soon. But we could be wrong; a URL we used in an earlier revision of this chapter disappeared in May, 2016, after more than a decade of non-stop streaming. So it’s good to know how to choose and use a different URL for yourself.

To use a different stream, find a station you like in iTunes’ Internet Radio section (but not the Radio section, which is only for Apple Music subscribers), and while it’s playing, use Get Info (I) to show its URL. Copy and paste this string in the playground, replacing our URL (make sure it’s still in quotes). The AVPlayer class can handle the sort of HTTP-based audio streams seen in iTunes’ Internet Radio section, so thousands of choices are available.

The results pane will show the URL string next to the line that creates the URL, and the lines involving the player will show AVPlayer and a big (64-bit) hexadecimal number (the player object’s address in memory, which does us no good here). But nothing’s actually playing, right?

images/playing/playground-player-results.png

We need to do one special thing for this example. Swift is a language with built-in memory management that frees unused objects for us. Once we say player.play(), all the code in the playground has been executed, and Xcode assumes it’s OK to clean everything up. Unfortunately, this results in the immediate destruction of the player object that we want to play our audio!

When we have a case where we want the contents of the playground to hang around after the initial execution finishes, we need to send a special command to Xcode to do so. Add the following to the end of the playground code:

1: import​ ​PlaygroundSupport
2: PlaygroundPage​.current.needsIndefiniteExecution = ​true

The import statement on line 1 tells the playground to load the functions and methods that let us interact with the playground execution itself, and setting the PlaygroundPage.current.needsIndefiniteExecution variable to true on line 2 gets the current page of the playground and tells it to keep executing indefinitely, instead of exiting (which would destroy the player variable).

Notice that as soon as you enter this last line and stop typing for a few seconds, you’ll start hearing the web radio station playing (provided you have an Internet connection). Cool! Web radio with six lines of code!

Typing again causes the audio to stop, until you let up on the keyboard. Basically, when the playground thinks you’re done editing, it tries to build and run the code, and in this case, that means music starts playing again.

So that’s our first little bit of code that does something cool. Now let’s look at how we got it to work at all.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset