Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC音频控制从字节播放歌曲_Asp.net Mvc_Byte_Html5 Audio - Fatal编程技术网

Asp.net mvc MVC音频控制从字节播放歌曲

Asp.net mvc MVC音频控制从字节播放歌曲,asp.net-mvc,byte,html5-audio,Asp.net Mvc,Byte,Html5 Audio,我的歌曲以字节[]的形式存储在数据库中。如何在标记中使用这些 像这样。我需要先将字节转换为其他内容吗?我不确定 foreach (var item in Model) { <audio controls> <source src=@item.SongBytes type="audio/mp3"/> </audio> } foreach(模型中的变量项) { } 一种方法是在控制器中添

我的歌曲以字节[]的形式存储在数据库中。如何在
标记中使用这些

像这样。我需要先将字节转换为其他内容吗?我不确定

foreach (var item in Model)
    {
        <audio controls>
            <source src=@item.SongBytes type="audio/mp3"/>
        </audio>  
    }
foreach(模型中的变量项)
{
}

一种方法是在控制器中添加一个新操作,以返回数据:

public ActionResult Audio(int someId)
{
    byte[] songBytes; 
    // Add code to get data
    return new FileStreamResult(songBytes, "audio/mp3");
}
然后将该URL放入src属性:

foreach (var item in Model)
{
    <audio controls>
        <source src="/Controller/Audio/@item.someId" type="audio/mp3"/>
    </audio>  
}
foreach(模型中的变量项)
{
}

谷歌chrome/Ipad需要对内容范围请求的支持,因此要在此处添加给定答案,请执行以下操作:

 public FileStreamResult StreamUploadedSongs(int id)
    {
        byte[] song = db.UploadedSongs.Where(x => x.Id == id).FirstOrDefault().SongBytes;

        long fSize = song.Length;
        long startbyte = 0;
        long endbyte = fSize - 1;
        int statusCode = 200;
        if ((Request.Headers["Range"] != null))
        {
            //Get the actual byte range from the range header string, and set the starting byte.
            string[] range = Request.Headers["Range"].Split(new char[] { '=', '-' });
            startbyte = Convert.ToInt64(range[1]);
            if (range.Length > 2 && range[2] != "") endbyte = Convert.ToInt64(range[2]);
            //If the start byte is not equal to zero, that means the user is requesting partial content.
            if (startbyte != 0 || endbyte != fSize - 1 || range.Length > 2 && range[2] == "")
            { statusCode = 206; }//Set the status code of the response to 206 (Partial Content) and add a content range header.                                    
        }
        long desSize = endbyte - startbyte + 1;
        //Headers
        Response.StatusCode = statusCode;

        Response.ContentType = "audio/mp3";
        Response.AddHeader("Content-Accept", Response.ContentType);
        Response.AddHeader("Content-Length", desSize.ToString());
        Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", startbyte, endbyte, fSize));
        //Data

        var stream = new MemoryStream(song, (int)startbyte, (int)desSize);

        return new FileStreamResult(stream, Response.ContentType);
    }

我的问题是,当我试图在chrome中通过wavesurfer.js调用controller方法来播放.wav文件时,该方法返回文件正在播放的ActionResult,但我无法向前和向后搜索。当我搜索时,玩家返回起始位置。尽管此功能在Firfox中运行良好。

我相信您需要
范围[2]!=“”
或者,您可以获得带有全范围标题的206。否则这帮了我大忙,谢谢@乔纳森很高兴这有帮助。我记得很难找到解决方案。我建议的另一个更改是始终发送
206
,即使范围标头包含完整文件。至少在我过去一周的经验中,Chrome
不允许搜索带有
200
的文件。谢谢你的回答。您可以添加一些示例代码参考或链接吗