zoukankan      html  css  js  c++  java
  • asp.net mvc 返回音频,可支持快进

     在响应头里,需要加上    Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", startbyte, endbyte, fSize)); 
     
     
        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);
        }

    http://stackoverflow.com/questions/31246314/mvc-audio-controls-playing-song-from-bytes

  • 相关阅读:
    python GUI
    Python 博客网站资源
    Python 100 天学习计划
    pycharm基本设置
    MySQL 57安装部署(Zip版)(Windows版)
    nginx在centos下的安装
    仓位计算
    python笔记
    vue(一)--监听事件
    Hibernate(五)--级联
  • 原文地址:https://www.cnblogs.com/sdwdjzhy/p/5368758.html
Copyright © 2011-2022 走看看