azure - 如何使用 Xamarin 流式传输存储在 Azure 存储 Blob 中的视频

标签 azure xamarin.forms azure-blob-storage

我已将视频文件上传到 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;

最佳答案

首先更改内容类型:如@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;
    }
}

关于azure - 如何使用 Xamarin 流式传输存储在 Azure 存储 Blob 中的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57214767/

相关文章:

azure - Mach-O header 代码 : 0x72613c21 when importing Microsoft Azure Mobile Framework

azure - 无法将服务计划连接到 vnet

git - Azure git,将 fork 存储库与主同步

java - 使用 SHA 3 算法代替 SHA 1 为 Native 和 Xamarin 签名 Android Apk

azure - ARM 模板为存储容器类型数组抛出不正确的段长度

c# - OData 和 Cosmos DB

xaml - Xamarin Forms : How Can I Change the FlyoutItem. 选择/取消选择时图标的颜色?

c# - Android 上的 Xamarin.Forms TapRecognizer 取消 ItemTapped 事件

azure-blob-storage - 从逻辑应用读取 blob 内容

Azure 逻辑应用程序 - 如何将文件从字节数组上传到 Azure Blob 存储