locked
Capture Video on Windows phone RRS feed

  • Question

  • Hi. Can you give me the C# code(steps) to capture videos on windows phone. Thanks
    • Moved by Jamles Hez Wednesday, January 14, 2015 9:21 AM
    Wednesday, January 7, 2015 8:46 AM

Answers

  • You can do that. Take a look at the Windows.Media.Capture API. This sample should also help.

    Common steps are:

    1. Initialize the MediaCapture object.
    2. Set the proper media type (with the desired resolution, or close to the desired one) for preview and record.
    3. Start preview.
    4. Start recording with a proper media type.

    And I would recommend writing a universal application instead of the Silverlight one.



    Thursday, January 8, 2015 12:06 AM
  • Hi Clint,

    I would suggest you to open another thread for your question.

    You can find some difference here: Windows Phone 8 and Windows 8 platform comparison

    Windows Phone runtime and Windows Store App can be worked at same time in Universal App, another word is Universal App contains Windows Phone runtime template.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Jamles Hez Wednesday, January 14, 2015 9:21 AM
    • Marked as answer by Jamles Hez Thursday, January 22, 2015 4:45 AM
    Wednesday, January 14, 2015 9:20 AM
  • Here's the sample code:

    using (var mc = new MediaCapture())
    {
        //
        // Initialize MediaCapture.
        //
    
        // Select video camera.
        var device = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)).FirstOrDefault(di => di.IsEnabled && di.EnclosureLocation != null && di.EnclosureLocation.Panel == Panel.Back);
        if (device == null)
        {
            // No device found.
            return;
        }
    
        await mc.InitializeAsync(new MediaCaptureInitializationSettings{ VideoDeviceId = device.Id});
    
        //
        // Select media type.
        //
    
        // Choose 320x240 NV12 media format that is native for the WP8.1 devices (might not work on older devices).
        var formatSelector = new Func<VideoEncodingProperties, bool>(ep => ep.Height * ep.Width == 320 * 240 && string.Equals(ep.Subtype, "nv12", StringComparison.OrdinalIgnoreCase));
        var recordFormat   = mc.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord).OfType<VideoEncodingProperties>().FirstOrDefault(formatSelector);
        var previewFormat  = mc.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).OfType<VideoEncodingProperties>().FirstOrDefault(formatSelector);
        if (recordFormat == null || previewFormat == null)
        {
            // No suitable formats found.
            return;
        }
    
        await mc.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewFormat);
        await mc.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, recordFormat);
    
        //
        // Start preview.
        //
    
        // Preview is the CaptureElement XAML component.
        this.Preview.Source = mc;
        await mc.StartPreviewAsync();
    
        //
        // Record to file.
        //
    
        var outputFile = await KnownFolders.VideosLibrary.CreateFileAsync("output.mp4", CreationCollisionOption.GenerateUniqueName);
    
        // QVGA is 320x240 (http://en.wikipedia.org/wiki/Graphics_display_resolution#QVGA).
        var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Qvga);
        await mc.StartRecordToStorageFileAsync(encodingProfile, outputFile);
    
        // Wait for some time.
        await Task.Delay(TimeSpan.FromSeconds(1));
    
        // Stop recording and shutdown camera.
        await mc.StopRecordAsync();
        await mc.StopPreviewAsync();
        this.Preview.Source = null;
    }

    I would not recommend you to write Silverlight apps, if you can write a Universal app. 

    • Marked as answer by Jamles Hez Thursday, January 22, 2015 4:45 AM
    Wednesday, January 21, 2015 8:31 PM

All replies

  • Hello,

    What kind of videos do you want to capture?

    Wednesday, January 7, 2015 9:05 AM
  • Thanks for replying so fast. I have a multi(Android, Blackberry and now Windows phone) platform app that shares videos. So the codecs can be h.264 mp4 and I want to control the resolution also. ex. If user A captures a video for user B that user A will get user B's resolution before capturing then I want to capture in that resolution ex if user B have a 240*320 than user A must capture a video in 240*320 + h.264(I heard that format is compatible with most devices) + mp4. Thanks in advance for the answer...:-)

    So to answer you: I want to capture mp4 (h.264) with specified resolution.

    ps. when I can do this I want to ask you more.

    Wednesday, January 7, 2015 9:41 AM
  • You can do that. Take a look at the Windows.Media.Capture API. This sample should also help.

    Common steps are:

    1. Initialize the MediaCapture object.
    2. Set the proper media type (with the desired resolution, or close to the desired one) for preview and record.
    3. Start preview.
    4. Start recording with a proper media type.

    And I would recommend writing a universal application instead of the Silverlight one.



    Thursday, January 8, 2015 12:06 AM
  • Hi, thank you for this answer. I'm using the windows phone template because I'm targeting windows phone users. When do I want to use a universal application or Silverlight one? What is the difference between the 3( Windows Phone Template, Universal Application, Silverlight )?
    Thursday, January 8, 2015 8:23 AM
  • Hi Clint,

    I would suggest you to open another thread for your question.

    You can find some difference here: Windows Phone 8 and Windows 8 platform comparison

    Windows Phone runtime and Windows Store App can be worked at same time in Universal App, another word is Universal App contains Windows Phone runtime template.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Jamles Hez Wednesday, January 14, 2015 9:21 AM
    • Marked as answer by Jamles Hez Thursday, January 22, 2015 4:45 AM
    Wednesday, January 14, 2015 9:20 AM
  • Here's the sample code:

    using (var mc = new MediaCapture())
    {
        //
        // Initialize MediaCapture.
        //
    
        // Select video camera.
        var device = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)).FirstOrDefault(di => di.IsEnabled && di.EnclosureLocation != null && di.EnclosureLocation.Panel == Panel.Back);
        if (device == null)
        {
            // No device found.
            return;
        }
    
        await mc.InitializeAsync(new MediaCaptureInitializationSettings{ VideoDeviceId = device.Id});
    
        //
        // Select media type.
        //
    
        // Choose 320x240 NV12 media format that is native for the WP8.1 devices (might not work on older devices).
        var formatSelector = new Func<VideoEncodingProperties, bool>(ep => ep.Height * ep.Width == 320 * 240 && string.Equals(ep.Subtype, "nv12", StringComparison.OrdinalIgnoreCase));
        var recordFormat   = mc.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord).OfType<VideoEncodingProperties>().FirstOrDefault(formatSelector);
        var previewFormat  = mc.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).OfType<VideoEncodingProperties>().FirstOrDefault(formatSelector);
        if (recordFormat == null || previewFormat == null)
        {
            // No suitable formats found.
            return;
        }
    
        await mc.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, previewFormat);
        await mc.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, recordFormat);
    
        //
        // Start preview.
        //
    
        // Preview is the CaptureElement XAML component.
        this.Preview.Source = mc;
        await mc.StartPreviewAsync();
    
        //
        // Record to file.
        //
    
        var outputFile = await KnownFolders.VideosLibrary.CreateFileAsync("output.mp4", CreationCollisionOption.GenerateUniqueName);
    
        // QVGA is 320x240 (http://en.wikipedia.org/wiki/Graphics_display_resolution#QVGA).
        var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Qvga);
        await mc.StartRecordToStorageFileAsync(encodingProfile, outputFile);
    
        // Wait for some time.
        await Task.Delay(TimeSpan.FromSeconds(1));
    
        // Stop recording and shutdown camera.
        await mc.StopRecordAsync();
        await mc.StopPreviewAsync();
        this.Preview.Source = null;
    }

    I would not recommend you to write Silverlight apps, if you can write a Universal app. 

    • Marked as answer by Jamles Hez Thursday, January 22, 2015 4:45 AM
    Wednesday, January 21, 2015 8:31 PM