Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public int SelectedSampleRate

async void PlayAudio()
{
if (audioSource != null)
if (audioSource is not null)
{
audioPlayer = this.audioManager.CreateAsyncPlayer(((FileAudioSource)audioSource).GetAudioStream());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public double Volume
get => audioPlayer?.Volume ?? 1;
set
{
if (audioPlayer != null)
if (audioPlayer is not null)
{
audioPlayer.Volume = value;
}
Expand All @@ -142,7 +142,7 @@ public double Balance
get => audioPlayer?.Balance ?? 0;
set
{
if (audioPlayer != null)
if (audioPlayer is not null)
{
audioPlayer.Balance = value;
}
Expand Down
18 changes: 15 additions & 3 deletions src/Plugin.Maui.Audio/AudioMixer/AudioMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ public class AudioMixer : IDisposable
/// <param name="numberOfChannels">The number of audio channels to manage.</param>
public AudioMixer(IAudioManager audioManager, int numberOfChannels)
{
if (audioManager == null)
if (audioManager is null)
{
throw new ArgumentNullException(nameof(audioManager));
}

if (numberOfChannels <= 0)
{
throw new ArgumentOutOfRangeException(nameof(numberOfChannels), "Number of channels must be positive.");
}

this.audioManager = audioManager;
ChannelCount = numberOfChannels;
Expand Down Expand Up @@ -94,8 +98,10 @@ public void Play(int channelIndex, bool loop = false)
public void Play(int channelIndex, IAudioSource audioClip, bool loop = false)
{
ValidateChannelIndex(channelIndex);
if (audioClip == null)
if (audioClip is null)
{
throw new ArgumentNullException(nameof(audioClip));
}

var player = channels[channelIndex];
player.Stop();
Expand Down Expand Up @@ -177,8 +183,10 @@ public void PlayAll()
public void SetSource(int channelIndex, IAudioSource audioClip)
{
ValidateChannelIndex(channelIndex);
if (audioClip == null)
if (audioClip is null)
{
throw new ArgumentNullException(nameof(audioClip));
}

var player = channels[channelIndex];
player.Stop();
Expand Down Expand Up @@ -244,7 +252,9 @@ public void SetVolume(int channelIndex, float volume)
void ValidateChannelIndex(int channelIndex)
{
if (channelIndex < 0 || channelIndex >= ChannelCount)
{
throw new ArgumentOutOfRangeException(nameof(channelIndex), $"Channel index must be between 0 and {ChannelCount - 1}.");
}
}

/// <summary>
Expand Down Expand Up @@ -307,7 +317,9 @@ public float GetDistanceDecay(float dist)
protected virtual void Dispose(bool disposing)
{
if (IsDisposed)
{
return;
}

foreach (var player in channels)
{
Expand Down
9 changes: 7 additions & 2 deletions src/Plugin.Maui.Audio/AudioPlayer/AudioPlayer.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ public bool Loop

void PrepareAudioSource()
{
if (audioBytes == null && string.IsNullOrWhiteSpace(file))
if (audioBytes is null && string.IsNullOrWhiteSpace(file))
{
throw new ArgumentException("audio source is not set");
}

if (audioBytes != null && OperatingSystem.IsAndroidVersionAtLeast(23))
if (audioBytes is not null && OperatingSystem.IsAndroidVersionAtLeast(23))
{
stream = new MemoryStream(audioBytes);
var mediaSource = new StreamMediaDataSource(stream);
Expand All @@ -172,6 +172,11 @@ void PrepareAudioSource()
}
else
{
if (file is null)
{
throw new FailedToLoadAudioException("Provided file was null");
}

AssetFileDescriptor afd = Android.App.Application.Context.Assets?.OpenFd(file)
?? throw new FailedToLoadAudioException("Unable to create AssetFileDescriptor.");

Expand Down
4 changes: 2 additions & 2 deletions src/Plugin.Maui.Audio/AudioPlayer/AudioPlayer.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public bool Loop

internal AudioPlayer(AudioPlayerOptions audioPlayerOptions)
{
if (emptySource == null)
if (emptySource is null)
{
byte[] empty = new byte[16];
int sampleRate = 44100;
Expand All @@ -127,7 +127,7 @@ internal AudioPlayer(AudioPlayerOptions audioPlayerOptions)
/// <exception cref="FailedToLoadAudioException">Thrown when the audio stream cannot be loaded.</exception>
public void SetSource(Stream audioStream)
{
if (player != null)
if (player is not null)
{
player.FinishedPlaying -= OnPlayerFinishedPlaying;
player.DecoderError -= OnPlayerError;
Expand Down
30 changes: 21 additions & 9 deletions src/Plugin.Maui.Audio/AudioRecorder/AudioRecorder.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AudioRecorder(AudioRecorderOptions options)
var packageManager = Android.App.Application.Context.PackageManager;

CanRecordAudio = packageManager?.HasSystemFeature(Android.Content.PM.PackageManager.FeatureMicrophone) ?? false;
this.audioRecorderOptions = options;
audioRecorderOptions = options;
}

public Task StartAsync(AudioRecorderOptions? options = null) => StartAsync(GetTempFilePath(), options);
Expand All @@ -57,17 +57,17 @@ public Task StartAsync(string filePath, AudioRecorderOptions? recordingOptions =

if (recordingOptions is not null)
{
this.audioRecorderOptions = recordingOptions;
audioRecorderOptions = recordingOptions;
}

audioFilePath = filePath;

// solve some parameters needed for AudioRecord/MediaRecorder
ChannelIn channelIn =
SharedChannelTypesToAndroidChannelTypes(audioRecorderOptions.Channels, audioRecorderOptions.ThrowIfNotSupported);
this.sampleRate = audioRecorderOptions.SampleRate;
this.bitDepth = (int)audioRecorderOptions.BitDepth;
this.channels = channelIn == ChannelIn.Mono ? 1 : 2;
sampleRate = audioRecorderOptions.SampleRate;
bitDepth = (int)audioRecorderOptions.BitDepth;
channels = channelIn == ChannelIn.Mono ? 1 : 2;
int bitRate = audioRecorderOptions.BitRate;
int numChannels = (int)audioRecorderOptions.Channels;

Expand Down Expand Up @@ -124,10 +124,22 @@ public Task StartAsync(string filePath, AudioRecorderOptions? recordingOptions =
}

// Create MediaRecorder
mediaRecorder =
new MediaRecorder(Platform.CurrentActivity
.ApplicationContext); //needs context, obsoleted without context https://stackoverflow.com/questions/73598179/deprecated-mediarecorder-new-mediarecorder#73598440

if (OperatingSystem.IsAndroidVersionAtLeast(31))
{
if (Platform.CurrentActivity?.ApplicationContext is null)
{
throw new FailedToStartRecordingException("Android ApplicationContext is null");
}

//needs context, obsoleted without context https://stackoverflow.com/questions/73598179/deprecated-mediarecorder-new-mediarecorder#73598440
mediaRecorder =
new MediaRecorder(Platform.CurrentActivity.ApplicationContext);
}
else
{
mediaRecorder = new MediaRecorder();
}

mediaRecorder.Reset();
mediaRecorder.SetAudioSource(AudioSource.Mic);
mediaRecorder.SetOutputFormat(outputFormat);
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin.Maui.Audio/AudioRecorder/AudioRecorder.windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ partial class AudioRecorder : IAudioRecorder
string audioFilePath = string.Empty;

public bool CanRecordAudio { get; private set; } = true;
public bool IsRecording => mediaCapture != null;
public bool IsRecording => mediaCapture is not null;

AudioRecorderOptions audioRecorderOptions;
static readonly AudioRecorderOptions defaultOptions = new AudioRecorderOptions();
Expand Down Expand Up @@ -56,7 +56,7 @@ public async Task StartAsync(string filePath, AudioRecorderOptions? options = nu
CanRecordAudio = false;
DeleteMediaCapture();

if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UnauthorizedAccessException))
if (ex.InnerException is not null && ex.InnerException.GetType() == typeof(UnauthorizedAccessException))
{
throw ex.InnerException;
}
Expand Down Expand Up @@ -152,7 +152,7 @@ async Task InitMediaCapture(MediaCaptureInitializationSettings settings)

public async Task<IAudioSource> StopAsync()
{
if (mediaCapture == null)
if (mediaCapture is null)
{
throw new InvalidOperationException("No recording in progress");
}
Expand Down
13 changes: 7 additions & 6 deletions src/Plugin.Maui.Audio/AudioStreamer/AudioStream.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Task Start()

BufferOperation(() => audioQueue.AllocateBuffer(bufferByteSize, out bufferPtr), () =>
{
BufferOperation(() => audioQueue.EnqueueBuffer(bufferPtr, bufferByteSize, null), () => Debug.WriteLine("AudioQueue buffer enqueued :: {0} of {1}", index + 1, countAudioBuffers));
BufferOperation(() => audioQueue.EnqueueBuffer(bufferPtr, bufferByteSize, null!), () => Debug.WriteLine("AudioQueue buffer enqueued :: {0} of {1}", index + 1, countAudioBuffers));
});
}

Expand Down Expand Up @@ -150,11 +150,12 @@ void QueueInputCompleted(object? sender, InputCompletedEventArgs e)
// check if active again, because the auto stop logic may stop the audio queue from within this handler!
if (Active)
{
BufferOperation(() => audioQueue.EnqueueBuffer(e.IntPtrBuffer, null), null, status =>
{
Debug.WriteLine("AudioStream.QueueInputCompleted() :: audioQueue.EnqueueBuffer returned non-Ok status :: {0}", status);
OnException?.Invoke(this, new Exception($"audioQueue.EnqueueBuffer returned non-Ok status :: {status}"));
});
BufferOperation(() => audioQueue?.EnqueueBuffer(e.IntPtrBuffer, null!) ?? AudioQueueStatus.InvalidBuffer,
null, status =>
{
Debug.WriteLine("AudioStream.QueueInputCompleted() :: audioQueue.EnqueueBuffer returned non-Ok status :: {0}", status);
OnException?.Invoke(this, new Exception($"audioQueue.EnqueueBuffer returned non-Ok status :: {status}"));
});
}
}
catch (Exception ex)
Expand Down
29 changes: 17 additions & 12 deletions src/Plugin.Maui.Audio/AudioStreamer/AudioStreamer.net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,43 @@ public partial class AudioStreamer : IAudioStreamer
/// <summary>
/// Gets whether the device is capable of streaming audio.
/// </summary>
public bool CanStreamAudio => false;
public bool CanStreamAudio => throw new PlatformNotSupportedException();

/// <summary>
/// Gets whether the streamer is currently streaming audio.
/// </summary>
public bool IsStreaming => false;
public bool IsStreaming => throw new PlatformNotSupportedException();

/// <summary>
/// Gets the audio streaming options.
/// </summary>
public AudioStreamOptions Options { get; } = AudioManager.Current.DefaultStreamerOptions;
public AudioStreamOptions Options => throw new PlatformNotSupportedException();

/// <summary>
/// Captured linear PCM audio (raw WAV audio)
/// </summary>
public event EventHandler<AudioStreamEventArgs>? OnAudioCaptured;
public event EventHandler<AudioStreamEventArgs>? OnAudioCaptured
{
add
{
throw new PlatformNotSupportedException();
}

remove
{
throw new PlatformNotSupportedException();
}
}

/// <summary>
/// Start streaming audio to <see cref="OnAudioCaptured"/>.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public Task StartAsync()
{
return Task.CompletedTask;
}
public Task StartAsync() => throw new PlatformNotSupportedException();

/// <summary>
/// Stop streaming.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public Task StopAsync()
{
return Task.CompletedTask;
}
public Task StopAsync() => throw new PlatformNotSupportedException();
}
2 changes: 1 addition & 1 deletion src/Plugin.Maui.Audio/Plugin.Maui.Audio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" IsImplicitlyDefined="true" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls" Version="9.0.0" />
</ItemGroup>

<!-- Package additions -->
Expand Down
Loading
Loading