如何读取RANGE请求标头C#

时间:2018-12-19 13:12:04

标签: azure audio streaming

如何读取RANGE请求标头并相应地将字节发送回范围标头

我将此代码编写为在Chrome上运行,但不在Firefox中运行,实际上我想直接在浏览器中播放音频类型的ogg,但我不能为Firefox只能在Chrome上使用此解决方案

请帮助?

提前谢谢

CloudFile file = null;
string url = Request.Url.AbsoluteUri;
string fileName = Path.GetFileName(url);

var storageaccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(fpuser, fppass), false);
CloudFileClient fileClient = storageaccount.CreateCloudFileClient();
// Get a reference to the file share we created previously.
CloudFileShare share = fileClient.GetShareReference(ShareFolder);
//share.CreateIfNotExistsAsync().Wait();
// Ensure that the share exists.
if (share.Exists())
{
    // Get a reference to the root directory for the share.
    CloudFileDirectory rootDir = share.GetRootDirectoryReference();

    // Get a reference to the directory we created previously.
    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(Pathurl);

    // Ensure that the directory exists.
    if (sampleDir.Exists())
    {
        // Get a reference to the file we created previously.
        file = sampleDir.GetFileReference(fileName);

        // Ensure that the file exists.
        if (file.Exists())
        {
            var context = System.Web.HttpContext.Current;
            Console.Write("before the file");
            try
            {
                using (Stream fs = file.OpenRead())
                {
                    Console.Write("in the file");

                    context.Response.ContentType = "audio/ogg";
                    //context.Response.Conv = "audio/ogg";
                    context.Response.AddHeader("Content-Type", "audio/ogg");
                    context.Response.Headers.Add("Content-Range", $"bytes 0-{fs.Length - 1}/{fs.Length}");
                    context.Response.AddHeader("Accept-Ranges", "0-" + fs.Length);
                    context.Response.AddHeader("Content-Length", fs.Length.ToString());

                    context.Response.StatusCode = 206; // set to partial content

                    byte[] buffer = new byte[64 * 1024];
                    //// read in chunks of 2KB
                    //byte[] buffer = new byte[2048];
                    try
                    {
                        using (BinaryWriter bw = new BinaryWriter(context.Response.OutputStream))
                        {
                            int read;
                            while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                bw.Write(buffer, 0, read);
                                bw.Flush(); //seems to have no effect
                            }
                            bw.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write("closded connection");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write("Error - " + ex);
            }
         // }  
         }
     }
 }

 return null;

0 个答案:

没有答案