From e76b54b8e3f3d7299e5a921dcecc9dc442b278e1 Mon Sep 17 00:00:00 2001 From: Arjun Roychowdhury Date: Wed, 28 Oct 2015 13:31:08 -0400 Subject: media plugin --- plugins/org.apache.cordova.media/src/wp/Media.cs | 590 +++++++++++++++++++++++ 1 file changed, 590 insertions(+) create mode 100644 plugins/org.apache.cordova.media/src/wp/Media.cs (limited to 'plugins/org.apache.cordova.media/src/wp/Media.cs') diff --git a/plugins/org.apache.cordova.media/src/wp/Media.cs b/plugins/org.apache.cordova.media/src/wp/Media.cs new file mode 100644 index 00000000..aedd2bb6 --- /dev/null +++ b/plugins/org.apache.cordova.media/src/wp/Media.cs @@ -0,0 +1,590 @@ +/* + 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 WPCordovaClassLib.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; } + + public string CallbackId { get; set; } + } + + /// + /// Releases the audio player instance to save memory. + /// + public void release(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + MediaOptions mediaOptions = new MediaOptions(); + + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + mediaOptions.Id = optionsString[0]; + callbackId = mediaOptions.CallbackId = optionsString[1]; + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); + return; + } + + if (!Media.players.ContainsKey(mediaOptions.Id)) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, false), callbackId); + 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), mediaOptions.CallbackId); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), mediaOptions.CallbackId); + } + }); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + } + + private AudioPlayer GetOrCreatePlayerById(string id) + { + AudioPlayer audio = null; + + lock (Media.players) + { + if (!Media.players.TryGetValue(id, out audio)) + { + audio = new AudioPlayer(this, id); + Media.players.Add(id, audio); + Debug.WriteLine("Media Created in GetOrCreatePlayerById"); + } + } + + + + return audio; + } + + /// + /// Starts recording and save the specified file + /// + public void startRecordingAudio(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + MediaOptions mediaOptions = new MediaOptions(); + + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + mediaOptions.Id = optionsString[0]; + mediaOptions.Src = optionsString[1]; + callbackId = mediaOptions.CallbackId = optionsString[2]; + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), mediaOptions.CallbackId); + return; + } + + if (mediaOptions != null) + { + + Deployment.Current.Dispatcher.BeginInvoke(() => + { + try + { + AudioPlayer audio; + if (!Media.players.ContainsKey(mediaOptions.Id)) + { + audio = new AudioPlayer(this, mediaOptions.Id); + Media.players.Add(mediaOptions.Id, audio); + } + else + { + audio = Media.players[mediaOptions.Id]; + } + + if (audio != null) + { + audio.startRecording(mediaOptions.Src); + DispatchCommandResult(new PluginResult(PluginResult.Status.OK), mediaOptions.CallbackId); + } + else + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, + "Error accessing AudioPlayer for key " + mediaOptions.Id), mediaOptions.CallbackId); + } + + + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), mediaOptions.CallbackId); + } + + }); + } + else + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), mediaOptions.CallbackId); + } + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + } + + /// + /// Stops recording and save to the file specified when recording started + /// + public void stopRecordingAudio(string options) + { + string callbackId = this.CurrentCommandCallbackId; + + try + { + string[] optStrings = JSON.JsonHelper.Deserialize(options); + string mediaId = optStrings[0]; + callbackId = optStrings[1]; + 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), callbackId); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + }); + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); + } + } + + public void setVolume(string options) // id,volume + { + string callbackId = this.CurrentCommandCallbackId; + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + string id = optionsString[0]; + double volume = 0.0d; + double.TryParse(optionsString[1], out volume); + + callbackId = optionsString[2]; + + 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), callbackId); + } + }); + } + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, + "Error parsing options into setVolume method"), callbackId); + } + } + + // Some Audio Notes: + // In the Windows Phone Emulator, playback of video or audio content using the MediaElement control is not supported. + // While playing, a MediaElement stops all other media playback on the phone. + // Multiple MediaElement controls are NOT supported + + // Called when you create a new Media('blah.wav') object in JS. + public void create(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + MediaOptions mediaOptions; + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + mediaOptions = new MediaOptions(); + mediaOptions.Id = optionsString[0]; + mediaOptions.Src = optionsString[1]; + callbackId = mediaOptions.CallbackId = optionsString[2]; + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, + "Error parsing options into create method"), callbackId); + return; + } + + GetOrCreatePlayerById(mediaOptions.Id); + DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId); + + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + } + + /// + /// Starts or resume playing audio file + /// + public void startPlayingAudio(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + MediaOptions mediaOptions; + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + mediaOptions = new MediaOptions(); + mediaOptions.Id = optionsString[0]; + mediaOptions.Src = optionsString[1]; + int msec = 0; + if (int.TryParse(optionsString[2], out msec)) + { + mediaOptions.Milliseconds = msec; + } + callbackId = mediaOptions.CallbackId = optionsString[3]; + + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); + return; + } + + AudioPlayer audio = GetOrCreatePlayerById(mediaOptions.Id); + + Deployment.Current.Dispatcher.BeginInvoke(() => + { + try + { + audio.startPlaying(mediaOptions.Src); + DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + }); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + } + + + /// + /// Seeks to a location + /// + public void seekToAudio(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + MediaOptions mediaOptions; + + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + mediaOptions = new MediaOptions(); + mediaOptions.Id = optionsString[0]; + int msec = 0; + if (int.TryParse(optionsString[2], out msec)) + { + mediaOptions.Milliseconds = msec; + } + callbackId = mediaOptions.CallbackId = optionsString[3]; + + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); + 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), callbackId); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + }); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + } + + /// + /// Pauses playing + /// + public void pausePlayingAudio(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + string mediaId = optionsString[0]; + callbackId = optionsString[1]; + + 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), callbackId); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message),callbackId); + } + }); + + + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION),callbackId); + } + + + } + + + /// + /// Stops playing the audio file + /// + public void stopPlayingAudio(String options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + string[] optionsStrings = JSON.JsonHelper.Deserialize(options); + string mediaId = optionsStrings[0]; + callbackId = optionsStrings[1]; + 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), callbackId); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + }); + + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); + } + } + + /// + /// Gets current position of playback + /// + public void getCurrentPositionAudio(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + string[] optionsStrings = JSON.JsonHelper.Deserialize(options); + string mediaId = optionsStrings[0]; + callbackId = optionsStrings[1]; + Deployment.Current.Dispatcher.BeginInvoke(() => + { + try + { + if (Media.players.ContainsKey(mediaId)) + { + AudioPlayer audio = Media.players[mediaId]; + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, audio.getCurrentPosition()), callbackId); + } + else + { + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, -1), callbackId); + } + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + }); + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); + return; + } + } + + + /// + /// Gets the duration of the audio file + /// + + [Obsolete("This method will be removed shortly")] + public void getDurationAudio(string options) + { + string callbackId = this.CurrentCommandCallbackId; + try + { + MediaOptions mediaOptions; + + try + { + string[] optionsString = JSON.JsonHelper.Deserialize(options); + + mediaOptions = new MediaOptions(); + mediaOptions.Id = optionsString[0]; + mediaOptions.Src = optionsString[1]; + callbackId = mediaOptions.CallbackId = optionsString[2]; + } + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION), callbackId); + 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)), callbackId); + }); + } + catch (Exception e) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message), callbackId); + } + } + } +} -- cgit v1.2.3