Author: jrtilson

What makes a good mobile app?

What do I, an aspiring app developer, think makes a good mobile app?

I think fundamentally, a ‘good’ app is just easy to use. It does it’s job and it does it’s job well. It doesn’t try to be anything more than it is: a mobile app with the mission of helping you complete a specific task.

A good app is more than just easy, it’s intuitive. When you load it up you aren’t left thinking “WTF am I supposed to do now?”. If an app is just a portable version of a popular web service it should be immediately obvious how to use it compared to it’s bigger brother (I’m picturing the Facebook or Twitter apps here).

Personally, I like an app to look good. I can’t be alone in this.. I’ve seen some plain ugly apps in my experience. I don’t think it’s difficult to make an app look good. A barebones beginner iOS app can look good if you don’t try to go overboard with fancy graphics and a weird colour scheme. In addition to looking good, it should follow and implement common UI/Interface patterns. Why re-invent the wheel when you can lean on built in UI frameworks?

Lastly, and if at all possible, I think an app should be fun to use! A good (or great) app will make you say “Whoa, that’s cool!”.

iOS: Audio With Volume Control Using Xcode 7 and Swift 2

So you want to play some audio in your iOS app? This is a relatively simple task, but what if you want to be able to control the volume as well? Let’s dive in and create a super simple app that does just that.

Before we get started, I just want to note that this tutorial was written using Xcode 7 and Swift 2. Since Swift 2 is pretty new, it’s tough to find example code out there that works! I hope that this will be a valuable resource.

Fire up a new Xcode project

For this tutorial I selected the Single View Application template and I’m targeting only the iPhone

New Xcode Project

Add Some Audio

Now we’ve got a play button, we’ll need something to play. Right-click (or Control-click) on your project folder in the Navigator and choose “Add files to <your project>”. Then find your favourite song or any other audio clip. Make sure that you select Copy items if needed!

findsomeaudiofavsong

Set Up The Play Button

If we’re going to play audio, we’ll need a button to start playback. Open up Main.storyboard in the Interface Builder, and drag a Button from the Object Library on to the storyboard:

Mainstoryboardplay

Add an Action to the View Controller

Next we’ll need to add an action so that we can execute some code when the button is pressed. Just Control-drag from the button to ViewController.swift, set the connection to Action and give it a name (ie: playButton)

addplaybuttonaction

Write Some Code To Play Audio

Open up ViewController.swift, and add a new import statement. We need to import the AVFoundation framework:

import UIKit
import AVFoundation

Then we’ll need to make a new class variable to contain our audio player. We’ll be making use of the AVAudioPlayer class to accomplish this. (Add this immediately under the class definition)

var audioPlayer: AVAudioPlayer!

Next add the following code inside the viewDidLoad() function:

if let filePath = NSBundle.mainBundle().pathForResource("my_fav_song", ofType: "mp3", inDirectory: "") {
    // Good, got a file
    let filePathUrl = NSURL.fileURLWithPath(filePath)

    // Try to instantiate the audio player
    do {
        self.audioPlayer = try AVAudioPlayer(contentsOfURL: filePathUrl)
    } catch {
        print(error)
    }
} else {
    print("filePath is empty!")
}

What are we doing here you ask? Here’s what happening:

  1. We’re building the path to our audio file using NSBundle.mainBundle(), which gets the location of the current application on the device (our running app). We then locate the audio file by supplying it’s name and file type.
  2. The AVAudioPlayer class (the class we’ll be using to play our audio file) needs to be instantiated with an NSURL, since our filePath is a string, we use the NSURL.fileURLWithPath() function to convert it.
  3. Next we’re using a do-catch statement to handle any potential errors that come up when we instantiate the AVAudioPlayer

The last thing we need to do to play the audio is to add a few lines of code to the playSound action we created earlier:

self.audioPlayer.currentTime = 0.0
self.audioPlayer.stop()
self.audioPlayer.play()

I think this is pretty self explanatory! But I’ll break it down. First we’re setting the currentTime to 0.0. This will restart the audio if the play button is pressed again. For good measure, we call audioPlayer.stop() to stop the player before starting the audio. Finally we’ll play the audio with audioPlayer.play()

That’s it for code for now. If you run the app in the simulator or on your device. You should be able to tap ‘Play’ and hear your audio!

Volume Control

Now that we’ve got our audio playing, we’re going to add a volume control to our app. To do this we’re going to use the MPVolumeView class. It turns out, this is relatively simply to implement (after much trail and error on my part!).

There is however, one huge caveat: The MPVolumeView will now show up if you test the app in the iOS Simulator. You’ll need to test the app directly on your iPhone to get it to work.

To add the MPVolumeView, drag a generic View object onto the storyboard in the Interface Builder. Then, under the Identity Inspector, enter MPVolumeView as the view’s custom class.

genericview

Then for good measure, create and outlet for the volume view:

volumeviewoutlet

Lastly, you’ll want to set the background of your app view to something dark, as the MPVolumeView wont show up with a white background. I chose a dark grey with a white tint:

background

That’s it! If you run the app on your iPhone, you’ll now be able to control the volume of playback. Here’s what it looks like:

audiovolume

Bonus Feature!

Did you notice that icon on the right of the volume slider? You’ve probably seen it before. Adding the MPVolumeView also gives us the ability to route the audio to an external source (ie: an Apple TV)

bonus