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 /// <since_tizen> 3 </since_tizen>
28 public static class TonePlayer
31 /// Plays a tone, asynchronously.
33 /// <param name="tone">A <see cref="ToneType"/> to play.</param>
34 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
35 /// <param name="durationMilliseconds">The tone duration in milliseconds. -1 indicates an infinite duration.</param>
36 /// <returns>A task that represents the asynchronous operation.</returns>
37 /// <exception cref="ArgumentException"><paramref name="tone"/> is invalid.</exception>
38 /// <exception cref="ArgumentNullException"><paramref name="streamPolicy"/> is null.</exception>
39 /// <exception cref="ArgumentOutOfRangeException"><paramref name="durationMilliseconds"/> is less than -1.</exception>
40 /// <exception cref="InvalidOperationException">Any invalid operations occurred.</exception>
41 /// <exception cref="NotSupportedException"><paramref name="tone"/> is not a supported type.</exception>
42 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
43 /// <since_tizen> 3 </since_tizen>
44 public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy,
45 int durationMilliseconds)
47 return StartAsync(tone, streamPolicy, durationMilliseconds, CancellationToken.None);
51 /// Plays a tone, asynchronously.
53 /// <param name="tone">A <see cref="ToneType"/> to play.</param>
54 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
55 /// <param name="durationMilliseconds">The tone duration in milliseconds. -1 indicates an infinite duration.</param>
56 /// <param name="cancellationToken">The cancellation token which can be used to stop playing the tone.</param>
57 /// <returns>A task that represents the asynchronous operation.</returns>
58 /// <exception cref="ArgumentException"><paramref name="tone"/> is invalid.</exception>
59 /// <exception cref="ArgumentNullException"><paramref name="streamPolicy"/> is null.</exception>
60 /// <exception cref="ArgumentOutOfRangeException"><paramref name="durationMilliseconds"/> is less than -1.</exception>
61 /// <exception cref="InvalidOperationException">Any invalid operations occurred.</exception>
62 /// <exception cref="NotSupportedException"><paramref name="tone"/> is not a supported type.</exception>
63 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
64 /// <since_tizen> 3 </since_tizen>
65 public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy,
66 int durationMilliseconds, CancellationToken cancellationToken)
68 if (durationMilliseconds < -1)
70 throw new ArgumentOutOfRangeException(nameof(durationMilliseconds), durationMilliseconds,
71 $"{nameof(durationMilliseconds)} can't be less than -1.");
74 if (streamPolicy == null)
76 throw new ArgumentNullException(nameof(streamPolicy));
79 ValidationUtil.ValidateEnum(typeof(ToneType), tone, nameof(tone));
81 if (cancellationToken.IsCancellationRequested)
83 return Task.FromCanceled(cancellationToken);
86 return StartAsyncCore(tone, streamPolicy, durationMilliseconds, cancellationToken);
89 private static async Task StartAsyncCore(ToneType tone, AudioStreamPolicy streamPolicy,
90 int durationMilliseconds, CancellationToken cancellationToken)
93 var tcs = new TaskCompletionSource<bool>();
95 Native.Start(tone, streamPolicy.Handle, durationMilliseconds, out var id).
96 Validate("Failed to play tone.");
98 using (RegisterCancellationAction(tcs, cancellationToken, id))
100 await WaitForDuration(tcs, cancellationToken, durationMilliseconds);
106 private static async Task WaitForDuration(TaskCompletionSource<bool> tcs,
107 CancellationToken cancellationToken, int durationMilliseconds)
109 if (durationMilliseconds == -1)
116 await Task.Delay(durationMilliseconds, cancellationToken);
117 tcs.TrySetResult(true);
119 catch (TaskCanceledException)
124 private static IDisposable RegisterCancellationAction(TaskCompletionSource<bool> tcs,
125 CancellationToken cancellationToken, int id)
127 if (cancellationToken.CanBeCanceled == false)
132 return cancellationToken.Register(() =>
134 Native.Stop(id).Validate("Failed to cancel");
135 tcs.TrySetCanceled();