70 lines
2.0 KiB
Plaintext
70 lines
2.0 KiB
Plaintext
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Media;
|
|
|
|
namespace SafeBagDE70.Services
|
|
{
|
|
public static class SoundManager
|
|
{
|
|
private static MediaPlayer _mediaPlayer = new MediaPlayer();
|
|
private static Uri _audioFileUri;
|
|
private static bool _isWarningActive = false;
|
|
|
|
private static MediaPlayer _pressPlayer = new MediaPlayer();
|
|
private static Uri _audioPressFileUri;
|
|
|
|
|
|
static SoundManager()
|
|
{
|
|
var audioFilePath = "assets/audio/alarm.mp3";
|
|
_audioFileUri = new Uri(audioFilePath, UriKind.RelativeOrAbsolute);
|
|
_mediaPlayer.MediaEnded += MediaPlayer_MediaEnded;
|
|
|
|
var audioPressPath = "assets/audio/buttonpress.mp3";
|
|
_audioPressFileUri = new Uri(audioPressPath, UriKind.RelativeOrAbsolute);
|
|
//_pressPlayer.MediaEnded += _pressPlayer_MediaEnded; ;
|
|
}
|
|
|
|
private static void _pressPlayer_MediaEnded(object? sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
public static void StartButtonPressSound()
|
|
{
|
|
_pressPlayer.Open(_audioPressFileUri);
|
|
_pressPlayer.Play();
|
|
}
|
|
|
|
// Starts the continuous playback
|
|
public static void StartWarningSound()
|
|
{
|
|
if (!_isWarningActive)
|
|
{
|
|
_isWarningActive = true;
|
|
_mediaPlayer.Open(_audioFileUri);
|
|
_mediaPlayer.Play();
|
|
}
|
|
}
|
|
|
|
// Stops the playback
|
|
public static void StopWarningSound()
|
|
{
|
|
_mediaPlayer.Stop();
|
|
_isWarningActive= false;
|
|
}
|
|
|
|
// Handles the MediaEnded event to replay the audio
|
|
private static void MediaPlayer_MediaEnded(object sender, EventArgs e)
|
|
{
|
|
if (_isWarningActive)
|
|
{
|
|
_mediaPlayer.Position = TimeSpan.Zero;
|
|
_mediaPlayer.Play();
|
|
}
|
|
}
|
|
}
|
|
}
|