我已经实现了媒体流源类。当我点击一些时,我想停止相机馈送。关闭媒体元素和媒体流源的正确方法是什么。这样我的应用程序就不会崩溃。
这是我的简单代码Mainpage.xaml.cs
,CamerastreamSource
源自MediaStreamSource
MainPage.xaml.cs 文件代码。
async void MainPage_Loaded(object sender, RoutedEventArgs e) {
Windows.Foundation.Size size;
PhotoCaptureDevice m_camera;
CameraStreamsource m_vf;
Debug.WriteLine("supported capture resolution:");
foreach (var i in PhotoCaptureDevice.GetAvailableCaptureResolutions
(CameraSensorLocation.Back)) {
Debug.WriteLine(i.Height + "," + i.Width);
}
size = PhotoCaptureDevice.GetAvailableCaptureResolutions
(CameraSensorLocation.Back).First();
m_camera = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, size);
// Resolve and set the preview resolution
Debug.WriteLine("supported preview resolution:");
size.Width = 640; size.Height = 480;
await m_camera.SetPreviewResolutionAsync(size);
m_vf = new CameraStreamSource(m_camera,this);
//_mediaElement is the MediaElemet control in MainPage.xaml
_mediaElement.SetSource(m_vf);
}
CameraStreamSource : MediaStreamSource
代码
class CameraStreamSource : MediaStreamSource {
//private AudioVideoCaptureDevice _device;
public PhotoCaptureDevice _device;
private long _currentTime;
private readonly Dictionary<MediaSampleAttributeKeys, string> _emptySampleDict =
new Dictionary<MediaSampleAttributeKeys, string>();
//viewfinder display
private MediaStreamDescription _videoStreamDes;
private byte[] _byteBuf =null;
private MemoryStream _stream;
MediaStreamSample _sample = null;
public CameraStreamSource(PhotoCaptureDevice device,MainPage obj)
{
mainPage = obj;
_device = device;
}
protected override void GetSampleAsync(MediaStreamType mediaStreamType)
{
_device.GetPreviewBufferYCbCr(_byteBuf);
if (_sample == null)
{
_sample = new MediaStreamSample(_videoStreamDes, _stream, 0, _stream.Length, _currentTime, _emptySampleDict);
}
ReportGetSampleCompleted(_sample);
}
protected override void OpenMediaAsync()
{
// todo
Dictionary<MediaSourceAttributesKeys, string> mediaSourceAttributes =
new Dictionary<MediaSourceAttributesKeys, string>();
Dictionary<MediaStreamAttributeKeys, string> mediaStreamAttributes =
new Dictionary<MediaStreamAttributeKeys, string>();
List<MediaStreamDescription> mediaStreamDescriptions =
new List<MediaStreamDescription>();
mediaStreamAttributes[MediaStreamAttributeKeys.VideoFourCC] = "YV12";
mediaStreamAttributes[MediaStreamAttributeKeys.Width] = _device.PreviewResolution.Width.ToString();
mediaStreamAttributes[MediaStreamAttributeKeys.Height] = _device.PreviewResolution.Height.ToString();
_videoStreamDes =
new MediaStreamDescription(MediaStreamType.Video, mediaStreamAttributes);
mediaStreamDescriptions.Add(_videoStreamDes);
// a zero timespan is an infinite video
mediaSourceAttributes[MediaSourceAttributesKeys.Duration] =
TimeSpan.FromSeconds(0).Ticks.ToString(CultureInfo.InvariantCulture);
mediaSourceAttributes[MediaSourceAttributesKeys.CanSeek] = false.ToString();
// Report that we finished initializing its internal state and can now
// pass in frame samples.
ReportOpenMediaCompleted(mediaSourceAttributes, mediaStreamDescriptions);
_byteBuf = new byte[(int)((_device.PreviewResolution.Width * _device.PreviewResolution.Height) * 1.5)];
_stream = new MemoryStream(_byteBuf);
}
protected override void SeekAsync(long seekToTime)
{
// todo
_currentTime = seekToTime;
ReportSeekCompleted(seekToTime);
}
protected override void CloseMedia() {
// No implementation required
}
protected override void GetDiagnosticAsync(MediaStreamSourceDiagnosticKind diagnosticKind) {
throw new NotImplementedException();
}
protected override void SwitchMediaStreamAsync(MediaStreamDescription mediaStreamDescription) {
throw new NotImplementedException();
}
任何预兆都可以帮助我,我如何在一些类似按钮上优雅地点击关闭它。我实际上需要代码来MediaElement
优雅MediaStreamSource
地关闭