Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Commit

Permalink
Fixed detecting audio playing from another app. Improved timeout capa…
Browse files Browse the repository at this point in the history
…bilities.
  • Loading branch information
AzureKitsune committed Nov 23, 2013
1 parent 3563b8c commit 8185593
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/WP8/HanasuWP8/ViewModel/MainPageStationsViewModel.cs
Expand Up @@ -46,7 +46,7 @@ private async Task PlayStation(Station y)
if (!playCommandLock.WaitOne(5000)) return;

Microsoft.Xna.Framework.FrameworkDispatcher.Update();
if (!Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl)
if (!Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl && BackgroundAudioPlayer.Instance.Track == null)
if (ServiceManager.Resolve<IMessageBoxService>()
.ShowOkayCancelMessage("Playing Media", "By proceeding, your current media will stop and the selected station will be played instead.") == false)
{
Expand Down Expand Up @@ -114,19 +114,26 @@ private async Task PlayStation(Station y)
BAPClosed = false;
}

bool openedEvents = false;
if (connectedEvent == null)
{
await OpenEvents();

if (connectedEvent == null || errorEvent == null)
openedEvents = false;
else
openedEvents = true;
}


var playTask = connectedEvent.WaitAsync().AsTask();
var errorTask = errorEvent.WaitAsync().AsTask();

var timeoutTask = Task.Delay(System.Diagnostics.Debugger.IsAttached ? 12000 : 7000);

var what = await Task.WhenAny(playTask, timeoutTask, errorTask);

if ((what == timeoutTask || what == errorTask) && !App.IsPlaying)
if ((what == timeoutTask || what == errorTask || !openedEvents) && !App.IsPlaying)
{
ServiceManager.Resolve<IMessageBoxService>().ShowMessage("Uh-oh!",
what == timeoutTask ? "Unable to connect in a timely fashion!" : what == errorTask ? "An error occurred while connecting." : "Something bad happened!");
Expand Down Expand Up @@ -167,23 +174,32 @@ private async Task PlayStation(Station y)

private async Task OpenEvents()
{
int wait = 0;
while (true)
{
//loop until we get the event.
if (NamedEvent.TryOpen(IPCConsts.CONNECTED_EVENT_NAME, out connectedEvent))
{
wait = 0;
while (true)
{
if (NamedEvent.TryOpen(IPCConsts.ERROR_EVENT_NAME, out errorEvent))
{
break;
}
await Task.Delay(100);

if (wait >= 5000)
return;
}
break;
}

await Task.Delay(100);
wait += 100;

if (wait >= 5000)
return;
}
}

Expand Down

0 comments on commit 8185593

Please sign in to comment.