MediaTranscoder.PrepareFileTranscodeAsync UnauthorizedAccessException

时间:2017-04-23 10:46:10

标签: c# uwp system.windows.media

我在页面上有一个Button,按钮的click事件使用以下方法:

StorageFile _sourceFile;    
private string _sourceToken;
private async void btnSelect_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker fop = new FileOpenPicker();
    fop.FileTypeFilter.Add(".mp4");

    StorageFile inFile = await fop.PickSingleFileAsync();
    _sourceToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(inFile);

    _sourceFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(_sourceToken);
    mediaElement.AutoPlay = false;
    IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.ReadWrite);
    mediaElement.SetSource(stream, _outFile.ContentType);

}

如果我点击MediaElement上的播放,我选择的视频就可以正常播放。

我还有另一个按钮,其点击事件中包含以下代码:

private async void btnExport_Click(object sender, RoutedEventArgs e)
{
    StorageFile outFile = await KnownFolders.VideosLibrary.CreateFileAsync("Outfie.mp4", CreationCollisionOption.ReplaceExisting);

    MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);

    MediaTranscoder transcoder = new MediaTranscoder();

    PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(_sourceFile, outFile, profile);

    if (prepareOp.CanTranscode)
    {
        var transcodeOp = prepareOp.TranscodeAsync();

        transcodeOp.Progress += new AsyncActionProgressHandler<double>(TranscodeProgress);
        transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
    }
    else
    {
        switch (prepareOp.FailureReason)
        {
            case TranscodeFailureReason.CodecNotFound:
                System.Diagnostics.Debug.WriteLine("Codec not found.");
                break;
            case TranscodeFailureReason.InvalidProfile:
                System.Diagnostics.Debug.WriteLine("Invalid profile.");
                break;
            default:
                System.Diagnostics.Debug.WriteLine("Unknown failure.");
                break;
        }
    }
}

不幸的是,行transcoder.PrepareFileTranscodeAsync抛出了UnauthorizedAccessException。但是如果我使用以下代替_sourceFile它可以工作:

StorageFile sourceFile = await KnownFolders.VideosLibrary.GetFileAsync("sourceFile.mp4");

抛出的错误是:

System.UnauthorizedAccessException:&#39;访问被拒绝。 (HRESULT异常:0x80070005(E_ACCESSDENIED))&#39;

要清楚,我选择了KnownFolders Enumeration外的文件,因此我使用的是Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList。

任何人都可以解释原因吗?

编辑: 如果我将源文件更改为FileOpenPicker的结果,那么它可以工作。所以它引出了一个问题,为什么FutureAccessList不工作​​?

private async void btnExport_Click(object sender, RoutedEventArgs e)
{
    StorageFile outFile = await KnownFolders.VideosLibrary.CreateFileAsync("Outfie.mp4", CreationCollisionOption.ReplaceExisting);

    FileOpenPicker fop = new FileOpenPicker();
    fop.SuggestedStartLocation = PickerLocationId.ComputerFolder;
    fop.FileTypeFilter.Add(".mp4");

    StorageFile sourceFile = await fop.PickSingleFileAsync();

    MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);

    MediaTranscoder transcoder = new MediaTranscoder();

    PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(sourceFile, outFile, profile);

    if (prepareOp.CanTranscode)
    {
        var transcodeOp = prepareOp.TranscodeAsync();

        transcodeOp.Progress += new AsyncActionProgressHandler<double>(TranscodeProgress);
        transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
    }
    else
    {
        switch (prepareOp.FailureReason)
        {
            case TranscodeFailureReason.CodecNotFound:
                System.Diagnostics.Debug.WriteLine("Codec not found.");
                break;
            case TranscodeFailureReason.InvalidProfile:
                System.Diagnostics.Debug.WriteLine("Invalid profile.");
                break;
            default:
                System.Diagnostics.Debug.WriteLine("Unknown failure.");
                break;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您是否可以访问您尝试写入的文件?也许它是只读的或由您自己以外的其他用户创建的? (在资源管理器中右键单击+属性可以让您更清楚地了解文件权限)

此外,如果您尝试写入您无法访问的文件夹,则可能会遇到该例外情况。

检查您的凭据,我猜它的相关内容。

答案 1 :(得分:1)

所以我似乎是在ReadWrite模式下打开源文件

IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.ReadWrite);

是问题的原因。根据{{​​3}}页面

  

只有当您准备好立即按 编写时才使用读/写模式,以避免与其他操作发生冲突。

所以我改为这个并且一切正常

IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.Read);