2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Threading;
19 using System.Threading.Tasks;
20 using Native = Interop.TonePlayer;
22 namespace Tizen.Multimedia
25 /// Provides the ability to play a tone.
27 public static class TonePlayer
30 /// Plays a tone, asynchronously.
32 /// <param name="tone">A <see cref="ToneType"/> to play.</param>
33 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
34 /// <param name="durationMilliseconds">The tone duration in milliseconds. -1 indicates an infinite duration.</param>
35 /// <returns>A task that represents the asynchronous operation.</returns>
36 /// <exception cref="ArgumentException"><paramref name="tone"/> is invalid.</exception>
37 /// <exception cref="ArgumentNullException"><paramref name="streamPolicy"/> is null.</exception>
38 /// <exception cref="ArgumentOutOfRangeException"><paramref name="durationMilliseconds"/> is less than -1.</exception>
39 /// <exception cref="InvalidOperationException">Any invalid operations occurred.</exception>
40 /// <exception cref="NotSupportedException"><paramref name="tone"/> is not a supported type.</exception>
41 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
42 public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy,
43 int durationMilliseconds)
45 return StartAsync(tone, streamPolicy, durationMilliseconds, CancellationToken.None);
49 /// Plays a tone, asynchronously.
51 /// <param name="tone">A <see cref="ToneType"/> to play.</param>
52 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
53 /// <param name="durationMilliseconds">The tone duration in milliseconds. -1 indicates an infinite duration.</param>
54 /// <param name="cancellationToken">The cancellation token which can be used to stop playing the tone.</param>
55 /// <returns>A task that represents the asynchronous operation.</returns>
56 /// <exception cref="ArgumentException"><paramref name="tone"/> is invalid.</exception>
57 /// <exception cref="ArgumentNullException"><paramref name="streamPolicy"/> is null.</exception>
58 /// <exception cref="ArgumentOutOfRangeException"><paramref name="durationMilliseconds"/> is less than -1.</exception>
59 /// <exception cref="InvalidOperationException">Any invalid operations occurred.</exception>
60 /// <exception cref="NotSupportedException"><paramref name="tone"/> is not a supported type.</exception>
61 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
62 public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy,
63 int durationMilliseconds, CancellationToken cancellationToken)
65 if (durationMilliseconds < -1)
67 throw new ArgumentOutOfRangeException(nameof(durationMilliseconds), durationMilliseconds,
68 $"{nameof(durationMilliseconds)} can't be less than -1.");
71 if (streamPolicy == null)
73 throw new ArgumentNullException(nameof(streamPolicy));
76 ValidationUtil.ValidateEnum(typeof(ToneType), tone, nameof(tone));
78 if (cancellationToken.IsCancellationRequested)
80 return Task.FromCanceled(cancellationToken);
83 return StartAsyncCore(tone, streamPolicy, durationMilliseconds, cancellationToken);
86 private static async Task StartAsyncCore(ToneType tone, AudioStreamPolicy streamPolicy,
87 int durationMilliseconds, CancellationToken cancellationToken)
90 var tcs = new TaskCompletionSource<bool>();
92 Native.Start(tone, streamPolicy.Handle, durationMilliseconds, out var id).
93 Validate("Failed to play tone.");
95 using (RegisterCancellationAction(tcs, cancellationToken, id))
97 await WaitForDuration(tcs, cancellationToken, durationMilliseconds);
103 private static async Task WaitForDuration(TaskCompletionSource<bool> tcs,
104 CancellationToken cancellationToken, int durationMilliseconds)
106 if (durationMilliseconds == -1)
113 await Task.Delay(durationMilliseconds, cancellationToken);
114 tcs.TrySetResult(true);
116 catch (TaskCanceledException)
121 private static IDisposable RegisterCancellationAction(TaskCompletionSource<bool> tcs,
122 CancellationToken cancellationToken, int id)
124 if (cancellationToken.CanBeCanceled == false)
129 return cancellationToken.Register(() =>
131 Native.Stop(id).Validate("Failed to cancel");
132 tcs.TrySetCanceled();