Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AudioIO] Add new internal API for play audio with repetitions #6395

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
4 changes: 4 additions & 0 deletions src/Tizen.Multimedia.AudioIO/Interop/Interop.WavPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ internal static partial class WavPlayer
internal static extern WavPlayerError Start(string filePath, AudioStreamPolicyHandle streamInfoHandle,
WavPlayerCompletedCallback completedCallback, IntPtr userData, out int id);

[DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_start_loop")]
internal static extern WavPlayerError StartLoop(string filePath, AudioStreamPolicyHandle streamInfoHandle, uint count,
WavPlayerCompletedCallback completedCallback, IntPtr userData, out int id);

[DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_stop")]
internal static extern WavPlayerError Stop(int id);
}
Expand Down
51 changes: 47 additions & 4 deletions src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -85,20 +86,62 @@ public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
}

return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
StartAsyncCore(path, streamPolicy, cancellationToken);
StartAsyncCore(path, streamPolicy, 1, cancellationToken);
}

private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy,
/// <summary>
/// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/> with given repetition number.
/// </summary>
/// <remarks>If loopCount is 0, it means infinite loops</remarks>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <param name="path">A file path to play.</param>
/// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
/// <param name="loopCount">A number of repetitions.</param>
/// <param name="cancellationToken">A cancellation token which can be used to stop.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="path"/> is null.
/// <para>-or-</para>
/// <paramref name="streamPolicy"/> is null.
/// </exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
/// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public static Task StartAsync(string path, AudioStreamPolicy streamPolicy, uint loopCount,
CancellationToken cancellationToken)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}

if (streamPolicy == null)
{
throw new ArgumentNullException(nameof(streamPolicy));
}

if (File.Exists(path) == false)
{
throw new FileNotFoundException("File does not exists.", path);
}

return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
StartAsyncCore(path, streamPolicy, loopCount, cancellationToken);
}

private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy, uint loopCount,
CancellationToken cancellationToken)
{
int id = 0;
var tcs = new TaskCompletionSource<bool>();

Native.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true);

using (var cbKeeper = ObjectKeeper.Get(cb))
{
Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id).
Validate("Failed to play.");
Native.StartLoop(path, streamPolicy.Handle, loopCount, cb, IntPtr.Zero, out id).
Validate("Failed to play with loop.");

using (RegisterCancellationAction(tcs, cancellationToken, id))
{
Expand Down
Loading