【问题标题】:src in audio-tag with webapi stream getting songname from responsephrase带有 Web api 流的音频标签中的 src 从响应短语中获取歌曲名称
【发布时间】:2016-08-29 02:10:23
【问题描述】:

我有一个带有 html5 音频标签的页面。 src 正在询问 webapi 控制器。控制器返回异步流,在音频标签中播放。 现在我添加了随机功能并想显示歌曲标题。在控制器中,我向响应中添加了带有歌曲标题的响应短语。 这里是 HTML 代码:

<div id="audioControl" class="col-md-6">
  <audio controls autoplay="autoplay" id="player">
    <source id="audioSource" src='/api/Audio/mp3/' type='audio/mp3' />
    not supported
  </audio>

和控制器:

var response = Request.CreateResponse();
FileInfo fileInfo = new FileInfo(rndPath[rndIndex]); //path of the mp3 
response.StatusCode = HttpStatusCode.OK;
response.ReasonPhrase = fileInfo.Name;
response.Content = new PushStreamContent
    (audio.WriteToStream, new   MediaTypeHeaderValue("audio/mp3"));
return response;

使用 fiedler 或浏览器的网络选项卡,我得到了正确的 mp3 名称。现在我想使用 javascript 函数设置&lt;source&gt; 标记的src 的src,我可以获得带有歌曲标题的响应短语。 知道该怎么做吗?

或者是获取 mp3 名称的唯一方法,使 web-api 有状态或使用 cookie?

我的最新想法是将名称保存在服务器会话变量中,并通过另一个 api 调用获取名称。

【问题讨论】:

    标签: javascript asp.net-web-api


    【解决方案1】:

    找到了解决办法。 在控制器中:

    FileInfo fileInfo = new FileInfo(rndPath[rndIndex]); 
    response.StatusCode = HttpStatusCode.OK;
    response.ReasonPhrase = fileInfo.Name;
    HttpContext.Current.Cache[User.Identity.Name] = fileInfo.Name;
    
    response.Content = new PushStreamContent
      (audio.WriteToStream, new    MediaTypeHeaderValue("audio/mp3"));
    return response;
    

    Guess 不能在打开 2 个浏览器窗口的情况下工作。 添加了一个新控制器:

     public class TitleController : ApiController
          {
            public string Get()
            {
              string title =(string)HttpContext.Current.Cache[User.Identity.Name.ToString()];
    
          return title;
        }
      }
    

    在视图中

    var aud = document.getElementById("player");
    aud.onloadeddata = function ()
          {
            $.ajax({
              url: 'api/Title/',
              cache: false,
              async: false
            }).success(function (data)
            {
              $("#title").html(data);
    
        });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2015-03-25
      相关资源
      最近更新 更多