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.
19 using System.Threading;
20 using System.Threading.Tasks;
21 using Native = Interop.WavPlayer;
23 namespace Tizen.Multimedia
26 /// Provides the ability to play a wav file.
28 /// <since_tizen> 3 </since_tizen>
29 public static class WavPlayer
32 /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
34 /// <returns>A task that represents the asynchronous operation.</returns>
35 /// <param name="path">A file path to play.</param>
36 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
37 /// <exception cref="ArgumentNullException">
38 /// <paramref name="path"/> is null.
40 /// <paramref name="streamPolicy"/> is null.
42 /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
43 /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
44 /// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
45 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
46 /// <since_tizen> 3 </since_tizen>
47 public static Task StartAsync(string path, AudioStreamPolicy streamPolicy)
49 return StartAsync(path, streamPolicy, CancellationToken.None);
53 /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
55 /// <returns>A task that represents the asynchronous operation.</returns>
56 /// <param name="path">A file path to play.</param>
57 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
58 /// <param name="cancellationToken">A cancellation token which can be used to stop.</param>
59 /// <exception cref="ArgumentNullException">
60 /// <paramref name="path"/> is null.
62 /// <paramref name="streamPolicy"/> is null.
64 /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
65 /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
66 /// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
67 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
68 /// <since_tizen> 3 </since_tizen>
69 public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
70 CancellationToken cancellationToken)
74 throw new ArgumentNullException(nameof(path));
77 if (streamPolicy == null)
79 throw new ArgumentNullException(nameof(streamPolicy));
82 if (File.Exists(path) == false)
84 throw new FileNotFoundException("File does not exists.", path);
87 return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
88 StartAsyncCore(path, streamPolicy, cancellationToken);
91 private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy,
92 CancellationToken cancellationToken)
94 var tcs = new TaskCompletionSource<bool>();
96 Native.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true);
98 using (ObjectKeeper.Get(cb))
100 Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id).
101 Validate("Failed to play.");
103 using (RegisterCancellationAction(tcs, cancellationToken, id))
110 private static IDisposable RegisterCancellationAction(TaskCompletionSource<bool> tcs,
111 CancellationToken cancellationToken, int id)
113 if (cancellationToken.CanBeCanceled == false)
118 return cancellationToken.Register(() =>
120 Native.Stop(id).Validate("Failed to cancel");
121 tcs.TrySetCanceled();