我被告知,当我允许用户在我的应用内创建视频时,应该会自动生成.jpg缩略图。但是,使用Windows Phone电动工具,我可以看到只生成视频,没有图像。以下是代码:
编辑:问题在这里,是如何从保存在独立存储中的给定视频中获取缩略图?
' Set recording state: start recording.
Private Async Sub StartVideoRecording()
Try
App.ViewModel.IsDataLoaded = False
'isoStore = Await ApplicationData.Current.LocalFolder.GetFolderAsync("IsolatedStore")
strVideoName = GenerateVideoName()
isoFile = Await isoVideoFolder.CreateFileAsync(strVideoName, CreationCollisionOption.ReplaceExisting)
thisAccessStream = Await isoFile.OpenAsync(FileAccessMode.ReadWrite)
Await avDevice.StartRecordingToStreamAsync(thisAccessStream)
'save the name of the video file into the list of video files in isolated storage settings
Dim videoList As New List(Of InfoViewModel)
isoSettings = IsolatedStorageSettings.ApplicationSettings
If isoSettings.Contains("ListOfVideos") Then
videoList = isoSettings("ListOfVideos")
Else
isoSettings.Add("ListOfVideos", videoList)
End If
videoList.Add(New InfoViewModel With {.Name = strVideoName, .DateRecorded = Date.Now})
isoSettings("ListOfVideos") = videoList
isoSettings.Save()
isoSettings = Nothing
' Set the button states and the message.
UpdateUI(ButtonState.Recording, "Recording...")
Catch e As Exception
' If recording fails, display an error.
Me.Dispatcher.BeginInvoke(Sub() txtDebug.Text = "ERROR: " & e.Message.ToString())
End Try
End Sub
答案 0 :(得分:1)
在Windows Phone 7.0中,除了使用CameraCaptureTask之外,无法使用“官方”SDK访问相机。但是使用Microsoft.Phone.InteropServices
库可以在应用程序中创建自定义相机视图。如果您使用此VideoCamera
类,则会有一个名为ThumbnailSavedToDisk
的事件,这可能是您所听到的。但是,Windows Phone Marketplace中从未允许使用Microsoft.Phone.InteropServices
的应用。您可以阅读更多about it here。
要从视频文件生成缩略图,一种解决方案是阅读MP4文件格式的工作原理并提取其中一个帧并将其用作缩略图。不幸的是,我没有找到任何可以做到这一点的库。
另一个,但远非最佳解决方案,将是在MediaElement中播放视频,然后将该控件呈现为位图。这可以通过以下代码完成(用XAML / C#编写,我不知道VB.net,但应该很容易移植):
<MediaElement
x:Name="VideoPlayer"
Width="640"
Height="480"
AutoPlay="True"
RenderTransformOrigin="0.5, 0.5"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Stretch="Fill"
Canvas.Left="80"/>
然后创建缩略图:
// Set an event handler for when video has loaded
VideoPlayer.MediaOpened += VideoPlayer_MediaOpened;
// Read video from storage
IsolatedStorageFileStream videoFile = new IsolatedStorageFileStream("MyVideo.mp4", FileMode.Open, FileAccess.Read, IsolatedStorageFile.GetUserStoreForApplication());
VideoPlayer.SetSource(videoFile);
// Start playback
VideoPlayer.Play();
然后创建“捕获”缩略图的事件处理程序:
void VideoPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
Thread.Sleep(100); // Wait for a short time to avoid black frame
WriteableBitmap wb = new WriteableBitmap(VideoPlayer, new TranslateTransform());
thumbnailImageHolder.Source = wb; // Setting it to a Image in the XAML code to see it
VideoPlayer.Stop();
}
以下是使用Video Recorder Sample的修改版本时的外观。 (右上角带有蓝色边框的小图像是录制视频的缩略图。后面是相机查看器。)