/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Windows; using System.Diagnostics; namespace WP7CordovaClassLib.Cordova.Commands { /// /// Provides the ability to record and play back audio files on a device. /// public class Media : BaseCommand { /// /// Audio player objects /// private static Dictionary players = new Dictionary(); /// /// Represents Media action options. /// [DataContract] public class MediaOptions { /// /// Audio id /// [DataMember(Name = "id", IsRequired = true)] public string Id { get; set; } /// /// Path to audio file /// [DataMember(Name = "src")] public string Src { get; set; } /// /// New track position /// [DataMember(Name = "milliseconds")] public int Milliseconds { get; set; } } /// /// Releases the audio player instance to save memory. /// public void release(string options) { try { MediaOptions mediaOptions; try { mediaOptions = JSON.JsonHelper.Deserialize(options)[0]; } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } if (!Media.players.ContainsKey(mediaOptions.Id)) { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, false)); return; } Deployment.Current.Dispatcher.BeginInvoke(() => { try { AudioPlayer audio = Media.players[mediaOptions.Id]; Media.players.Remove(mediaOptions.Id); audio.Dispose(); DispatchCommandResult(new PluginResult(PluginResult.Status.OK, true)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } } /// /// Starts recording and save the specified file /// public void startRecordingAudio(string options) { try { MediaOptions mediaOptions; try { string[] optionsString = JSON.JsonHelper.Deserialize(options); mediaOptions = new MediaOptions(); mediaOptions.Id = optionsString[0]; mediaOptions.Src = optionsString[1]; } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } if (mediaOptions != null) { Deployment.Current.Dispatcher.BeginInvoke(() => { try { if (!Media.players.ContainsKey(mediaOptions.Id)) { AudioPlayer audio = new AudioPlayer(this, mediaOptions.Id); Media.players.Add(mediaOptions.Id, audio); audio.startRecording(mediaOptions.Src); } DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); } } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } } /// /// Stops recording and save to the file specified when recording started /// public void stopRecordingAudio(string options) { try { string mediaId = JSON.JsonHelper.Deserialize(options)[0]; Deployment.Current.Dispatcher.BeginInvoke(() => { try { if (Media.players.ContainsKey(mediaId)) { AudioPlayer audio = Media.players[mediaId]; audio.stopRecording(); Media.players.Remove(mediaId); } DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); } } public void setVolume(string options) // id,volume { try { string[] optionsString = JSON.JsonHelper.Deserialize(options); string id = optionsString[0]; double volume = double.Parse(optionsString[1]); if (Media.players.ContainsKey(id)) { Deployment.Current.Dispatcher.BeginInvoke(() => { try { AudioPlayer player = Media.players[id]; player.setVolume(volume); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Error parsing options into setVolume method")); return; } } // Called when you create a new Media('blah') object in JS. public void create(string options) { try { MediaOptions mediaOptions; try { string[] optionsString = JSON.JsonHelper.Deserialize(options); mediaOptions = new MediaOptions(); mediaOptions.Id = optionsString[0]; mediaOptions.Src = optionsString[1]; } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Error parsing options into create method")); return; } AudioPlayer audio = new AudioPlayer(this, mediaOptions.Id); Media.players.Add(mediaOptions.Id, audio); DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } } /// /// Starts or resume playing audio file /// public void startPlayingAudio(string options) { try { MediaOptions mediaOptions; try { string[] optionsString = JSON.JsonHelper.Deserialize(options); mediaOptions = new MediaOptions(); mediaOptions.Id = optionsString[0]; mediaOptions.Src = optionsString[1]; mediaOptions.Milliseconds = int.Parse(optionsString[2]); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } AudioPlayer audio; if (!Media.players.ContainsKey(mediaOptions.Id)) { audio = new AudioPlayer(this, mediaOptions.Id); Media.players.Add(mediaOptions.Id, audio); } else { Debug.WriteLine("INFO: startPlayingAudio could not find mediaPlayer for " + mediaOptions.Id); audio = Media.players[mediaOptions.Id]; } Deployment.Current.Dispatcher.BeginInvoke(() => { try { audio.startPlaying(mediaOptions.Src); DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } } /// /// Seeks to a location /// public void seekToAudio(string options) { try { MediaOptions mediaOptions; try { string[] optionsString = JSON.JsonHelper.Deserialize(options); mediaOptions = new MediaOptions(); mediaOptions.Id = optionsString[0]; mediaOptions.Milliseconds = int.Parse(optionsString[1]); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } Deployment.Current.Dispatcher.BeginInvoke(() => { try { if (Media.players.ContainsKey(mediaOptions.Id)) { AudioPlayer audio = Media.players[mediaOptions.Id]; audio.seekToPlaying(mediaOptions.Milliseconds); } else { Debug.WriteLine("ERROR: seekToAudio could not find mediaPlayer for " + mediaOptions.Id); } DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } } /// /// Pauses playing /// public void pausePlayingAudio(string options) { try { string mediaId = JSON.JsonHelper.Deserialize(options)[0]; Deployment.Current.Dispatcher.BeginInvoke(() => { try { if (Media.players.ContainsKey(mediaId)) { AudioPlayer audio = Media.players[mediaId]; audio.pausePlaying(); } else { Debug.WriteLine("ERROR: pausePlayingAudio could not find mediaPlayer for " + mediaId); } DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); } } /// /// Stops playing the audio file /// public void stopPlayingAudio(String options) { try { string mediaId = JSON.JsonHelper.Deserialize(options)[0]; Deployment.Current.Dispatcher.BeginInvoke(() => { try { if (Media.players.ContainsKey(mediaId)) { AudioPlayer audio = Media.players[mediaId]; audio.stopPlaying(); } else { Debug.WriteLine("stopPlaying could not find mediaPlayer for " + mediaId); } DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); } } /// /// Gets current position of playback /// public void getCurrentPositionAudio(string options) { try { string mediaId = JSON.JsonHelper.Deserialize(options)[0]; Deployment.Current.Dispatcher.BeginInvoke(() => { try { if (Media.players.ContainsKey(mediaId)) { AudioPlayer audio = Media.players[mediaId]; DispatchCommandResult(new PluginResult(PluginResult.Status.OK, audio.getCurrentPosition())); } else { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, -1)); } } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } }); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } } /// /// Gets the duration of the audio file /// [Obsolete("This method will be removed shortly")] public void getDurationAudio(string options) { try { MediaOptions mediaOptions; try { mediaOptions = JSON.JsonHelper.Deserialize(options); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } AudioPlayer audio; if (Media.players.ContainsKey(mediaOptions.Id)) { audio = Media.players[mediaOptions.Id]; } else { Debug.WriteLine("ERROR: getDurationAudio could not find mediaPlayer for " + mediaOptions.Id); audio = new AudioPlayer(this, mediaOptions.Id); Media.players.Add(mediaOptions.Id, audio); } Deployment.Current.Dispatcher.BeginInvoke(() => { DispatchCommandResult(new PluginResult(PluginResult.Status.OK, audio.getDuration(mediaOptions.Src))); }); } catch (Exception e) { DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message)); } } } }