1

我正在尝试使用 WindowsPhone 8.1 SDK 中的 MediaTranscoder 类创建带有图像的视频。如果我成功了,我会用它来创建一个包含更多图像的视频。

在下面的代码中,我在图片库中找到了一个图像并从中创建了一个 MediaStreamSample。这很好用!

然后我创建一个持续时间为 1(秒)的 MediaStreamSource 并将其与我之前创建的示例相关联。这也很好用!

不起作用的是 MediaTranscoder 实例。

await transcoder.PrepareMediaStreamSourceTranscodeAsync(src, ras, profile);

当我调用上述方法时,我得到一个“引用未设置为实例”异常。(空引用异常)

我不知道为什么会发生这种情况......请帮助我。

StorageFolder cameraFolder = KnownFolders.CameraRoll;
        IReadOnlyList<StorageFile> fileList = await cameraFolder.GetFilesAsync();

        var query = fileList.Where(f => f.Name.StartsWith("JeyLapse")).ToList();

        var stream = await query[0].OpenStreamForReadAsync();

        MediaStreamSample sample =
            await MediaStreamSample.CreateFromStreamAsync(stream.AsInputStream(), (uint)stream.Length, new TimeSpan());


        MediaStreamSource src = new MediaStreamSource(new VideoStreamDescriptor(VideoEncodingProperties.CreateMpeg2()));
        src.Duration = TimeSpan.FromSeconds(1);

        src.SampleRequested += (sender, args) =>
        {
            args.Request.Sample = sample;
        };

        src.Starting += (sender, args) => args.Request.SetActualStartPosition(new TimeSpan());
        src.SwitchStreamsRequested += (sender, args) => args.Request.GetDeferral().Complete();

        MediaTranscoder transcoder = new MediaTranscoder();
        MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);

        var newFile = await KnownFolders.VideosLibrary.CreateFileAsync("tryJeylapse", CreationCollisionOption.GenerateUniqueName);
        var writestr = await newFile.OpenStreamForWriteAsync();

        var ras = writestr.AsRandomAccessStream();
        transcoder.VideoProcessingAlgorithm = MediaVideoProcessingAlgorithm.Default;

        var trans = await transcoder.PrepareMediaStreamSourceTranscodeAsync(src, ras, profile);
        await trans.TranscodeAsync();
4

0 回答 0