You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2012/09/12 02:36:55 UTC

[6/8] wp7 commit: added param to retain callback script, renamed MediaStates, fixed deserialization error

added param to retain callback script, renamed MediaStates, fixed deserialization error


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/commit/a27394b5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/tree/a27394b5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/diff/a27394b5

Branch: refs/heads/master
Commit: a27394b539c3a5170e1c461dd5e7db686ef5835a
Parents: 051bd86
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Thu Sep 6 15:13:18 2012 -0700
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Thu Sep 6 15:13:18 2012 -0700

----------------------------------------------------------------------
 .../standalone/cordovalib/Commands/AudioPlayer.cs  |   92 ++++++++-------
 .../standalone/cordovalib/Commands/BaseCommand.cs  |    7 +-
 templates/standalone/cordovalib/Commands/Media.cs  |    6 +-
 3 files changed, 58 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/a27394b5/templates/standalone/cordovalib/Commands/AudioPlayer.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/AudioPlayer.cs b/templates/standalone/cordovalib/Commands/AudioPlayer.cs
index 43ae237..83f178c 100644
--- a/templates/standalone/cordovalib/Commands/AudioPlayer.cs
+++ b/templates/standalone/cordovalib/Commands/AudioPlayer.cs
@@ -34,11 +34,11 @@ namespace WP7CordovaClassLib.Cordova.Commands
         #region Constants
 
         // AudioPlayer states
-        private const int MediaNone = 0;
-        private const int MediaStarting = 1;
-        private const int MediaRunning = 2;
-        private const int MediaPaused = 3;
-        private const int MediaStopped = 4;
+        private const int PlayerState_None = 0;
+        private const int PlayerState_Starting = 1;
+        private const int PlayerState_Running = 2;
+        private const int PlayerState_Paused = 3;
+        private const int PlayerState_Stopped = 4;
 
         // AudioPlayer messages
         private const int MediaState = 1;
@@ -89,7 +89,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         /// <summary>
         /// State of recording or playback
         /// </summary>
-        private int state = MediaNone;
+        private int state = PlayerState_None;
 
         /// <summary>
         /// File name to play or record to
@@ -155,7 +155,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         {
             if (this.player != null)
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPlayModeSet));
+                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPlayModeSet),false);
             }
             else if (this.recorder == null)
             {
@@ -171,17 +171,17 @@ namespace WP7CordovaClassLib.Cordova.Commands
                     this.WriteWavHeader(this.memoryStream, this.recorder.SampleRate);
                     this.recorder.Start();
                     FrameworkDispatcher.Update();
-                    this.SetState(MediaRunning);
+                    this.SetState(PlayerState_Running);
                 }
                 catch (Exception)
                 {
-                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingRecording));
+                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingRecording),false);
                 }
             }
             else
             {
 
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorAlreadyRecording));
+                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorAlreadyRecording),false);
             }
         }
 
@@ -192,7 +192,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         {
             if (this.recorder != null)
             {
-                if (this.state == MediaRunning)
+                if (this.state == PlayerState_Running)
                 {
                     try
                     {
@@ -201,7 +201,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
                         this.recorder = null;
                         SaveAudioClipToLocalStorage();
                         this.FinalizeXnaGameLoop();
-                        this.SetState(MediaStopped);
+                        this.SetState(PlayerState_Stopped);
                     }
                     catch (Exception)
                     {
@@ -223,7 +223,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         {
             if (this.recorder != null)
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorRecordModeSet));
+                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorRecordModeSet),false);
                 return;
             }
 
@@ -286,28 +286,30 @@ namespace WP7CordovaClassLib.Cordova.Commands
                             else
                             {
                                 Debug.WriteLine("Error: source doesn't exist :: " + filePath);
-                                throw new ArgumentException("Source doesn't exist");
+                                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, 1),false);
+                                return;
+                                //throw new ArgumentException("Source doesn't exist");
                             }
                         }
                     }
-                    this.SetState(MediaStarting);
+                    this.SetState(PlayerState_Starting);
                 }
                 catch (Exception e)
                 {
                     Debug.WriteLine("Error: " + e.Message);
-                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingPlayback));
+                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStartingPlayback),false);
                 }
             }
             else
             {
-                if (this.state != MediaRunning)
+                if (this.state != PlayerState_Running)
                 {
                     this.player.Play();
-                    this.SetState(MediaRunning);
+                    this.SetState(PlayerState_Running);
                 }
                 else
                 {
-                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorResumeState));
+                    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorResumeState),false);
                 }
             }
         }
@@ -317,15 +319,21 @@ namespace WP7CordovaClassLib.Cordova.Commands
         /// </summary>
         private void MediaOpened(object sender, RoutedEventArgs arg)
         {
-            if (!this.prepareOnly)
+            if (this.player != null)
             {
-                this.player.Play();
-                this.SetState(MediaRunning);
+                this.duration = this.player.NaturalDuration.TimeSpan.TotalSeconds;
+                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaDuration, this.duration),false);
+                if (!this.prepareOnly)
+                {
+                    this.player.Play();
+                    this.SetState(PlayerState_Running);
+                }
+                this.prepareOnly = false;
+            }
+            else
+            {
+                // TODO: occasionally MediaOpened is signalled, but player is null
             }
-
-            this.duration = this.player.NaturalDuration.TimeSpan.TotalSeconds;
-            this.prepareOnly = false;
-            this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaDuration, this.duration));
         }
 
         /// <summary>
@@ -333,7 +341,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         /// </summary>
         private void MediaEnded(object sender, RoutedEventArgs arg)
         {
-            this.SetState(MediaStopped);
+            this.SetState(PlayerState_Stopped);
         }
 
         /// <summary>
@@ -342,7 +350,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         private void MediaFailed(object sender, RoutedEventArgs arg)
         {
             player.Stop();
-            this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), "Media failed"));
+            this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError.ToString(), "Media failed"),false);
         }
 
         /// <summary>
@@ -355,7 +363,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
             {
                 TimeSpan tsPos = new TimeSpan(0, 0, 0, 0, milliseconds);
                 this.player.Position = tsPos;
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, milliseconds / 1000.0f));
+                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, milliseconds / 1000.0f),false);
             }
         }
 
@@ -376,14 +384,14 @@ namespace WP7CordovaClassLib.Cordova.Commands
         /// </summary>
         public void pausePlaying()
         {
-            if (this.state == MediaRunning)
+            if (this.state == PlayerState_Running)
             {
                 this.player.Pause();
-                this.SetState(MediaPaused);
+                this.SetState(PlayerState_Paused);
             }
             else
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPauseState));
+                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorPauseState),false);
             }
         }
 
@@ -393,17 +401,17 @@ namespace WP7CordovaClassLib.Cordova.Commands
         /// </summary>
         public void stopPlaying()
         {
-            if ((this.state == MediaRunning) || (this.state == MediaPaused))
+            if ((this.state == PlayerState_Running) || (this.state == PlayerState_Paused))
             {
                 this.player.Stop();
 
                 this.player.Position = new TimeSpan(0L);
-                this.SetState(MediaStopped);
-            }
-            else
-            {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStopState));
+                this.SetState(PlayerState_Stopped);
             }
+            //else // Why is it an error to call stop on a stopped media?
+            //{
+            //    this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaError, MediaErrorStopState), false);
+            //}
         }
 
         /// <summary>
@@ -412,15 +420,15 @@ namespace WP7CordovaClassLib.Cordova.Commands
         /// <returns>current position</returns>
         public double getCurrentPosition()
         {
-            if ((this.state == MediaRunning) || (this.state == MediaPaused))
+            if ((this.state == PlayerState_Running) || (this.state == PlayerState_Paused))
             {
                 double currentPosition = this.player.Position.TotalSeconds;
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, currentPosition));
+                //this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaPosition, currentPosition),false);
                 return currentPosition;
             }
             else
             {
-                return -1;
+                return 0;
             }
         }
 
@@ -457,7 +465,7 @@ namespace WP7CordovaClassLib.Cordova.Commands
         {
             if (this.state != state)
             {
-                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaState, state));
+                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction, this.id, MediaState, state),false);
             }
 
             this.state = state;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/a27394b5/templates/standalone/cordovalib/Commands/BaseCommand.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/BaseCommand.cs b/templates/standalone/cordovalib/Commands/BaseCommand.cs
index 5cdad2b..5f5c379 100644
--- a/templates/standalone/cordovalib/Commands/BaseCommand.cs
+++ b/templates/standalone/cordovalib/Commands/BaseCommand.cs
@@ -72,12 +72,15 @@ namespace WP7CordovaClassLib.Cordova.Commands
         }
 
 
-        public void InvokeCustomScript(ScriptCallback script)
+        public void InvokeCustomScript(ScriptCallback script, bool removeHandler)
         {
             if (this.OnCustomScript != null)
             {
                 this.OnCustomScript(this, script);
-                this.OnCustomScript = null;
+                if (removeHandler)
+                {
+                    this.OnCustomScript = null;
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/a27394b5/templates/standalone/cordovalib/Commands/Media.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/Media.cs b/templates/standalone/cordovalib/Commands/Media.cs
index 56076bd..f5efdac 100644
--- a/templates/standalone/cordovalib/Commands/Media.cs
+++ b/templates/standalone/cordovalib/Commands/Media.cs
@@ -66,7 +66,9 @@ namespace WP7CordovaClassLib.Cordova.Commands
 
                 try
                 {
-                    mediaOptions = JSON.JsonHelper.Deserialize<MediaOptions[]>(options)[0];
+                    string[] optionsString = JSON.JsonHelper.Deserialize<string[]>(options);
+                    mediaOptions = new MediaOptions();
+                    mediaOptions.Id = optionsString[0];
                 }
                 catch (Exception)
                 {
@@ -246,7 +248,6 @@ namespace WP7CordovaClassLib.Cordova.Commands
 
                 AudioPlayer audio = new AudioPlayer(this, mediaOptions.Id);
                 Media.players.Add(mediaOptions.Id, audio);
-
                 DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
 
             }
@@ -300,7 +301,6 @@ namespace WP7CordovaClassLib.Cordova.Commands
                     try
                     {
                         audio.startPlaying(mediaOptions.Src);
-
                         DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
                     }
                     catch (Exception e)