Volume Control Using AVAudioPlayer In Xamarin iOS App

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example - Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

AVAudioPlayer

AVAudioPlayer class calls an audio player and provides playback of audio data from a file or memory.
Using an AVAudioPlayer
  1. Play sounds of any duration.
  2. Play sounds from files or memory buffers.
  3. Loop sounds.
  4. Play multiple sounds simultaneously, one sound per audio player, with precise synchronization.
  5. Control relative playback level, stereo positioning, and playback rate for each sound you are playing.
  6. Seek to a particular point in a sound file, which supports such application features as fast forward and rewind.
  7. Obtain data you can use for playback-level metering.
Prerequisites
  • Xamarin Studio.
  • Xcode.
The steps given below are required to be followed in order to Control Volume Using AVAudioPlayer in Xamarin iOS, using Xamarin Studio.

Step 1

Go To Xamarin Studio.

Click New Solution—> select iOS—>select App--> Choose single View App. Afterwards, click Next.



Step 2

In this step, configure your app. Give the app name (Ex - sample) and organization identifier. Afterwards, click Next.



Step 3

In this step, give your project name (Ex: Sample) and solution name (Ex: Sample). Give the path of your project. Afterwards, click Create.



Step 4

Subsequently, go to the solution. In the solution, you will get all the files and sources in your project.
 
Now, select Main.storyboard and double click to open Main.storyboard page.



Step 5

After opening the Main.storyboard, you can design this page as per your desire.



Step 6

In this step, design your app using Storyboard, Toolbox and Properties, and add the following three components.
  1. Slider (volumeControlSlider).
  2. Label (lblVolume).
  3. Button (btnPlay).


Step 7

In this step, add one folder and name it as Sounds.

Go to Solution—>Resource—>Right click—>Add—>New Folder.



Step 8

In this step, add a sound fiel from your Local system.

Go to Solution—>Resource-->Right click-->Add-->Add Files. Now, you can choose the required sound file. Click Open.

Next, choose "Copy the Files to the directory". Afterwards, click OK.



Step 9

In this step, go to the ViewController.cs page and write the code given below.

ViewController.cs
  1. using System;  
  2. using AVFoundation;  
  3. using Foundation;  
  4. using UIKit;  
  5. namespace XamariniOSVolumeControl {  
  6.     public partial class ViewController: UIViewController {  
  7.         public AVAudioPlayer player;  
  8.         public float MusicVolume {  
  9.             get;  
  10.             set;  
  11.         } = 0;  
  12.         public bool MusicOn {  
  13.             get;  
  14.             set;  
  15.         } = true;  
  16.         protected ViewController(IntPtr handle): base(handle) {  
  17.             // Note: this .ctor should not contain any initialization logic.  
  18.         }  
  19.         public override void ViewDidLoad() {  
  20.             base.ViewDidLoad();  
  21.             volumeControlSlider.MaxValue = 10;  
  22.             volumeControlSlider.MinValue = 0;  
  23.             volumeControlSlider.ValueChanged += (sender, e) => {  
  24.                 MusicVolume = volumeControlSlider.Value;  
  25.                 lblVoume.Text = Math.Round(volumeControlSlider.Value).ToString();  
  26.             };  
  27.         }  
  28.         partial void BtnPlay_TouchUpInside(UIButton sender) {  
  29.             PlayMusic();  
  30.         }  
  31.         public void PlayMusic() {  
  32.             NSUrl songURL;  
  33.             if (!MusicOn) return;  
  34.             songURL = new NSUrl("Sounds/song.mp3");  
  35.             NSError err;  
  36.             player = new AVAudioPlayer(songURL, "mp3", out err);  
  37.             player.Volume = MusicVolume;  
  38.             player.FinishedPlaying += delegate {  
  39.                 player = null;  
  40.             };  
  41.             player.Play();  
  42.         }  
  43.         public override void DidReceiveMemoryWarning() {  
  44.             base.DidReceiveMemoryWarning();  
  45.             // Release any cached data, images, etc that aren't in use.  
  46.         }  
  47.     }  
  48. }  


Step 10

Now, go to Run option, choose Debug. It will open the list of the available iPhone and iPad simulators. You can choose any one simulator and run it.



Output

After a few seconds, the app will start running on your iPhone simulator. You will see your app working successfully. You can slide to control volume, it must work successfully.



Summary

That's it. This was the process of integrating Volume Control using AVAudioPlayer in Xamarin iOS app.