locked
MediaCapture.InitializeAsync exception

    Question

  • Hello. I am taking the first step in writing a Universal App (Win 8.1 and Win Phone 8.1) in C# to work with the tablet and phone cameras. I am trying to follow the "MediaCapture using capture device" sample. since I am not planning on taking photos, I do not want to use the CameraCaptureUI. The sample works fine. 

    The issue I am having is after hitting the Windows key to push the app to the background, then bringing the app back by tapping its icon from the start menu, the InitializeAsync() method gives me an exception: HRESULT = 0xC00D36B2, Message: "The request is invalid in the current state.\r\nInitialized"

    Init code: 

    	MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
    	settings.AudioDeviceId = "";
    	settings.PhotoCaptureSource = PhotoCaptureSource.Photo;
    	settings.MediaCategory = MediaCategory.Other;
    	settings.StreamingCaptureMode = StreamingCaptureMode.Video;
    
    	int i = 0;
    	while (i < collection.Count)
    	{
    		if (collection[i].EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front)
    		{
    			break;
    		}
    		i++;
    	}
    
    	settings.VideoDeviceId = collection[i].Id;
    
    	camera.Failed += camera_Failed;
    	camera.RecordLimitationExceeded += camera_RecordLimitationExceeded;
    	try
    	{
    		await camera.InitializeAsync( settings );
    	}
    	catch (Exception e) 
    	{
    		MessageDialog msg = new MessageDialog( e.Message );
    		msg.ShowAsync();
    		return;
    	}

    Destroy code: 

    	await camera.StopPreviewAsync();
    	camera.Failed -= camera_Failed;
    	camera.RecordLimitationExceeded -= camera_RecordLimitationExceeded;
    	camera.Dispose();

    Events from the page, similar to the MediaCapture sample and the only events that are actually triggered when the Windows key is hit:

    protected override void OnNavigatedTo( Windows.UI.Xaml.Navigation.NavigationEventArgs e )
    {
    	SystemMediaTransportControls transport = SystemMediaTransportControls.GetForCurrentView();
    	transport.PropertyChanged += propChanged;
    
    	if (!camControl.IsInitialized)
    	{
    		camControl.InitCamera();
    	}
    
    	base.OnNavigatedTo( e );
    }
    
    protected override void OnNavigatedFrom( Windows.UI.Xaml.Navigation.NavigationEventArgs e )
    {
    	SystemMediaTransportControls transport = SystemMediaTransportControls.GetForCurrentView();
    	transport.PropertyChanged -= propChanged;
    	camControl.DestroyCamera();
    	camera_viewer.Source = null;
    	base.OnNavigatedFrom( e );
    }
    
    private async void propertyChangedHandler( SystemMediaTransportControls sender, SystemMediaTransportControlsPropertyChangedEventArgs e )
    {
    	switch (e.Property)
    	{
    		case SystemMediaTransportControlsProperty.SoundLevel:
    			await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    			{
    				if (sender.SoundLevel != Windows.Media.SoundLevel.Muted)
    				{
    					camControl.InitCamera();
    				}
    				else
    				{
    					camControl.DestroyCamera();
    				}
    			} );
    			break;
    		default:
    			break;
    	}
    }

    Is there something I'm missing? The Destroy call is triggered when the Windows Key is hit, but it doesn't seem to want to close the camera. I haven't found anything done differently in the sample. The preview works fine at the app start, and will also reload fine I close the app instead of sending it to the background with the Windows Key. 

    I have tried changing the settings to different options, and removing the event handlers, but nothing seems to have had any effect. I've seen another thread asking if there is a way to tell if the camera is already initialized, but that thread was unanswered. 

    Also, even though the exception says "Initialized", if I attempt to use any of the objects, like the VideoDeviceController, I get a different exception saying that I must initialize the object before I can use it (which is easy to show by attempting to get VideoDeviceController before calling InitializeAsync() ). 

    Thanks. 

    Friday, May 30, 2014 2:42 PM

All replies

  • Which device are you using? Can you place an actual project on Onedrive for reproduction?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, June 02, 2014 7:10 PM
    Moderator
  • Thanks for the quick response! The devices I've tried this on are the Microsoft Surface RT (ARM), and the Toshiba Encore (x86). I've tried this also on a Nokia 1520 updated to 8.1, but i have not updated that portion of the project to match the tablet one. I'm much more concerned with the tablet as I already have a Windows Phone 8 app running how I want, using the Advanced Capture API. 

    I've tried to run it on my 8.1 laptop (Dell M4800) which has an embedded webcam, but both that and the Simulator gave me "The capture source does not have an independent photo stream." I know I can test for concurrent photo stream or whatever, so i'm not concerned about that either. 

    I've uploaded it to Onedrive: http://1drv.ms/SoOQVC

    The main goal was to test the camera capabilities that just came out with the VideoDeviceController. We were hoping to utilize the same code to run both one the tablet and the phones, but sadly the functionality is likely not even close. Without something similar to GetPreviewBuffer(), it's very unlikely that the streaming image performance will be good enough for us to utilize the tablets cameras. 

    Monday, June 02, 2014 8:28 PM
  • Thanks, but after reviewing and trying it out, there was nothing that I had not already done. Plus there is no cleanup in the downloadable project at all. You can easily get an exception and break that program by:

    1. Record and save a sound clip

    2. Use the windows key to go out to the menu

    3. Open Music

    4. Play the clip

    5. Hit windows again

    6. Open project and hit Record. It will say that it cannot go into a record state from a paused state. 

    Thursday, June 12, 2014 7:36 PM