繁体   English   中英

在iPhone的网络浏览器中从ashx处理程序播放MP3文件

[英]Play MP3 file from ashx handler in iPhone's web browser

我有带有在线播放器的ASP.NET WebForms项目。 当我单击“播放”时,播放器会使用以下代码向网络处理程序询问mp3:

public void ProcessRequest(HttpContext context)
    {
        context.Response.Clear();
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Cache.SetNoStore();
        context.Response.Cache.SetExpires(DateTime.MinValue);

        context.Response.ContentType = "audio/mpeg";

        string songID = context.Request.QueryString["song"];
        if (string.IsNullOrEmpty(songID))
            return;
        Song song = new Song();
        song.GetSong(long.Parse(songID));
        // Show filename in format "artist - song.mp3"
        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + (string.IsNullOrEmpty(song.ArtistName) ? song.Title : (song.ArtistName + " - " + song.Title))); // title for file download
        context.Response.WriteFile(song.FileName);

        context.Response.End();
}

然后,该播放器(我使用JPlayer )开始播放歌曲。 这个东西在所有浏览器中都可以使用,即使在Android和Windows Phone上也是如此,但是它无法在Safari(ON OSX!在Windows Safari上一切正常)和iPhone上播放一些歌曲。 正如我发现的那样,处理程序对他们来说有问题,因为当我尝试设置未通过处理程序iphone播放的mp3文件的直接路径时,它播放得很好。 只是不知道这里可能出什么问题。为什么它们播放某些文件而不播放其他文件(mp3是使用相同软件编码的,并且具有相同的比特率)?

万一有人遇到这样的问题。
答案是:Safari / iphone过分尊重REST。 他们需要context.Response.AddHeader("Accept-Ranges", "bytes");
正确播放mp3。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM