Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Playing music and sounds


In this recipe, we will learn how to play both simple audio files and songs stored on the device.

Getting ready

Create a new Single View Application in Xamarin Studio and name it PlayMusicApp.

Note

This example will not work on the simulator. You will also need at least one song stored on the device's iTunes library.

How to do it...

Perform the following steps:

  1. Add three buttons to the view of the controller.

  2. Add the following using directive in the PlayMusicAppViewController.cs file:

    using MonoTouch.MediaPlayer;
  3. Add the following two fields in the class:

    private MPMusicPlayerController musicPlayer;
    private MPMediaPickerController mediaPicker;
  4. Add the following code in the ViewDidLoad method:

    this.mediaPicker = new MPMediaPickerController(MPMediaType.Music);
    this.mediaPicker.ItemsPicked += MediaPicker_ItemsPicked;
    this.mediaPicker.DidCancel += MediaPicker_DidCancel;
    this.musicPlayer = 
      MPMusicPlayerController.ApplicationMusicPlayer;
    this.btnSelect.TouchUpInside += async (s...