|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | using System;
|
| 18 | +using System.ComponentModel; |
18 | 19 | using System.IO;
|
19 | 20 | using System.Threading;
|
20 | 21 | using System.Threading.Tasks;
|
@@ -85,20 +86,62 @@ public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
|
85 | 86 | }
|
86 | 87 |
|
87 | 88 | return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
|
88 |
| - StartAsyncCore(path, streamPolicy, cancellationToken); |
| 89 | + StartAsyncCore(path, streamPolicy, 1, cancellationToken); |
89 | 90 | }
|
90 | 91 |
|
91 |
| - private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy, |
| 92 | + /// <summary> |
| 93 | + /// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/> with given repetition number. |
| 94 | + /// </summary> |
| 95 | + /// <remarks>If loopCount is 0, it means infinite loops</remarks> |
| 96 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 97 | + /// <param name="path">A file path to play.</param> |
| 98 | + /// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param> |
| 99 | + /// <param name="loopCount">A number of repetitions.</param> |
| 100 | + /// <param name="cancellationToken">A cancellation token which can be used to stop.</param> |
| 101 | + /// <exception cref="ArgumentNullException"> |
| 102 | + /// <paramref name="path"/> is null. |
| 103 | + /// <para>-or-</para> |
| 104 | + /// <paramref name="streamPolicy"/> is null. |
| 105 | + /// </exception> |
| 106 | + /// <exception cref="InvalidOperationException">An internal error occurs.</exception> |
| 107 | + /// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception> |
| 108 | + /// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception> |
| 109 | + /// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception> |
| 110 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 111 | + public static Task StartAsync(string path, AudioStreamPolicy streamPolicy, uint loopCount, |
| 112 | + CancellationToken cancellationToken) |
| 113 | + { |
| 114 | + if (path == null) |
| 115 | + { |
| 116 | + throw new ArgumentNullException(nameof(path)); |
| 117 | + } |
| 118 | + |
| 119 | + if (streamPolicy == null) |
| 120 | + { |
| 121 | + throw new ArgumentNullException(nameof(streamPolicy)); |
| 122 | + } |
| 123 | + |
| 124 | + if (File.Exists(path) == false) |
| 125 | + { |
| 126 | + throw new FileNotFoundException("File does not exists.", path); |
| 127 | + } |
| 128 | + |
| 129 | + return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) : |
| 130 | + StartAsyncCore(path, streamPolicy, loopCount, cancellationToken); |
| 131 | + } |
| 132 | + |
| 133 | + private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy, uint loopCount, |
92 | 134 | CancellationToken cancellationToken)
|
93 | 135 | {
|
| 136 | + int id = 0; |
94 | 137 | var tcs = new TaskCompletionSource<bool>();
|
95 | 138 |
|
96 | 139 | Native.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true);
|
97 | 140 |
|
98 | 141 | using (var cbKeeper = ObjectKeeper.Get(cb))
|
99 | 142 | {
|
100 |
| - Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id). |
101 |
| - Validate("Failed to play."); |
| 143 | + Native.StartLoop(path, streamPolicy.Handle, loopCount, cb, IntPtr.Zero, out id). |
| 144 | + Validate("Failed to play with loop."); |
102 | 145 |
|
103 | 146 | using (RegisterCancellationAction(tcs, cancellationToken, id))
|
104 | 147 | {
|
|
0 commit comments