Commit bc09e1b3 authored by keesschollaart81's avatar keesschollaart81

Added debug sound on position changed in WP8

parent da7baa65
using System; using System;
using System.Windows;
using Windows.Devices.Geolocation; using Windows.Devices.Geolocation;
using WPCordovaClassLib.Cordova; using WPCordovaClassLib.Cordova;
using WPCordovaClassLib.Cordova.Commands; using WPCordovaClassLib.Cordova.Commands;
...@@ -28,20 +27,21 @@ namespace Cordova.Extension.Commands ...@@ -28,20 +27,21 @@ namespace Cordova.Extension.Commands
public BackgroundGeoLocation() public BackgroundGeoLocation()
{ {
this.IsConfiguring = false; IsConfiguring = false;
this.IsConfigured = false; IsConfigured = false;
} }
public void configure(string args) public void configure(string args)
{ {
this.IsConfiguring = true; IsConfiguring = true;
this.ConfigureCallbackToken = CurrentCommandCallbackId; ConfigureCallbackToken = CurrentCommandCallbackId;
RunningInBackground = false; RunningInBackground = false;
BackgroundGeoLocationOptions = this.ParseBackgroundGeoLocationOptions(args); BackgroundGeoLocationOptions = this.ParseBackgroundGeoLocationOptions(args);
this.IsConfigured = BackgroundGeoLocationOptions.ParsingSucceeded; IsConfigured = BackgroundGeoLocationOptions.ParsingSucceeded;
this.IsConfiguring = false; IsConfiguring = false;
} }
private BackgroundGeoLocationOptions ParseBackgroundGeoLocationOptions(string configureArgs) private BackgroundGeoLocationOptions ParseBackgroundGeoLocationOptions(string configureArgs)
...@@ -51,11 +51,11 @@ namespace Cordova.Extension.Commands ...@@ -51,11 +51,11 @@ namespace Cordova.Extension.Commands
var options = JsonHelper.Deserialize<string[]>(configureArgs); var options = JsonHelper.Deserialize<string[]>(configureArgs);
var customParameters = JsonHelper.Deserialize<BackgroundGeoLocationOptionsParameters>(options[0]); var customParameters = JsonHelper.Deserialize<BackgroundGeoLocationOptionsParameters>(options[0]);
var stationaryRadius = 0.0; double stationaryRadius;
var distanceFilter = 0.0; double distanceFilter;
UInt32 locationTimeout = 0; UInt32 locationTimeout;
UInt32 desiredAccuracy = 0; UInt32 desiredAccuracy;
var debug = false; bool debug;
if (!double.TryParse(options[3], out stationaryRadius)) if (!double.TryParse(options[3], out stationaryRadius))
{ {
...@@ -140,21 +140,24 @@ namespace Cordova.Extension.Commands ...@@ -140,21 +140,24 @@ namespace Cordova.Extension.Commands
RunningInBackground = true; RunningInBackground = true;
DispatchCommandResult(new PluginResult(PluginResult.Status.OK)); DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
} }
private void OnGeolocatorOnPositionChanged(Geolocator sender, PositionChangedEventArgs ConfigureCallbackTokenargs) private void OnGeolocatorOnPositionChanged(Geolocator sender, PositionChangedEventArgs configureCallbackTokenargs)
{ {
if (Geolocator.LocationStatus == PositionStatus.Disabled || if (Geolocator.LocationStatus == PositionStatus.Disabled || Geolocator.LocationStatus == PositionStatus.NotAvailable)
Geolocator.LocationStatus == PositionStatus.NotAvailable)
{ {
DispatchMessage(PluginResult.Status.ERROR, string.Format("Cannot start: LocationStatus/PositionStatus: {0}! {1}", Geolocator.LocationStatus, IsConfigured), true, this.ConfigureCallbackToken); DispatchMessage(PluginResult.Status.ERROR, string.Format("Cannot start: LocationStatus/PositionStatus: {0}! {1}", Geolocator.LocationStatus, IsConfigured), true, ConfigureCallbackToken);
return; return;
} }
var callbackJsonResult = ConfigureCallbackTokenargs.Position.Coordinate.ToJson(); var callbackJsonResult = configureCallbackTokenargs.Position.Coordinate.ToJson();
if (BackgroundGeoLocationOptions.Debug)
{
DebugAudioNotifier.GetDebugAudioNotifier().PlaySound(DebugAudioNotifier.Tone.High, TimeSpan.FromSeconds(3));
Debug.WriteLine("PositionChanged token{0}, Coordinates: {1}", ConfigureCallbackToken, callbackJsonResult); Debug.WriteLine("PositionChanged token{0}, Coordinates: {1}", ConfigureCallbackToken, callbackJsonResult);
DispatchMessage(PluginResult.Status.OK, callbackJsonResult, true, this.ConfigureCallbackToken); }
DispatchMessage(PluginResult.Status.OK, callbackJsonResult, true, ConfigureCallbackToken);
} }
public void stop() public void stop()
...@@ -173,7 +176,7 @@ namespace Cordova.Extension.Commands ...@@ -173,7 +176,7 @@ namespace Cordova.Extension.Commands
public void finish() public void finish()
{ {
throw new NotImplementedException(); DispatchCommandResult(new PluginResult(PluginResult.Status.NO_RESULT));
} }
public void onPaceChange(bool isMoving) public void onPaceChange(bool isMoving)
...@@ -192,10 +195,10 @@ namespace Cordova.Extension.Commands ...@@ -192,10 +195,10 @@ namespace Cordova.Extension.Commands
var options = JsonHelper.Deserialize<string[]>(setConfigArgs); var options = JsonHelper.Deserialize<string[]>(setConfigArgs);
var stationaryRadius = 0.0; double stationaryRadius;
var distanceFilter = 0.0; double distanceFilter;
UInt32 locationTimeout = 0; UInt32 locationTimeout;
UInt32 desiredAccuracy = 0; UInt32 desiredAccuracy;
if (!double.TryParse(options[0], out stationaryRadius)) if (!double.TryParse(options[0], out stationaryRadius))
{ {
...@@ -238,5 +241,4 @@ namespace Cordova.Extension.Commands ...@@ -238,5 +241,4 @@ namespace Cordova.Extension.Commands
DispatchCommandResult(pluginResult, callBackId); DispatchCommandResult(pluginResult, callBackId);
} }
} }
} }
using System;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
namespace Cordova.Extension.Commands
{
public class DebugAudioNotifier : IDisposable
{
private static DebugAudioNotifier _audioNotifier;
private DynamicSoundEffectInstance _dynamicSound;
private DispatcherTimer _timer;
private TimeSpan _timeLeft;
private const int SampleRate = 48000;
private Tone _frequency = Tone.Low;
private int _bufferSize;
private byte[] _soundBuffer;
private int _totalTime;
public enum Tone
{
Low = 300,
High = 900
}
private DebugAudioNotifier()
{
}
public static DebugAudioNotifier GetDebugAudioNotifier()
{
return _audioNotifier ?? (_audioNotifier = new DebugAudioNotifier());
}
public void PlaySound(Tone tone, TimeSpan duration)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (_timer == null)
{
_timer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(33)
};
_timer.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } };
}
if (_timer.IsEnabled) _timer.Stop();
_timeLeft = duration;
FrameworkDispatcher.Update();
_frequency = tone;
_dynamicSound = new DynamicSoundEffectInstance(SampleRate, AudioChannels.Mono);
_dynamicSound.BufferNeeded += dynamicSound_BufferNeeded;
_dynamicSound.Play();
_bufferSize = _dynamicSound.GetSampleSizeInBytes(TimeSpan.FromSeconds(1));
_soundBuffer = new byte[_bufferSize];
_timer.Start();
});
}
private void dynamicSound_BufferNeeded(object sender, EventArgs e)
{
for (var i = 0; i < _bufferSize - 1; i += 2)
{
var time = _totalTime / (double)SampleRate;
var sample = (short)(Math.Sin(2 * Math.PI * (int)_frequency * time) * short.MaxValue);
_soundBuffer[i] = (byte)sample;
_soundBuffer[i + 1] = (byte)(sample >> 8);
_totalTime++;
}
_timeLeft = _timeLeft.Subtract(TimeSpan.FromSeconds(_totalTime / SampleRate));
if (_timeLeft.Ticks <= 0)
{
_totalTime = 0;
return;
}
_dynamicSound.SubmitBuffer(_soundBuffer);
}
public void Dispose()
{
_dynamicSound.Dispose();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment