地铁应用中的视频捕捉

时间:2013-09-27 21:41:44

标签: c# microsoft-metro

我目前正在使用Visual Studio Express for Windows 8 IDE为Windows 8平板电脑Windows 8平板电脑开发视频聊天应用程序。我无法使用MediaCapture捕获流。我想在CaptureElement中捕获视频,然后进行测试,将其显示在mediaelement中。我基本上想要将IRandomAccessStream转换为字节,然后在mediaelement中反之亦然。以下是我的代码:

public sealed partial class MainPage : Page
{

    MediaCapture mediaCapture;
    IRandomAccessStream randomAccessStream;


    public MainPage()
    {
        this.InitializeComponent();
        mediaCapture = new MediaCapture();
        randomAccessStream = new InMemoryRandomAccessStream();

    }


    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }


     async private void startCapture(object sender, RoutedEventArgs e)
     {

         await mediaCapture.InitializeAsync();
         capturePreview.Source = mediaCapture;

         MediaEncodingProfile profile = MediaEncodingProfile.CreateWmv(VideoEncodingQuality.Auto);
             await mediaCapture.StartRecordToStreamAsync(profile, randomAccessStream);



     }

     async private void stopCapture(object sender, RoutedEventArgs e)
     {
         await mediaCapture.StopRecordAsync();
         await randomAccessStream.FlushAsync();

         randomAccessStream.Seek(0);

         // want to convert this randomAccessStream into byte[]
         mediaElement.SetSource(randomAccessStream, "video/x-ms-wmv");

     }


}

1 个答案:

答案 0 :(得分:0)

我试过下面的代码,它对我来说很好。只在事件中添加了代码。

public sealed partial class MainPage : Page
{
    MediaCapture mediaCapture;
    IRandomAccessStream randomAccessStream;
    public MainPage()
    {
        this.InitializeComponent();

        mediaCapture = new MediaCapture();
        randomAccessStream = new InMemoryRandomAccessStream();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private async void StartButton_Click(object sender, RoutedEventArgs e)
    {
        await mediaCapture.InitializeAsync();

        MediaEncodingProfile profile = MediaEncodingProfile.CreateWmv(VideoEncodingQuality.Auto);
        await mediaCapture.StartRecordToStreamAsync(profile, randomAccessStream);
    }

    private async void StropButton_Click(object sender, RoutedEventArgs e)
    {
        await mediaCapture.StopRecordAsync();
        await randomAccessStream.FlushAsync();

        randomAccessStream.Seek(0);

        // want to convert this randomAccessStream into byte[]
        mediaElement.SetSource(randomAccessStream, "video/x-ms-wmv");
    }
}