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;
21 namespace Tizen.Multimedia
24 /// Provides the ability to play a tone.
26 public static class TonePlayer
29 /// Plays a tone, asynchronously.
31 /// <param name="tone">A <see cref="ToneType"/> to play.</param>
32 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
33 /// <param name="durationMilliseconds">The tone duration in milliseconds. -1 indicates an infinite duration.</param>
34 /// <returns>A task that represents the asynchronous operation.</returns>
35 /// <exception cref="ArgumentException"><paramref name="tone"/> is invalid.</exception>
36 /// <exception cref="ArgumentNullException"><paramref name="streamPolicy"/> is null.</exception>
37 /// <exception cref="ArgumentOutOfRangeException"><paramref name="durationMilliseconds"/> is less than -1.</exception>
38 /// <exception cref="InvalidOperationException">Any invalid operations occurred.</exception>
39 /// <exception cref="NotSupportedException"><paramref name="tone"/> is not a supported type.</exception>
40 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed.</exception>
41 public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy,
42 int durationMilliseconds)
44 return StartAsync(tone, streamPolicy, durationMilliseconds, CancellationToken.None);
48 /// Plays a tone, asynchronously.
50 /// <param name="tone">A <see cref="ToneType"/> to play.</param>
51 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
52 /// <param name="durationMilliseconds">The tone duration in milliseconds. -1 indicates an infinite duration.</param>
53 /// <param name="cancellationToken">The cancellation token which can be used to stop playing the tone.</param>
54 /// <returns>A task that represents the asynchronous operation.</returns>
55 /// <exception cref="ArgumentException"><paramref name="tone"/> is invalid.</exception>
56 /// <exception cref="ArgumentNullException"><paramref name="streamPolicy"/> is null.</exception>
57 /// <exception cref="ArgumentOutOfRangeException"><paramref name="durationMilliseconds"/> is less than -1.</exception>
58 /// <exception cref="InvalidOperationException">Any invalid operations occurred.</exception>
59 /// <exception cref="NotSupportedException"><paramref name="tone"/> is not a supported type.</exception>
60 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed.</exception>
61 public static Task StartAsync(ToneType tone, AudioStreamPolicy streamPolicy,
62 int durationMilliseconds, CancellationToken cancellationToken)
64 if (durationMilliseconds < -1)
66 throw new ArgumentOutOfRangeException(nameof(durationMilliseconds), durationMilliseconds,
67 $"{nameof(durationMilliseconds)} can't be less than -1.");
70 if (streamPolicy == null)
72 throw new ArgumentNullException(nameof(streamPolicy));
75 ValidationUtil.ValidateEnum(typeof(ToneType), tone, nameof(tone));
77 if (cancellationToken.IsCancellationRequested)
79 return Task.FromCanceled(cancellationToken);
82 return StartAsyncCore(tone, streamPolicy, durationMilliseconds, cancellationToken);
85 private static async Task StartAsyncCore(ToneType tone, AudioStreamPolicy streamPolicy,
86 int durationMilliseconds, CancellationToken cancellationToken)
89 var tcs = new TaskCompletionSource<bool>();
91 Interop.TonePlayer.Start(tone, streamPolicy.Handle, durationMilliseconds, out var id).
92 Validate("Failed to play tone.");
94 using (RegisterCancellationAction(tcs, cancellationToken, id))
96 await WaitForDuration(tcs, cancellationToken, durationMilliseconds);
102 private static async Task WaitForDuration(TaskCompletionSource<bool> tcs,
103 CancellationToken cancellationToken, int durationMilliseconds)
105 if (durationMilliseconds == -1)
112 await Task.Delay(durationMilliseconds, cancellationToken);
113 tcs.TrySetResult(true);
115 catch (TaskCanceledException)
120 private static IDisposable RegisterCancellationAction(TaskCompletionSource<bool> tcs,
121 CancellationToken cancellationToken, int id)
123 if (cancellationToken.CanBeCanceled == false)
128 return cancellationToken.Register(() =>
130 Interop.TonePlayer.Stop(id).Validate("Failed to cancel");
131 tcs.TrySetCanceled();