Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added basic media support (WAV and MP3 for now) and updated demo to r…

…eflect.
  • Loading branch information...
commit 667abc47ef2e927736b8b6e90a57b7cc2f577706 1 parent e9db71f
Fil Maj authored August 21, 2009 Joe Bowser committed November 04, 2009
4  winmo/CommandManager.cs
@@ -6,11 +6,11 @@ namespace PhoneGap {
6 6
 
7 7
     class CommandManager {
8 8
 
9  
-        private Command[] commands = new Command[1];
  9
+        private Command[] commands = new Command[2];
10 10
 
11 11
         public CommandManager() {
12 12
             commands[0] = new InitializationCommand();
13  
-            //commands[1] = new MediaCommand();
  13
+            commands[1] = new MediaCommand();
14 14
         }
15 15
 
16 16
         public String processInstruction(String instruction) {
91  winmo/MediaCommand.cs
... ...
@@ -0,0 +1,91 @@
  1
+using System;
  2
+using System.Linq;
  3
+using System.Collections.Generic;
  4
+using System.Text;
  5
+using System.IO;
  6
+using System.Reflection;
  7
+using System.Runtime.InteropServices;
  8
+
  9
+namespace PhoneGap
  10
+{
  11
+    // Need to invoke unmanaged mobile native code from .NET to play sounds. Woot!
  12
+    internal partial class PInvoke
  13
+    {
  14
+        private static IntPtr hSound = IntPtr.Zero; // Pointer/handle of currently-playing sound.
  15
+        const int SND_SCOPE_PROCESS = 0x1;
  16
+        private enum Flags
  17
+        {
  18
+            SND_SYNC = 0x0000,  /* play synchronously (default) */
  19
+            SND_ASYNC = 0x0001,  /* play asynchronously */
  20
+            SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
  21
+            SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
  22
+            SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
  23
+            SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
  24
+            SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
  25
+            SND_ALIAS = 0x00010000, /* name is a registry alias */
  26
+            SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
  27
+            SND_FILENAME = 0x00020000, /* name is file name */
  28
+            SND_RESOURCE = 0x00040004  /* name is resource name or atom */
  29
+        }
  30
+
  31
+
  32
+        [DllImport("aygshell.dll")]
  33
+        static extern uint SndOpen(string pszSoundFile, ref IntPtr phSound);
  34
+
  35
+        [DllImport("aygshell.dll")]
  36
+        static extern uint SndPlayAsync(IntPtr hSound, uint dwFlags);
  37
+
  38
+        [DllImport("aygshell.dll")]
  39
+        static extern uint SndClose(IntPtr hSound);
  40
+
  41
+        [DllImport("aygshell.dll")]
  42
+        static extern uint SndStop(int SoundScope, IntPtr hSound);
  43
+        public static bool PlaySound(string path)
  44
+        {
  45
+            if (File.Exists(path))
  46
+            {
  47
+                SndOpen(path, ref hSound);
  48
+                SndPlayAsync(hSound, 0);
  49
+                return true;
  50
+            }
  51
+            else return false;
  52
+        }
  53
+        public static void StopSound()
  54
+        {
  55
+            SndClose(hSound);
  56
+            SndStop(SND_SCOPE_PROCESS, hSound);
  57
+            hSound = IntPtr.Zero;
  58
+        }
  59
+    }
  60
+    class MediaCommand : Command
  61
+    {
  62
+        private string soundFileName = "";
  63
+        private string soundExtension = "";
  64
+        Boolean Command.accept(String instruction)
  65
+        {
  66
+            Boolean retVal = false;
  67
+            if (instruction.StartsWith("/media"))
  68
+            {
  69
+                int firstSlash = instruction.IndexOf('/',5);
  70
+                soundFileName = instruction.Substring(firstSlash);
  71
+                soundExtension = soundFileName.Substring(soundFileName.LastIndexOf('.'));
  72
+                // TODO: Test what other sound file types work.
  73
+                switch (soundExtension)
  74
+                {
  75
+                    case ".wav":
  76
+                    case ".mp3":
  77
+                        retVal = true;
  78
+                        break;
  79
+                }
  80
+            }
  81
+            return retVal;            
  82
+        }
  83
+        String Command.execute(String instruction)
  84
+        {
  85
+            Assembly assembly = Assembly.GetExecutingAssembly();
  86
+            string path = "\\Program Files\\" + assembly.GetName().Name + "\\" + soundFileName.Substring(1).Replace("/","\\");
  87
+            if (PInvoke.PlaySound(path)) return "";
  88
+            else return ";alert(\"[PhoneGap Error] Could not find sound file with path '" + path + "'.\");";
  89
+        }
  90
+    }
  91
+}
8  winmo/www/index.html
@@ -9,16 +9,20 @@
9 9
 		    document.getElementById('resultText').innerText = newText;
10 10
 		}
11 11
 		function playApplause() {
12  
-		    media.playSound('data:///www/media/applause.wav');
  12
+		    media.playSound('www/media/applause.wav');
13 13
 		    showLabel('CLAP CLAP CLAP!');
14 14
 		}
  15
+		function playBirds() {
  16
+		    media.playSound('www/media/bird.mp3');
  17
+		}
15 18
 		</script>
16 19
 	</head>
17 20
 	<body onload="device.init();"> <!-- on load call init to make sure we bridge to device properly-->
18 21
 	    <p>This is an HTML page.</p><br />
19 22
 	    <a href="javascript:showLabel(device.name);">Show me the device model/name</a><br />
20 23
 	    <a href='javascript:showLabel(device.uuid);'>Show me the device UUID</a><br />
21  
-	    <!--<a href="javascript:playApplause();">Play expected audience response ;)</a><br />-->
  24
+	    <a href="javascript:playApplause();">Play applause</a><br />
  25
+	    <a href="javascript:playBirds();">Play birds</a><br />
22 26
 	    <br />
23 27
 	    <div id="resultLabel" style="width:100%;height:20px;background-color:#dddddd;padding:2px;margin:5px;border:solid 1px black;"><p id="resultText" style="margin:0 auto;"></p></div>
24 28
 	    <br />
5  winmo/www/js/media.js
... ...
@@ -0,0 +1,5 @@
  1
+var media = {
  2
+    playSound: function(filename) {
  3
+        device.exec("media",[filename]);
  4
+    }
  5
+};
BIN  winmo/www/media/applause.wav
Binary file not shown
BIN  winmo/www/media/bird.mp3
Binary file not shown

0 notes on commit 667abc4

Please sign in to comment.
Something went wrong with that request. Please try again.