Skip to content

Commit

Permalink
added param to retain callback script, renamed MediaStates, fixed des…
Browse files Browse the repository at this point in the history
…erialization error
  • Loading branch information
purplecabbage authored and sgrebnov committed Oct 8, 2012
1 parent 90cff43 commit 790dd3f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
92 changes: 50 additions & 42 deletions templates/standalone/cordovalib/Commands/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ internal class AudioPlayer : IDisposable
#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;
Expand Down Expand Up @@ -89,7 +89,7 @@ internal class AudioPlayer : IDisposable
/// <summary>
/// State of recording or playback
/// </summary>
private int state = MediaNone;
private int state = PlayerState_None;

/// <summary>
/// File name to play or record to
Expand Down Expand Up @@ -155,7 +155,7 @@ public void startRecording(string filePath)
{
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)
{
Expand All @@ -171,17 +171,17 @@ public void startRecording(string filePath)
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);
}
}

Expand All @@ -192,7 +192,7 @@ public void stopRecording()
{
if (this.recorder != null)
{
if (this.state == MediaRunning)
if (this.state == PlayerState_Running)
{
try
{
Expand All @@ -201,7 +201,7 @@ public void stopRecording()
this.recorder = null;
SaveAudioClipToLocalStorage();
this.FinalizeXnaGameLoop();
this.SetState(MediaStopped);
this.SetState(PlayerState_Stopped);
}
catch (Exception)
{
Expand All @@ -223,7 +223,7 @@ public void startPlaying(string filePath)
{
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;
}

Expand Down Expand Up @@ -286,28 +286,30 @@ public void startPlaying(string filePath)
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);
}
}
}
Expand All @@ -317,23 +319,29 @@ public void startPlaying(string filePath)
/// </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>
/// Callback to be invoked when playback of a media source has completed
/// </summary>
private void MediaEnded(object sender, RoutedEventArgs arg)
{
this.SetState(MediaStopped);
this.SetState(PlayerState_Stopped);
}

/// <summary>
Expand All @@ -342,7 +350,7 @@ private void MediaEnded(object sender, RoutedEventArgs arg)
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>
Expand All @@ -355,7 +363,7 @@ public void seekToPlaying(int milliseconds)
{
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);
}
}

Expand All @@ -376,14 +384,14 @@ public void setVolume(double vol)
/// </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);
}
}

Expand All @@ -393,17 +401,17 @@ public void pausePlaying()
/// </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>
Expand All @@ -412,15 +420,15 @@ public void stopPlaying()
/// <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;
}
}

Expand Down Expand Up @@ -457,7 +465,7 @@ private void SetState(int state)
{
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;
Expand Down
7 changes: 5 additions & 2 deletions templates/standalone/cordovalib/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ public object InvokeMethodNamed(string methodName, params object[] args)
}


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;
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions templates/standalone/cordovalib/Commands/Media.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void release(string options)

try
{
mediaOptions = JSON.JsonHelper.Deserialize<MediaOptions[]>(options)[0];
string[] optionsString = JSON.JsonHelper.Deserialize<string[]>(options);
mediaOptions = new MediaOptions();
mediaOptions.Id = optionsString[0];
}
catch (Exception)
{
Expand Down Expand Up @@ -246,7 +248,6 @@ public void create(string options)

AudioPlayer audio = new AudioPlayer(this, mediaOptions.Id);
Media.players.Add(mediaOptions.Id, audio);

DispatchCommandResult(new PluginResult(PluginResult.Status.OK));

}
Expand Down Expand Up @@ -300,7 +301,6 @@ public void startPlayingAudio(string options)
try
{
audio.startPlaying(mediaOptions.Src);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (Exception e)
Expand Down

0 comments on commit 790dd3f

Please sign in to comment.