Windows Phone 8 Camera App:以不同方法启动和停止相同的视频录制流

时间:2014-10-02 12:04:22

标签: .net windows-phone-8 camera stream async-await

我正在尝试为Windows Phone 8.0实现一个简单的相机应用程序

public partial class MainPage : PhoneApplicationPage
{
    private const CameraSensorLocation SENSOR_LOCATION = CameraSensorLocation.Back;

    private AudioVideoCaptureDevice _videoDevice = null;

    // ... ctor and camInitialization methods etc.

    private async void CameraButtons_ShutterKeyPressed(object sender, EventArgs e)
    {    
        try{
            StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("videos", CreationCollisionOption.ReplaceExisting);
            StorageFile storageFile = await folder.CreateFileAsync("Video.mp4", CreationCollisionOption.GenerateUniqueName);

            using (var s = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                _videoDevice.VideoEncodingFormat = CameraCaptureVideoFormat.H264;
                await _videoDevice.StartRecordingToStreamAsync(s);

                Thread.Sleep(3000);

                // I want  the following 3 lines to be executed when I press the button again
                await _videoDevice.StopRecordingAsync();
                await s.FlushAsync();
                s.AsStream().Dispose();
            }
        } catch(Exception ex){
        }   
    }
}

一切正常 - 我在手机的IsolatedStorage中创建了一个文件,将其作为Stream打开并开始将视频录制到流中。现在,我显然不希望有Thread.Sleep来定义视频长度,但是当我再次按下按钮时我想停止录制。

我的问题是,我不知道如何做到这一点,因为我有一个在函数结束时将关闭的开放流。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我摆脱了使用块并为你的流创建属性/变量。这样就可以从多种方法中停止它。但是,您必须手动处理。

您还可以查看Microsoft提供的视频录制示例 - http://msdn.microsoft.com/en-us/library/windows/apps/hh394041%28v=vs.105%29.aspx