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 audio in the background


In this recipe, we will learn how to prevent the app from being suspended in order to allow audio playback.

Getting ready

Create a new Single View Application in Xamarin Studio and name it BackgroundAudioApp. Add a button on the view of the controller. You will also need an audio file. In this example, an M4A file with a duration of 21 seconds is used.

How to do it...

Perform the following steps:

  1. Double-click on the Info.plist file to open it. Select the Source tab at the bottom and add the UIBackgroundModes key (Required background modes) with the string value audio. The following screenshot shows you how the key and value are shown in the editor after they have been set:

  2. Add the MonoTouch.AVFoundation namespace in the BackgroundAudioAppViewController.cs file.

  3. Enter the following ViewDidLoad method in the class:

    private AVAudioPlayer audioPlayer;
    public override void ViewDidLoad ()
    {
      base.ViewDidLoad ();
      NSError error = null;
      AVAudioSession.SharedInstance(...