Pastoral

Press a tab to play a selection from each movement of Beethoven’s Pastoral Symphony. For a cartoon treatment, see the 1940 Disney Fantasia.

This app has a row of five ViewControllers under a tab bar controller. The tabBarController:didSelectViewController: method of the tab bar controller’s delegate is called when the user presses a tab. This method plays the sound file of the view controller belonging to that tab. If you press the same tab again while the sound is playing, it will immediately restart the file from the beginning.

We want to play the sound file when the user presses the ViewController’s tab, not when the ViewController’s view becomes visible. If we were to play the file when the view became visible, the first movement would starting playing automatically as soon as the app was launched, before any tab was pressed. That’s why we do not play the sound file in the viewDidAppear: method of the ViewController. We can, however, stop the sound file in the viewWillDisappear: method of the ViewController.

Source code in Pastoral.zip

  1. main.m
  2. Class PastoralAppDelegate. The application delegate is also the delegate of the tab bar controller.
  3. Class ViewController. There are five of them. I didn’t bother with tab bar images.
  4. I.mp3 (Allegro ma non troppo)
  5. II.mp3 (Andante molto mosso)
  6. III.mp3 (Allegro)
  7. IV.mp3 (Allegro)
  8. V.mp3 (Allegretto)

Create the project

Drag the five .mp3 files into the Supporting Files folder of the project.
Choose options for adding these files.
Destination ☑ Copy items into destination group’s folder (if required).