Commit ecdfc2c5 authored by Benn Mapes's avatar Benn Mapes
Browse files

update wp7 AudioPlayer.cs

parent 5d23b41c
Loading
Loading
Loading
Loading
+31 −4
Changes for src/wp7/AudioPlayer.cs: 31 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ using System.Windows.Resources;

namespace WPCordovaClassLib.Cordova.Commands
{

    /// <summary>
    /// Implements audio record and play back functionality.
    /// </summary>
@@ -62,6 +63,7 @@ namespace WPCordovaClassLib.Cordova.Commands

        #endregion


        /// <summary>
        /// The AudioHandler object
        /// </summary>
@@ -77,6 +79,7 @@ namespace WPCordovaClassLib.Cordova.Commands
        /// </summary>
        DispatcherTimer dtXna;


        /// <summary>
        /// Output buffer
        /// </summary>
@@ -128,6 +131,7 @@ namespace WPCordovaClassLib.Cordova.Commands
            this.id = id;
        }


        /// <summary>
        /// Destroys player and stop audio playing or recording
        /// </summary>
@@ -194,8 +198,26 @@ namespace WPCordovaClassLib.Cordova.Commands
                    this.recorder.BufferDuration = TimeSpan.FromMilliseconds(500);
                    this.buffer = new byte[recorder.GetSampleSizeInBytes(this.recorder.BufferDuration)];
                    this.recorder.BufferReady += new EventHandler<EventArgs>(recorderBufferReady);
                    this.memoryStream = new MemoryStream();
                    this.memoryStream.InitializeWavStream(this.recorder.SampleRate);
                    MemoryStream stream  = new MemoryStream();
                    this.memoryStream = stream;
                    int numBits = 16;
                    int numBytes = numBits / 8;

                    // inline version from AudioFormatsHelper
                    stream.Write(System.Text.Encoding.UTF8.GetBytes("RIFF"), 0, 4);
                    stream.Write(BitConverter.GetBytes(0), 0, 4);
                    stream.Write(System.Text.Encoding.UTF8.GetBytes("WAVE"), 0, 4);
                    stream.Write(System.Text.Encoding.UTF8.GetBytes("fmt "), 0, 4);
                    stream.Write(BitConverter.GetBytes(16), 0, 4);
                    stream.Write(BitConverter.GetBytes((short)1), 0, 2);
                    stream.Write(BitConverter.GetBytes((short)1), 0, 2);
                    stream.Write(BitConverter.GetBytes(this.recorder.SampleRate), 0, 4);
                    stream.Write(BitConverter.GetBytes(this.recorder.SampleRate * numBytes), 0, 4);
                    stream.Write(BitConverter.GetBytes((short)(numBytes)), 0, 2);
                    stream.Write(BitConverter.GetBytes((short)(numBits)), 0, 2);
                    stream.Write(System.Text.Encoding.UTF8.GetBytes("data"), 0, 4);
                    stream.Write(BitConverter.GetBytes(0), 0, 4);

                    this.recorder.Start();
                    FrameworkDispatcher.Update();
                    this.SetState(PlayerState_Running);
@@ -555,12 +577,17 @@ namespace WPCordovaClassLib.Cordova.Commands
        /// <returns></returns>
        private void SaveAudioClipToLocalStorage()
        {
            if (this.memoryStream == null || this.memoryStream.Length <= 0)
            if (memoryStream == null || memoryStream.Length <= 0)
            {
                return;
            }

            this.memoryStream.UpdateWavStream();
            long position = memoryStream.Position;
            memoryStream.Seek(4, SeekOrigin.Begin);
            memoryStream.Write(BitConverter.GetBytes((int)memoryStream.Length - 8), 0, 4);
            memoryStream.Seek(40, SeekOrigin.Begin);
            memoryStream.Write(BitConverter.GetBytes((int)memoryStream.Length - 44), 0, 4);
            memoryStream.Seek(position, SeekOrigin.Begin);

            try
            {