【问题标题】:How to stream Video that are stored in Azure storage Blob using Xamarin如何使用 Xamarin 流式传输存储在 Azure 存储 Blob 中的视频
【发布时间】:2019-12-04 11:42:00
【问题描述】:

我已将视频文件上传到 Azur Blob(容器),我想通过 Streaming 在移动应用程序中访问它们。我有扩展名为 .mp4 的文件。我已经完成了从 blob 下载并存储在本地驱动器中的代码,然后使用默认播放器播放,但我想为用户提供流式传输而不是下载的选项。 这个方法我用过

var credentials = new StorageCredentials("myaccountname", "mysecretkey");
    var account = new CloudStorageAccount(credentials, true);
    var container = account.CreateCloudBlobClient().GetContainerReference("yourcontainername");
    var blob = container.GetBlockBlobReference("yourmp4filename");
    var sas = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
    {
        Permissions = SharedAccessBlobPermissions.Read,
        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),//Set this date/time according to your requirements
    });
    var urlToBePlayed = string.Format("{0}{1}", blob.Uri, sas);//This is the URI which should be embedded in your video player.

问题:- 如果我浏览 Url(Blob Url) ,它会下载文件而不是直接播放它。但是在 App 中,什么都没有出现。黑屏。 我正在使用

<WebView Source="{Binding VideoUrl}" HeightRequest="200" WidthRequest="200"/>

在虚拟机中:

VideoUrl=url;

【问题讨论】:

  • 您能检查一下您的视频 blob 的 ContentType 吗?它会影响浏览器对其进行操作的行为。如果是默认值application/octet-stream,应该是下载的,需要改成video/mp4
  • @ZhaoxingLu-Microsoft,感谢您的回复。我试过 this 。似乎 ListBlob 不可用,对于 ListBlobsSegmentedAsync ,我找不到一个。我希望我能找到一个使用 ListBlobsSegmentedAsync 的。你能帮忙吗?
  • 这是您需要的吗? stackoverflow.com/a/54934260/2995449
  • 没有。我使用 ListBlobsSegmentedAsync 对其进行了转换。现在它也在浏览器和应用程序中播放。这就像解决一个难题并跳到另一个难题。谢谢@ZhaoxingLu-Microsoft :)

标签: azure xamarin.forms azure-blob-storage


【解决方案1】:

首先更改内容类型:就像@Zhaoxing Lu - 微软所说的

public async Task ChangeContentTypeAsync()
{
    try
    {
        UserDialogs.Instance.ShowLoading();
        BlobContinuationToken blobContinuationToken = null;
        var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
        var blobClient = storageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("videos");
        var results = await container.ListBlobsSegmentedAsync(null, blobContinuationToken);
        blobContinuationToken = results.ContinuationToken;

        BlobResultSegment blobs = await blobClient
            .GetContainerReference("videos")
            .ListBlobsSegmentedAsync(blobContinuationToken)
            ;

        foreach (CloudBlockBlob blob in blobs.Results)
        {
            if (Path.GetExtension(blob.Uri.AbsoluteUri) == ".mp4")
            {
                blob.Properties.ContentType = "video/mp4";
            }
            //// repeat and  resume
            await blob.SetPropertiesAsync();
        }
        UserDialogs.Instance.HideLoading();
    }
    catch (Exception ex)
    {
        var m = ex.Message;
    }

}

那就用这个方法:

private async Task StreamVideo(string filename)
{
    try
    {
        UserDialogs.Instance.ShowLoading();
        await azureBlob.ChangeContentTypeAsync();
        var secretkey = "xxxx";
        var credentials = new StorageCredentials("chatstorageblob", secretkey);
        var account = new CloudStorageAccount(credentials, true);
        var container = account.CreateCloudBlobClient().GetContainerReference("videos");
        var blob = container.GetBlockBlobReference(filename);
        var sas = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),//Set this date/time according to your requirements
        });
        var urlToBePlayed = string.Format("{0}{1}", blob.Uri, sas);//This is the URI which should be embedded in your video player.
        await Navigation.PushAsync(new VideoPlayerPage(urlToBePlayed));
        UserDialogs.Instance.HideLoading();
    }
    catch (Exception ex)
    {
        var m = ex.Message;
    }
}

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2019-01-27
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多