I'm building a metronome as part of my practice app in Maui. I am using Plugin.maui.audio to play the sounds, and I'm using System.timer to determine the interval at which the sounds should be played. However the sounds are played at in irregular tempo and not in sync with whatever I set the timer.interval to be. I'm a big noob to this, so there is probably an easy explaination for?
I've tried separating creating the audioplayer itself and actually playing it, as the metronome shouldn't create a whole new player and load it for each time the metronome ticks, but I can't seem to get away with splitting up the two lines of code
var audioPlayer = audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("Perc_Can_hi.wav"));
audioPlayer.Play();
Here is the XAML code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="The_Jazz_App.MetronomePage1"
Title="Metronome"
BackgroundColor="DarkOrange">
<VerticalStackLayout Padding="100" Spacing="25">
<Label
Text="Slide to adjust bpm"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<Label
x:Name="bpmValue"
TextColor="Black"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<Slider HorizontalOptions="Fill"
Maximum="400" Minimum="30"
ValueChanged="slider_ValueChanged"
x:Name="slider"/>
<ImageButton
Source="playbutton.png"
Pressed="ImageButton_Pressed"
VerticalOptions="Center"
HeightRequest="50"
WidthRequest="50"/>
<Picker VerticalOptions="Center" HorizontalOptions="Center" Title="Pick metronome sound" TitleColor="Black" TextColor="Black"/>
<Label x:Name="timerIntervalXAML"/>
</VerticalStackLayout>
</ContentPage>
And here is my xaml.cs code:
using System;
using System.Timers;
using System.Threading.Tasks;
using Plugin.Maui.Audio;
namespace The_Jazz_App;
public partial class MetronomePage1 : ContentPage
{
readonly System.Timers.Timer timer;
private readonly IAudioManager audioManager;
//default interval
double timerInterval = 3333;
public MetronomePage1(IAudioManager audioManager)
{
InitializeComponent();
this.audioManager = audioManager;
slider.Value = 200;
timer = new System.Timers.Timer();
timer.Interval = timerInterval;
timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true;
timer.Enabled = false;
}
//The audioplayer itself
private IAudioPlayer audioPlayer;
public async void Play()
{
var audioPlayer = audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("Perc_Can_hi.wav"));
audioPlayer.Play();
}
//Is supposed to play the sound repeatedly at the given BPM
public void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Play();
}
//A slider that lets the user choose the BPM of the metronome
public void slider_ValueChanged(object sender, ValueChangedEventArgs e)
{
double value = slider.Value;
bpmValue.Text = $"{((int)value)} bpm";
timerInterval = value / 60 * 1000;
timerIntervalXAML.Text = timerInterval.ToString();
}
//The button which activates the metronome
public void ImageButton_Pressed(object sender, EventArgs e)
{
if (timer.Enabled == false)
{
timer.Start();
}
else
{
timer.Stop();
}
}
}