Skip to content

Commit

Permalink
Merge pull request #5 from BeautifulPilgrim/Change_Methods_to_Properties
Browse files Browse the repository at this point in the history
Change methods to properties
  • Loading branch information
BeautifulPilgrim committed Oct 20, 2022
2 parents c538fde + ef61e37 commit 7f4bf1e
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 68 deletions.
32 changes: 25 additions & 7 deletions MauiAudio/INativeAudioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,37 @@ public interface INativeAudioService
Task PlayAsync(double position = 0);

Task PauseAsync();

Task SetMuted(bool value);

Task SetVolume(int value);

///<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;
Expand Down
2 changes: 1 addition & 1 deletion MauiAudio/MauiAudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<SupportedOSPlatformVersion Condition="'$(TargetFramework)' == 'net6.0-android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$(TargetFramework.Contains('-windows'))">10.0.17763.0</TargetPlatformMinVersion>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
<PackageId>Plugin.MauiAudio</PackageId>
Expand Down
29 changes: 22 additions & 7 deletions MauiAudio/NativeAudioService.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ namespace MauiAudio;
public class NativeAudioService : INativeAudioService
{
IAudioActivity instance;

double volume = 1;
double balance = 0;
bool muted=false;
private MediaPlayer mediaPlayer => instance != null &&
instance.Binder.GetMediaPlayerService() != null ?
instance.Binder.GetMediaPlayerService().mediaPlayer : null;

public bool IsPlaying => mediaPlayer?.IsPlaying ?? false;
public double Duration=>mediaPlayer?.Duration/1000 ?? 0;
public double CurrentPosition => mediaPlayer?.CurrentPosition / 1000 ?? 0;

public double Volume { get => volume; set { volume = value; SetVolume(volume = value, Balance); } }
public double Balance { get => balance; set { balance = value;SetVolume(Volume, balance = value); } }
public bool Muted { get => muted; set => SetMuted(value); }

public event EventHandler<bool> IsPlayingChanged;
public event EventHandler PlayEnded;
public event EventHandler PlayNext;
Expand All @@ -43,17 +50,24 @@ public async Task PlayAsync(double position = 0)
await instance.Binder.GetMediaPlayerService().Seek((int)position * 1000);
}

public Task SetMuted(bool value)
Task SetMuted(bool value)
{
instance?.Binder.GetMediaPlayerService().SetMuted(value);

muted = value;
if (value)
mediaPlayer.SetVolume(0, 0);
else SetVolume(volume,balance);
return Task.CompletedTask;
}

public Task SetVolume(int value)
Task SetVolume(double volume, double balance)
{
instance?.Binder.GetMediaPlayerService().SetVolume(value);
volume = Math.Clamp(volume, 0, 1);
balance = Math.Clamp(balance, -1, 1);

// Using the "constant power pan rule." See: http://www.rs-met.com/documents/tutorials/PanRules.pdf
var left = Math.Cos((Math.PI * (balance + 1)) / 4) * volume;
var right = Math.Sin((Math.PI * (balance + 1)) / 4) * volume;

mediaPlayer?.SetVolume((float)left, (float)right);
return Task.CompletedTask;
}

Expand Down Expand Up @@ -117,4 +131,5 @@ private async Task<Bitmap> GetImageBitmapFromUrl(string url)

return imageBitmap;
}

}
31 changes: 8 additions & 23 deletions MauiAudio/NativeAudioService.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ public class NativeAudioService : INativeAudioService
//AVPlayer avPlayer;
AVAudioPlayer avPlayer;
string _uri;

float volume = 1;
public bool IsPlaying => avPlayer != null
? avPlayer.Playing
: false;

public double CurrentPosition => avPlayer?.CurrentTime ?? 0;
public double Duration=>avPlayer?.Duration ?? 0;
public double Duration => avPlayer?.Duration ?? 0;

public double Volume { get => volume; set { volume = (float)Math.Clamp(value, 0, 1); avPlayer.Volume = volume; } }
public bool Muted { get => avPlayer?.Volume == 0; set { if (value) avPlayer.Volume = 0; else avPlayer.Volume = volume; } }
public double Balance { get => avPlayer?.Pan ?? 0; set => avPlayer.Pan = (float)Math.Clamp(value, -1, 1); }

public event EventHandler<bool> IsPlayingChanged;
public event EventHandler PlayEnded;
public event EventHandler PlayNext;
Expand Down Expand Up @@ -46,34 +51,14 @@ public Task SetCurrentTime(double value)
return Task.CompletedTask;
}

public Task SetMuted(bool value)
{
if (avPlayer != null)
{
avPlayer.Volume = value ? 100 : 0;
}

return Task.CompletedTask;
}

public Task SetVolume(int value)
{
if (avPlayer != null)
{
avPlayer.Volume = value;
}

return Task.CompletedTask;
}

public Task DisposeAsync()
{
avPlayer?.Dispose();
return Task.CompletedTask;
}

public async Task InitializeAsync(MediaPlay media)
{
{
_uri = media.URL;
NSUrl fileURL = new NSUrl(_uri.ToString());

Expand Down
35 changes: 15 additions & 20 deletions MauiAudio/NativeAudioService.windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ public class NativeAudioService : INativeAudioService
&& mediaPlayer.CurrentState == MediaPlayerState.Playing;
public double Duration => mediaPlayer?.NaturalDuration.TotalSeconds ?? 0;
public double CurrentPosition => mediaPlayer?.Position.TotalSeconds ?? 0;

public double Volume
{
get => mediaPlayer?.Volume ?? 0;


set => mediaPlayer.Volume = Math.Clamp(value, 0, 1);
}
public bool Muted
{
get => mediaPlayer?.IsMuted ?? false;
set => mediaPlayer.IsMuted = value;
}
public double Balance { get => mediaPlayer.AudioBalance; set => mediaPlayer.AudioBalance = Math.Clamp(value, -1, 1); }

public event EventHandler<bool> IsPlayingChanged;
public event EventHandler PlayEnded;
public event EventHandler PlayNext;
Expand Down Expand Up @@ -48,26 +63,6 @@ public Task SetCurrentTime(double value)
}
return Task.CompletedTask;
}

public Task SetMuted(bool value)
{
if (mediaPlayer != null)
{
mediaPlayer.IsMuted = value;
}
return Task.CompletedTask;
}

public Task SetVolume(int value)
{
if (mediaPlayer != null)
{
mediaPlayer.Volume = value != 0 ? value / 100d : 0;
}

return Task.CompletedTask;
}

public Task DisposeAsync()
{
mediaPlayer?.Dispose();
Expand Down
10 changes: 0 additions & 10 deletions MauiAudio/Platforms/Android/MediaPlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,6 @@ private void StartNotification()
MediaPlayerState == PlaybackStateCode.Playing);
}

internal void SetMuted(bool value)
{
mediaPlayer.SetVolume(0, 0);
}

internal void SetVolume(int value)
{
mediaPlayer.SetVolume(value, value);
}

/// <summary>
/// Updates the metadata on the lock screen
/// </summary>
Expand Down
Binary file added README.assets/sample_android.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,59 @@ await audioService.PauseAsync();

## Interface

### Version ≥ 1.0.3

```c#
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

```c#
public interface INativeAudioService
{
Expand Down Expand Up @@ -157,4 +210,10 @@ If you want to process the player's previous or next song:(only Android and Wind

## Sample

### Windows

![Snipaste_2022-10-11_21-35-57](https://github.com/BeautifulPilgrim/MauiAudio/raw/master/README.assets/Snipaste_2022-10-11_21-35-57.png)

### Android

![sample_android](https://github.com/BeautifulPilgrim/MauiAudio/raw/master/README.assets/sample_android.jpg)

0 comments on commit 7f4bf1e

Please sign in to comment.