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!
Add the NuGet package to the projects you want to use it in.
- Select the Browse tab, search for Plugin.MauiAudio
- Select Plugin.MauiAudio
In CreateMauiApp()[MauiProgram.cs]
using MauiAudio;
builder.UseMauiAudio()
#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
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);
}
}
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();
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;
}
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;