cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [1/4] git commit: unpack audio resource to isolated storage before playing
Date Wed, 06 Feb 2013 01:40:50 GMT
unpack audio resource to isolated storage before playing


Project: http://git-wip-us.apache.org/repos/asf/cordova-wp8/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-wp8/commit/936c03a6
Tree: http://git-wip-us.apache.org/repos/asf/cordova-wp8/tree/936c03a6
Diff: http://git-wip-us.apache.org/repos/asf/cordova-wp8/diff/936c03a6

Branch: refs/heads/master
Commit: 936c03a642835c889d5c97cf4faf35872092f1d3
Parents: 0d9b525
Author: Jesse MacFadyen <purplecabbage@gmail.com>
Authored: Tue Feb 5 15:03:43 2013 -0800
Committer: Jesse MacFadyen <purplecabbage@gmail.com>
Committed: Tue Feb 5 15:03:43 2013 -0800

----------------------------------------------------------------------
 .../standalone/cordovalib/Commands/AudioPlayer.cs  |   41 ++++++++++++--
 templates/standalone/cordovalib/Commands/Media.cs  |   21 ++++++-
 2 files changed, 53 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/936c03a6/templates/standalone/cordovalib/Commands/AudioPlayer.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/AudioPlayer.cs b/templates/standalone/cordovalib/Commands/AudioPlayer.cs
index 00e345c..c306aac 100644
--- a/templates/standalone/cordovalib/Commands/AudioPlayer.cs
+++ b/templates/standalone/cordovalib/Commands/AudioPlayer.cs
@@ -23,6 +23,7 @@ using Microsoft.Xna.Framework.Audio;
 using Microsoft.Xna.Framework.Media;
 using Microsoft.Phone.Controls;
 using System.Diagnostics;
+using System.Windows.Resources;
 
 namespace WPCordovaClassLib.Cordova.Commands
 {
@@ -132,7 +133,7 @@ namespace WPCordovaClassLib.Cordova.Commands
         /// </summary>
         public void Dispose()
         {
-            Debug.WriteLine("Dispose :: " + this.audioFile);
+            //Debug.WriteLine("Dispose :: " + this.audioFile);
             if (this.player != null)
             {
                 this.stopPlaying();
@@ -243,7 +244,6 @@ namespace WPCordovaClassLib.Cordova.Commands
                             if (grid != null)
                             {
 
-                                //Microsoft.Xna.Framework.Media.MediaPlayer.Play(
                                 this.player = grid.FindName("playerMediaElement") as MediaElement;
                                 if (this.player == null) // still null ?
                                 {
@@ -276,6 +276,37 @@ namespace WPCordovaClassLib.Cordova.Commands
                     {
                         using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                         {
+                            if (!isoFile.FileExists(filePath))
+                            {
+                                // try to unpack it from the dll into isolated storage
+                                StreamResourceInfo fileResourceStreamInfo = Application.GetResourceStream(new
Uri(filePath, UriKind.Relative));
+                                if (fileResourceStreamInfo != null)
+                                {
+                                    using (BinaryReader br = new BinaryReader(fileResourceStreamInfo.Stream))
+                                    {
+                                        byte[] data = br.ReadBytes((int)fileResourceStreamInfo.Stream.Length);
         
+
+                                        string[] dirParts = filePath.Split('/');
+                                        string dirName = "";
+                                        for (int n = 0; n < dirParts.Length - 1; n++)
+                                        {
+                                            dirName += dirParts[n] + "/";
+                                        }
+                                        if (!isoFile.DirectoryExists(dirName))
+                                        {
+                                            isoFile.CreateDirectory(dirName);
+                                        }
+
+                                        using (IsolatedStorageFileStream outFile = isoFile.OpenFile(filePath,
FileMode.Create))
+                                        {
+                                            using (BinaryWriter writer = new BinaryWriter(outFile))
+                                            {
+                                                writer.Write(data);
+                                            }
+                                        }
+                                    }
+                                }
+                            }
                             if (isoFile.FileExists(filePath))
                             {
                                 using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath,
FileMode.Open, isoFile))
@@ -285,10 +316,8 @@ namespace WPCordovaClassLib.Cordova.Commands
                             }
                             else
                             {
-                                Debug.WriteLine("Error: source doesn't exist :: " + filePath);
-                                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction,
this.id, MediaError, 1),false);
+                                this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction,
this.id, MediaError, 1), false);
                                 return;
-                                //throw new ArgumentException("Source doesn't exist");
                             }
                         }
                     }
@@ -296,7 +325,7 @@ namespace WPCordovaClassLib.Cordova.Commands
                 }
                 catch (Exception e)
                 {
-                    Debug.WriteLine("Error: " + e.Message);
+                    Debug.WriteLine("Error in AudioPlayer::startPlaying : " + e.Message);
                     this.handler.InvokeCustomScript(new ScriptCallback(CallbackFunction,
this.id, MediaError, MediaErrorStartingPlayback),false);
                 }
             }

http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/936c03a6/templates/standalone/cordovalib/Commands/Media.cs
----------------------------------------------------------------------
diff --git a/templates/standalone/cordovalib/Commands/Media.cs b/templates/standalone/cordovalib/Commands/Media.cs
index 69265dd..5de4884 100644
--- a/templates/standalone/cordovalib/Commands/Media.cs
+++ b/templates/standalone/cordovalib/Commands/Media.cs
@@ -132,13 +132,28 @@ namespace WPCordovaClassLib.Cordova.Commands
                     {
                         try
                         {
+                            AudioPlayer audio;
                             if (!Media.players.ContainsKey(mediaOptions.Id))
                             {
-                                AudioPlayer audio = new AudioPlayer(this, 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));
+                            }
+                            else
+                            {
+                                DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,
"Error accessing AudioPlayer for key " + mediaOptions.Id));
                             }
-                            DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
+                            
+                            
                         }
                         catch (Exception e)
                         {
@@ -292,7 +307,7 @@ namespace WPCordovaClassLib.Cordova.Commands
                 }
                 else
                 {
-                    Debug.WriteLine("INFO: startPlayingAudio FOUND mediaPlayer for " + mediaOptions.Id);
+                    //Debug.WriteLine("INFO: startPlayingAudio FOUND mediaPlayer for " +
mediaOptions.Id);
                     audio = Media.players[mediaOptions.Id];
                 }
 


Mime
View raw message