Plugin.MauiAudio 1.2.2

dotnet add package Plugin.MauiAudio --version 1.2.2
                    
NuGet\Install-Package Plugin.MauiAudio -Version 1.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.MauiAudio" Version="1.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Plugin.MauiAudio" Version="1.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Plugin.MauiAudio" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Plugin.MauiAudio --version 1.2.2
                    
#r "nuget: Plugin.MauiAudio, 1.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Plugin.MauiAudio@1.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Plugin.MauiAudio&version=1.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Plugin.MauiAudio&version=1.2.2
                    
Install as a Cake Tool

MauiAudio-Cross platform audio plugin for MAUI

.NET Nuget Nuget

Based from .NET Podcasts - Sample Application

Intro

An Audio Plugin in MAUI with native control.

A code novice, please forgive me if the documentation or code is not standardized. If you can help improve it, I would be very grateful!

Installation

Add the NuGet package to the projects you want to use it in.

  • Select the Browse tab, search for Plugin.MauiAudio
  • Select Plugin.MauiAudio

Init

Version ≥ 1.2.2

No Need init! Directly using it!

var audioService = NativeAudioService.Current;
Version ≥ 1.0.3:

In CreateMauiApp()[MauiProgram.cs]

using MauiAudio;

builder.UseMauiAudio()
Version < 1.0.3:

In CreateMauiApp()[MauiProgram.cs]

#if WINDOWS
        builder.Services.TryAddSingleton<MauiAudio.INativeAudioService, MauiAudio.Platforms.Windows.NativeAudioService>();
#elif ANDROID
        builder.Services.TryAddSingleton<MauiAudio.INativeAudioService, MauiAudio.Platforms.Android.NativeAudioService>();
#elif MACCATALYST
        builder.Services.TryAddSingleton<MauiAudio.INativeAudioService, MauiAudio.Platforms.MacCatalyst.NativeAudioService>();
        builder.Services.TryAddSingleton< Platforms.MacCatalyst.ConnectivityService>();
#elif IOS
        builder.Services.TryAddSingleton<MauiAudio.INativeAudioService, MauiAudio.Platforms.iOS.NativeAudioService>();
#endif

Platform/Android

AndroidManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" />

MainActivity.cs

using Android.Content;
using MauiAudio.Platforms.Android;
using MauiAudio.Platforms.Android.CurrentActivity;

public class MainActivity : MauiAppCompatActivity,IAudioActivity
{
    MediaPlayerServiceConnection mediaPlayerServiceConnection;

    public MediaPlayerServiceBinder Binder { get; set; }

    public event StatusChangedEventHandler StatusChanged;
    public event CoverReloadedEventHandler CoverReloaded;
    public event PlayingEventHandler Playing;
    public event BufferingEventHandler Buffering;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        CrossCurrentActivity.Current.Init(this, savedInstanceState);
        NotificationHelper.CreateNotificationChannel(ApplicationContext);
        if (mediaPlayerServiceConnection == null)
            InitializeMedia();
    }

    private void InitializeMedia()
    {
        mediaPlayerServiceConnection = new MediaPlayerServiceConnection(this);
        var mediaPlayerServiceIntent = new Intent(ApplicationContext, typeof(MediaPlayerService));
        BindService(mediaPlayerServiceIntent, mediaPlayerServiceConnection, Bind.AutoCreate);
    }
}

Usage

private readonly INativeAudioService audioService;
public async Task PlayAsync(string url)
    {
        await audioService.InitializeAsync(url);
        await audioService.PlayAsync(position);
    }

or use MediaPlay:

private readonly INativeAudioService audioService;
public class MediaPlay
    {
        public string Name { get; set; }
        public string Author { get; set; }
        // URL or Stream is must
        public string URL { get; set; }
        public Stream Stream { get; set; }
        public string Image { get; set; }
    }

MediaPlay media=new(){...};
await audioService.InitializeAsync(media);
# play
# position is double (second)
await InternalPlayAsync(position);
# pause
await audioService.PauseAsync();

Interface

Version ≥ 1.2.2

public interface INativeAudioService
{
    public static INativeAudioService Current;
    ······
}

Version ≥ 1.0.6

public interface INativeAudioService
{
    Task InitializeAsync(string audioURI);
    Task InitializeAsync(MediaPlay media);
    Task PlayAsync(double position = 0);

    Task PauseAsync();
    ///<Summary>
    /// Set the current playback position (in seconds).
    ///</Summary>
    Task SetCurrentTime(double value);

    Task DisposeAsync();
    ///<Summary>
    /// Gets a value indicating whether the currently loaded audio file is playing.
    ///</Summary>
    bool IsPlaying { get; }
    ///<Summary>
    /// Gets the current position of audio playback in seconds.
    ///</Summary>
    double CurrentPosition { get; }
    ///<Summary>
    /// Gets the length of audio in seconds.
    ///</Summary>
    double Duration { get; }
    ///<Summary>
    /// Gets or sets the playback volume 0 to 1 where 0 is no-sound and 1 is full volume.
    ///</Summary>
    double Volume { get; set; }
    /// <summary>
    /// Gets or sets the playback volume muted. false means not mute; true means mute.
    /// </summary>
    bool Muted { get; set; }

    ///<Summary>
    /// 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.
    ///</Summary>
    double Balance { get; set; }

    event EventHandler<bool> IsPlayingChanged;
    event EventHandler PlayEnded;
    event EventHandler PlayNext;
    event EventHandler PlayPrevious;
}

version<1.0.6

public interface INativeAudioService
{
    Task InitializeAsync(string audioURI);
    Task InitializeAsync(MediaPlay media);
    Task PlayAsync(double position = 0);

    Task PauseAsync();

    Task SetMuted(bool value);

    Task SetVolume(int value);

    Task SetCurrentTime(double value);

    ValueTask DisposeAsync();

    bool IsPlaying { get; }

    double CurrentPosition { get; }
    double Duration { get; }

    event EventHandler<bool> IsPlayingChanged;
    event EventHandler PlayEnded;
    event EventHandler PlayNext;
    event EventHandler PlayPrevious;
}

Notify

If you want to process the player's previous or next song:(only Android and Windows available now), preprocess the four EventHandlers.

    event EventHandler<bool> IsPlayingChanged;
    event EventHandler PlayEnded;
    event EventHandler PlayNext;
    event EventHandler PlayPrevious;

Sample

Windows

Snipaste_2022-10-11_21-35-57

Android

sample_android

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-android31.0 is compatible.  net6.0-ios was computed.  net6.0-ios15.4 is compatible.  net6.0-maccatalyst was computed.  net6.0-maccatalyst15.4 is compatible.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net6.0-windows10.0.19041 is compatible.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.2 1,934 7/14/2023
1.2.1 1,966 11/15/2022
1.2.0 430 11/14/2022
1.0.7 643 10/28/2022
1.0.6 556 10/20/2022
1.0.5 531 10/10/2022
1.0.4 770 9/7/2022
1.0.3 567 7/26/2022
1.0.2 464 7/26/2022
1.0.1 549 7/11/2022
1.0.0 515 7/10/2022