Windows Phone 8.1 多媒体(2):视频

本文介绍了Windows Phone 8.1平台上的多媒体开发技术,包括视频拍摄、视频编辑及屏幕录制等功能实现方法,并提供了详细的代码示例。

Windows Phone 8.1 多媒体(1):相片

Windows Phone 8.1 多媒体(2):视频

Windows Phone 8.1 多媒体(3):音乐

 


 

(1)拍摄视频

拍摄视频和拍摄相片的方法是基本一致的:

MediaCapture mediaCapture;
MediaEncodingProfile videoEncodingProperties;

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    HardwareButtons.CameraHalfPressed += HardwareButtons_CameraHalfPressed;
    HardwareButtons.CameraReleased += HardwareButtons_CameraReleased;

    videoCaptrueElement.Source = await Initialize();
    await mediaCapture.StartPreviewAsync();
}

async void HardwareButtons_CameraHalfPressed(object sender, CameraEventArgs e)
{
    if( mediaCapture != null )
    {
        var video = await KnownFolders.VideosLibrary.CreateFileAsync("video.mp4", CreationCollisionOption.GenerateUniqueName);
await mediaCapture.StartRecordToStorageFileAsync(videoEncodingProperties, video);
    }
}

async void HardwareButtons_CameraReleased(object sender, CameraEventArgs e)
{
    if( mediaCapture != null )
    {
        await mediaCapture.StopRecordAsync();
    }
}

private async Task<MediaCapture> Initialize()
{
    mediaCapture = new MediaCapture();
    await mediaCapture.InitializeAsync();

    mediaCapture.VideoDeviceController.PrimaryUse = CaptureUse.Video;

    videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);

    return mediaCapture;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    if( mediaCapture != null )
    {
        mediaCapture.Dispose();
        mediaCapture = null;
    }
}

 

(2)编辑视频

视频编辑的 API 在 Windows.Media.Editing 命名空间下,具体可看 MSDN:链接

简单的说就是把某些视频实例化为 MediaClip,然后将这些视频添加到 MediaComposition.Clips 中去,最后将这些视频拼接到一起或添加个 BackgroundAudioTrack 什么的:

MediaClip video = await MediaClip.CreateFromFileAsync(
                await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///thanks.mp4"))); MediaComposition videos = new MediaComposition(); videos.Clips.Add(video); BackgroundAudioTrack bgm = await BackgroundAudioTrack.CreateFromFileAsync(
                    await StorageFile.GetFileFromApplicationUriAsync(new Uri("Above Your Hand.mp3"))); videos.BackgroundAudioTracks.Clear(); videos.BackgroundAudioTracks.Add(bgm); await videos.SaveAsync(await ApplicationData.Current.LocalFolder.CreateFileAsync("video.mp4", CreationCollisionOption.ReplaceExisting));

 

(3)录制手机屏幕视频

录制手机屏幕视频是 WP8.1 新加的 API,使用方法和拍摄视频差不多,只需将录制对象设为屏幕即可:

var screenCapture = ScreenCapture.GetForCurrentView();

mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
    VideoSource = screenCapture.VideoSource,
    AudioSource = screenCapture.AudioSource,
});

var file = await KnownFolders.VideosLibrary.CreateFileAsync("screenrecording.mp4", CreationCollisionOption.ReplaceExisting);
await mediaCapture.StartRecordToStorageFileAsync(MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto), file);

停止录制:

if( mediaCapture != null )
{
    await mediaCapture.StopRecordAsync();
    mediaCapture.Dispose();
    mediaCapture = null;
}

 

转载于:https://www.cnblogs.com/xiaoshi3003/p/3784167.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值