Search code examples
iphone.netsafarijplayerashx

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


I have ASP.NET WebForms project with online player. When i click "play" player asks web handler for mp3 using such code:

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();
}

And after that player(i use JPlayer) starts playing the song. This thing works in all browsers, even on android and windows phones, tho it doesnt play some songs on safari(ON OSX! On windows Safari everything works fine) and iPhone. As i discovered there is something wrong for them with the handler, because when i try to set direct path to mp3 file that was not playing through handler-iphone plays it well. Just don't know what can be wrong here.. Why do they play some files and don't play other(mp3s were encoded using same software and they have same bitrate)?


Solution

  • In case anyone faces such problem..
    The answer is: safari/iphone respect REST too much. They need context.Response.AddHeader("Accept-Ranges", "bytes");
    to play mp3 properly.