Skip to content

amellini/MauiAudio

 
 

Repository files navigation

MauiAudio-Cross platform audio plugin for MAUI

.NET Nuget Nuget

Based from .NET Podcasts - Sample Application

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

In CreateMauiApp()[MauiProgram.cs]

Version ≥ 1.0.3:

using MauiAudio;

builder.UseMauiAudio()

Version < 1.0.3:

#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

Android

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; }
        public string URL { get; set; } # URL is must
        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

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;

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%