Add a Play Audio Option in Xamarin Forms using a Plugin

0715vsm_McClureXPlat

In the tech market a lot of audio and video devices are available and are successfully creating the awesome entertainment environment, therefore, watching the interest of the present youth in the gadgets and devices which are come along with an in-built audio feature the web and app development industry has also started working on delivering an audio button the applications. In this post here we are going to learn to add an audio play option in the Xamarin.Forms project which we can easily create with the help of a plugin namely Xam.Plugin.SimpleAudioPlayer.

To implement the process in the project for the firstmost step you need to install the plugin package in your project, and then call the project API as,

Call CrossSimpleAudioPlayer.Current

Now design the button layout in your code process page or in the Design Page Xamal file of the project by adding the mentioned code:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<ContentPage xmlns=”http://xamarin.com/schemas/2014/forms”

xmlns:x=”http://schemas.microsoft.com/winfx/2009/xaml”

x:Class=”SAPlayerSample.PCLAudioPage”

Title=”Shared Audio”>

<StackLayout Orientation=”Vertical” Spacing=”5″ Margin=”10″>

<Label Text=”Postion:” />

<Slider x:Name=”sliderPosition” />

<Label x:Name=”lblPosition” />

<Button x:Name=”btnPlay” Text=”Play” BorderColor=”Green” />

<Button x:Name=”btnPause” Text=”Pause” BorderColor=”Blue” />

<Button x:Name=”btnStop” Text=”Stop” BorderColor=”Red” />

<Label Text=”Volume:” />

<Slider x:Name=”sliderVolume” Minimum=”0″ Maximum=”1″/>

</StackLayout>

</ContentPage>

Now, in the final process to add functionality in that audio play button, add the following code in your project code file:

using Plugin.SimpleAudioPlayer.Abstractions;

using System;

using System.Collections.Generic;

using System.IO;

using System.Reflection;

using Xamarin.Forms;

namespace SAPlayerSample

{

public partial class PCLAudioPage : ContentPage

{

ISimpleAudioPlayer player;

public PCLAudioPage()

{

InitializeComponent();

var stream = GetStreamFromFile(“Diminished.mp3″);

            player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();

            player.Load(stream);

            InitControls();

}

void InitControls ()

{

sliderVolume.Value = player.Volume;

btnPlay.Clicked += BtnPlayClicked;

btnPause.Clicked += BtnPauseClicked;

btnStop.Clicked += BtnStopClicked;

sliderVolume.ValueChanged += SliderVolumeValueChanged;

sliderPosition.ValueChanged += SliderPostionValueChanged;

}

private void SliderPostionValueChanged(object sender, ValueChangedEventArgs e)

{

if (sliderPosition.Value != player.Duration)

player.Seek(sliderPosition.Value);

}

private void SliderVolumeValueChanged(object sender, ValueChangedEventArgs e)

{

player.Volume = sliderVolume.Value;

}

private void BtnStopClicked(object sender, EventArgs e)

{

player.Stop();

}

private void BtnPauseClicked(object sender, EventArgs e)

{

player.Pause();

}

private void BtnPlayClicked(object sender, EventArgs e)

{

player.Play();

sliderPosition.Maximum = player.Duration;

sliderPosition.IsEnabled = player.CanSeek;

Device.StartTimer(TimeSpan.FromSeconds(0.5), UpdatePosition);

}

bool UpdatePosition ()

{

lblPosition.Text = $”Postion: {(int)player.CurrentPosition} / {(int)player.Duration}”;

sliderPosition.ValueChanged -= SliderPostionValueChanged;

sliderPosition.Value = player.CurrentPosition;

sliderPosition.ValueChanged += SliderPostionValueChanged;

return player.IsPlaying;

}

Stream GetStreamFromFile(string filename)

{

var assembly = typeof(App).GetTypeInfo().Assembly;

var stream = assembly.GetManifestResourceStream(“SAPlayerSample.” + filename);

return stream;

}

}

}

If you ever want to implement an advance functionality in your Xamarin.Forms then you must get connected with the Hire Xamarin Developers. Here, you can share your project concept and required functionalities in your business application then the smart developers of the team of hire xamarin developers will develop a bug-less application with numerous in-built smooth accessible functionalities.

by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>