This is in a Windows Phone 8.1 Store app. My MainPage
has a CaptureElement
to display the preview stream from my MediaCapture
object. For navigation within the app (between pages), this works well:
MediaCapture mc;
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
mc = new MediaCapture();
await mc.InitializeAsync();
preview.Source = mc;
await mc.StartPreviewAsync();
}
protected override async void OnNavigatedFrom(NavigationEventArgs e)
{
await mc.StopPreviewAsync();
}
I can navigate to other pages and come back, and the preview runs reliably. I'm running into problems for the following scenarios though:
- User presses the Windows button, then the back button
- User presses the Windows button, then uses the task switcher to come back to my app
- User presses the search button, then the back button
- User presses the power button, then presses it again and swipes up to unlock the device
- User holds down the back button to enter the task switcher, then taps on my app again
After each of the above actions (and/or combinations of them), when my app comes back the preview is frozen at the last frame that was displayed.
If the user then navigates to a different page and then back to the MainPage, the preview starts running again without an issue, so this leads me to believe I just need to stop/start the preview after coming back from one of the above scenarios.
I tried subscribing to the App.Suspending
and App.Resuming
events, but these don't trigger on these occasions. What am I missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…