Maui- Plugin.Maui.Audio

[删除(380066935@qq.com或微信通知)]

jfversluis/Plugin.Maui.Audio: Plugin.Maui.Audio provides the ability to play audio inside a .NET MAUI application (github.com)

Plugin.Maui.Audio

Plugin.Maui.Audio provides the ability to play audio inside a .NET MAUI application.

Getting Started

API Usage

Plugin.Maui.Audio provides the AudioManager class that allows for the creation of AudioPlayers. The AudioManager can be used with or without dependency injection.

AudioManager

Dependency Injection

You will first need to register the AudioManager with the MauiAppBuilder following the same pattern that the .NET MAUI Essentials libraries follow.

builder.Services.AddSingleton(AudioManager.Current);

You can then enable your classes to depend on IAudioManager as per the following example.

public class AudioPlayerViewModel
{
    readonly IAudioManager audioManager;

    public AudioPlayerViewModel(IAudioManager audioManager)
    {
        this.audioManager = audioManager;
    }

    public async void PlayAudio()
    {
        var audioPlayer = audioManager.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("ukelele.mp3"));

        audioPlayer.Play();
    }
}

Straight usage

Alternatively if you want to skip using the dependency injection approach you can use the AudioManager.Current property.

public class AudioPlayerViewModel
{
    public async void PlayAudio()
    {
        var audioPlayer = AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync("ukelele.mp3"));

        audioPlayer.Play();
    }
}

AudioPlayer

Once you have created an AudioPlayer you can interact with it in the following ways:

Events

PlaybackEnded

Raised when audio playback completes successfully.

Properties

Balance

Gets or sets the balance left/right: -1 is 100% left : 0% right, 1 is 100% right : 0% left, 0 is equal volume left/right.

CanSeek

Gets a value indicating whether the position of the loaded audio file can be updated.

CurrentPosition

Gets the current position of audio playback in seconds.

Duration

Gets the length of audio in seconds.

IsPlaying

Gets a value indicating whether the currently loaded audio file is playing.

Loop

Gets or sets the playback volume 0 to 1 where 0 is no-sound and 1 is full volume.

Volume

Gets or sets whether the player will continuously repeat the currently playing sound.

Methods

Pause()

Pause playback if playing (does not resume).

Play()

Begin playback or resume if paused.

Seek(double position)

Set the current playback position (in seconds).

Stop()

Stop playback and set the current position to the beginning.