Skip to content

Commit a52c0e6

Browse files
authored
[AudioIO] Add new internal API for play audio with repetitions (Samsung#6395)
* [AudioIO] Add new internal API for play audio with repetitions
1 parent 2fa4078 commit a52c0e6

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/Tizen.Multimedia.AudioIO/Interop/Interop.WavPlayer.cs

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ internal static partial class WavPlayer
2929
internal static extern WavPlayerError Start(string filePath, AudioStreamPolicyHandle streamInfoHandle,
3030
WavPlayerCompletedCallback completedCallback, IntPtr userData, out int id);
3131

32+
[DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_start_loop")]
33+
internal static extern WavPlayerError StartLoop(string filePath, AudioStreamPolicyHandle streamInfoHandle, uint count,
34+
WavPlayerCompletedCallback completedCallback, IntPtr userData, out int id);
35+
3236
[DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_stop")]
3337
internal static extern WavPlayerError Stop(int id);
3438
}

src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayer.cs

+47-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
using System;
18+
using System.ComponentModel;
1819
using System.IO;
1920
using System.Threading;
2021
using System.Threading.Tasks;
@@ -85,20 +86,62 @@ public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
8586
}
8687

8788
return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
88-
StartAsyncCore(path, streamPolicy, cancellationToken);
89+
StartAsyncCore(path, streamPolicy, 1, cancellationToken);
8990
}
9091

91-
private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy,
92+
/// <summary>
93+
/// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/> with given repetition number.
94+
/// </summary>
95+
/// <remarks>If loopCount is 0, it means infinite loops</remarks>
96+
/// <returns>A task that represents the asynchronous operation.</returns>
97+
/// <param name="path">A file path to play.</param>
98+
/// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
99+
/// <param name="loopCount">A number of repetitions.</param>
100+
/// <param name="cancellationToken">A cancellation token which can be used to stop.</param>
101+
/// <exception cref="ArgumentNullException">
102+
/// <paramref name="path"/> is null.
103+
/// <para>-or-</para>
104+
/// <paramref name="streamPolicy"/> is null.
105+
/// </exception>
106+
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
107+
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
108+
/// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
109+
/// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
110+
[EditorBrowsable(EditorBrowsableState.Never)]
111+
public static Task StartAsync(string path, AudioStreamPolicy streamPolicy, uint loopCount,
112+
CancellationToken cancellationToken)
113+
{
114+
if (path == null)
115+
{
116+
throw new ArgumentNullException(nameof(path));
117+
}
118+
119+
if (streamPolicy == null)
120+
{
121+
throw new ArgumentNullException(nameof(streamPolicy));
122+
}
123+
124+
if (File.Exists(path) == false)
125+
{
126+
throw new FileNotFoundException("File does not exists.", path);
127+
}
128+
129+
return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
130+
StartAsyncCore(path, streamPolicy, loopCount, cancellationToken);
131+
}
132+
133+
private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy, uint loopCount,
92134
CancellationToken cancellationToken)
93135
{
136+
int id = 0;
94137
var tcs = new TaskCompletionSource<bool>();
95138

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

98141
using (var cbKeeper = ObjectKeeper.Get(cb))
99142
{
100-
Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id).
101-
Validate("Failed to play.");
143+
Native.StartLoop(path, streamPolicy.Handle, loopCount, cb, IntPtr.Zero, out id).
144+
Validate("Failed to play with loop.");
102145

103146
using (RegisterCancellationAction(tcs, cancellationToken, id))
104147
{

0 commit comments

Comments
 (0)