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

Recording with the microphone


In this recipe, we will learn how to use the device's microphone to record sounds.

Getting ready

Create a new project in Xamarin Studio and name it RecordSoundApp.

Note

This example will not work on the simulator.

How to do it...

Perform the following steps:

  1. Add two buttons and a label to the view of the controller.

  2. Enter the following using directives in the RecordSoundAppViewController.cs file:

    using System.IO;
    using MonoTouch.AVFoundation;
    using MonoTouch.AudioToolbox;
  3. Override the ViewDidLoad method and add the following code to it:

    NSUrl soundFileUrl = null;
    NSError error = null;
    AVAudioSession session = AVAudioSession.SharedInstance();
    session.SetCategory(AVAudioSession.CategoryPlayAndRecord, out error);
    session.SetActive(true, out error);
    bool grantedPermission = false;
    session.RequestRecordPermission((granted) => {
      if (granted) {
        grantedPermission = true;
        string soundFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal...