我正在尝试使用WindowsPhone 8.1 SDK中的MediaTranscoder类创建带有图像的视频。如果我成功,我会用它来创建一个包含更多图像的视频。
在下面的代码中,我在图片库中找到一个图像并从中创建一个MediaStreamSample。这很好用!
然后我创建一个持续时间为1(秒)的MediaStreamSource,并将其与之前创建的样本相关联。这也很好用!
MediaTranscoder实例不起作用。
await transcoder.PrepareMediaStreamSourceTranscodeAsync(src, ras, profile);
当我调用上面的方法时,我得到一个"引用未设置为实例"例外。 (null参考例外)
我不知道为什么会这样......请帮助我。
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();