Skip to content

Commit

Permalink
[AudioIO] Add new internal API for play audio with repetitions (Samsu…
Browse files Browse the repository at this point in the history
…ng#6395)

* [AudioIO] Add new internal API for play audio with repetitions
  • Loading branch information
hsgwon authored and Eunki, Hong committed Oct 10, 2024
1 parent 01c4437 commit 0bc0495
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
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

0 comments on commit 0bc0495

Please sign in to comment.