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;
22 namespace Tizen.Multimedia
25 /// Provides the ability to play a wav file.
27 public static class WavPlayer
30 /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
32 /// <returns>A task that represents the asynchronous operation.</returns>
33 /// <param name="path">A file path to play.</param>
34 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
35 /// <exception cref="ArgumentNullException">
36 /// <paramref name="path"/> is null.
38 /// <paramref name="streamPolicy"/> is null.
40 /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
41 /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
42 /// <exception cref="FileFormatException">The format of <paramref name=""/> is not supported.</exception>
43 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
44 public static Task StartAsync(string path, AudioStreamPolicy streamPolicy)
46 return StartAsync(path, streamPolicy, CancellationToken.None);
50 /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/>.
52 /// <returns>A task that represents the asynchronous operation.</returns>
53 /// <param name="path">A file path to play.</param>
54 /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
55 /// <param name="cancellationToken">A cancellation token which can be used to stop.</param>
56 /// <exception cref="ArgumentNullException">
57 /// <paramref name="path"/> is null.
59 /// <paramref name="streamPolicy"/> is null.
61 /// <exception cref="InvalidOperationException">An internal error occurs.</exception>
62 /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
63 /// <exception cref="FileFormatException">The format of <paramref name=""/> is not supported.</exception>
64 /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed.</exception>
65 public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
66 CancellationToken cancellationToken)
70 throw new ArgumentNullException(nameof(path));
73 if (streamPolicy == null)
75 throw new ArgumentNullException(nameof(streamPolicy));
78 if (File.Exists(path) == false)
80 throw new FileNotFoundException("File does not exists.", path);
83 return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
84 StartAsyncCore(path, streamPolicy, cancellationToken);
87 private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy,
88 CancellationToken cancellationToken)
90 var tcs = new TaskCompletionSource<bool>();
92 Interop.WavPlayer.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true);
94 Interop.WavPlayer.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id).
95 Validate("Failed to play.");
97 using (RegisterCancellationAction(tcs, cancellationToken, id))
103 private static IDisposable RegisterCancellationAction(TaskCompletionSource<bool> tcs,
104 CancellationToken cancellationToken, int id)
106 if (cancellationToken.CanBeCanceled == false)
111 return cancellationToken.Register(() =>
113 Interop.WavPlayer.Stop(id).Validate("Failed to cancel");
114 tcs.TrySetCanceled();