2 * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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.Runtime.InteropServices;
20 using System.ComponentModel;
25 /// The Text-to-speech (TTS) player.
27 /// <since_tizen> 3 </since_tizen>
28 public class TTSPlayer : BaseHandle
30 private static readonly TTSPlayer instance = TTSPlayer.Get();
31 private StateChangedEventCallbackType _stateChangedEventCallback;
32 private StateChangedSignalType stateChangedSignal;
34 internal TTSPlayer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.TtsPlayer.Upcast(cPtr), cMemoryOwn)
38 internal TTSPlayer() : this(Interop.TtsPlayer.NewTtsPlayer(), true)
40 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
43 internal TTSPlayer(TTSPlayer handle) : this(Interop.TtsPlayer.NewTtsPlayer(TTSPlayer.getCPtr(handle)), true)
45 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
48 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
49 private delegate void StateChangedEventCallbackType(TTSState prevState, TTSState nextState);
50 private event EventHandler<StateChangedEventArgs> _stateChangedEventHandler;
53 /// State changed event.
55 /// <since_tizen> 3 </since_tizen>
56 public event EventHandler<StateChangedEventArgs> StateChanged
60 if (_stateChangedEventHandler == null)
62 _stateChangedEventCallback = OnStateChanged;
63 stateChangedSignal = StateChangedSignal();
64 stateChangedSignal.Connect(_stateChangedEventCallback);
67 _stateChangedEventHandler += value;
71 _stateChangedEventHandler -= value;
73 if (_stateChangedEventHandler == null && stateChangedSignal?.Empty() == false && _stateChangedEventCallback != null)
75 stateChangedSignal.Disconnect(_stateChangedEventCallback);
76 stateChangedSignal.Dispose();
77 stateChangedSignal = null;
83 /// Enumeration for the instance of TTS mode.
85 /// <since_tizen> 3 </since_tizen>
89 /// Default mode for normal application.
93 /// Notification mode, such as playing utterance is started or completed.
97 /// Screen reader mode. <br />
98 /// To help visually impaired users interact with their devices,<br />
99 /// screen reader reads text or graphic elements on the screen using the TTS engine.
109 /// Enumeration for the instance of TTS state.
111 /// <since_tizen> 3 </since_tizen>
115 /// Player is not available.
119 /// Player is ready to play.
123 /// Player is playing.
127 /// Player is paused.
133 /// Gets the singleton of the TTSPlayer object.
135 /// <since_tizen> 5 </since_tizen>
136 public static TTSPlayer Instance
145 /// Gets the singleton of the TTS player for the given mode.
147 /// <param name="mode"> The mode of TTS player.</param>
148 /// <returns> A handle of the TTS player for the given mode.</returns>
149 /// <since_tizen> 3 </since_tizen>
150 public static TTSPlayer Get(TTSMode mode)
152 TTSPlayer ret = new TTSPlayer(Interop.TtsPlayer.Get((int)mode), true);
153 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
158 /// Gets the singleton of the TTS player for the default mode.
160 /// <returns> A handle of the TTS player for the default mode.</returns>
161 /// <since_tizen> 3 </since_tizen>
162 public static TTSPlayer Get()
164 TTSPlayer ret = new TTSPlayer(Interop.TtsPlayer.Get(), true);
165 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170 /// Starts playing the audio data synthesized from the specified text.
172 /// <param name="text"> The text to play.</param>
173 /// <remarks>The TTS player needs to be initialized.</remarks>
174 /// <since_tizen> 3 </since_tizen>
175 public void Play(string text)
177 Interop.TtsPlayer.Play(SwigCPtr, text);
178 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182 /// Stops playing the utterance.
184 /// <remarks>The TTS player needs to be initialized.</remarks>
185 /// <since_tizen> 3 </since_tizen>
188 Interop.TtsPlayer.Stop(SwigCPtr);
189 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
193 /// Pauses the currently playing utterance.
195 /// <remarks>The TTS player needs to be initialized.</remarks>
196 /// <since_tizen> 3 </since_tizen>
199 Interop.TtsPlayer.Pause(SwigCPtr);
200 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204 /// Resumes the previously paused utterance.
206 /// <remarks>The TTS player needs to be initialized.</remarks>
207 /// <since_tizen> 3 </since_tizen>
210 Interop.TtsPlayer.Resume(SwigCPtr);
211 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
215 /// Gets the current state of the player.
217 /// <returns> The current TTS state. </returns>
218 /// <remarks>The TTS player needs to be initialized.</remarks>
219 /// <since_tizen> 3 </since_tizen>
220 public TTSState GetState()
222 TTSState ret = (TTSState)Interop.TtsPlayer.GetState(SwigCPtr);
223 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
227 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TTSPlayer obj)
229 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
232 internal StateChangedSignalType StateChangedSignal()
234 StateChangedSignalType ret = new StateChangedSignalType(Interop.TtsPlayer.StateChangedSignal(SwigCPtr), false);
235 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
239 internal TTSPlayer Assign(TTSPlayer rhs)
241 TTSPlayer ret = new TTSPlayer(Interop.TtsPlayer.Assign(SwigCPtr, TTSPlayer.getCPtr(rhs)), false);
242 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
246 private void OnStateChanged(TTSState prevState, TTSState nextState)
248 StateChangedEventArgs e = new StateChangedEventArgs();
250 e.PrevState = prevState;
251 e.NextState = nextState;
253 if (_stateChangedEventHandler != null)
255 _stateChangedEventHandler(this, e);
260 /// State changed argument.
262 /// <since_tizen> 3 </since_tizen>
263 public class StateChangedEventArgs : EventArgs
268 /// <since_tizen> 3 </since_tizen>
269 public TTSState PrevState
278 /// <since_tizen> 3 </since_tizen>
279 public TTSState NextState