我想在asp.net应用上流式传输音频文件。 我找到了一些webapi流媒体的例子,我写道:
Public Class AudioController
Inherits ApiController
Public Function [Get](id As String) As HttpResponseMessage
Dim T As iTunesLib.IITTrack = Global_asax.iTunesApp.LibraryPlaylist.Tracks.ItemByPersistentID(Convert.ToInt32(Microsoft.VisualBasic.Left(id, 8), 16), Convert.ToInt32(Microsoft.VisualBasic.Right(id, 8), 16))
Dim filename As String = ""
If T.Kind = ITTrackKind.ITTrackKindCD Or T.Kind = ITTrackKind.ITTrackKindFile Then
filename = CType(T, IITFileOrCDTrack).Location
End If
If T.Kind = ITTrackKind.ITTrackKindURL Then
filename = CType(T, IITURLTrack).URL
End If
Dim vidFile = File.OpenRead(filename)
Return New ProgressiveDownload(Request).ResultMessage(vidFile, "audio/m4a")
End Function
End Class
Public Class ProgressiveDownload
Public ReadOnly Property IsRangeRequest() As Boolean
Get
Return _Request.Headers.Range IsNot Nothing AndAlso _Request.Headers.Range.Ranges.Count > 0
End Get
End Property
Public Function ResultMessage(stream As Stream, mediaType As String) As HttpResponseMessage
Try
If IsRangeRequest Then
Dim content = New ByteRangeStreamContent(stream, _Request.Headers.Range, mediaType)
Dim response = _Request.CreateResponse(HttpStatusCode.PartialContent)
response.Content = content
Return response
Else
Dim content = New StreamContent(stream)
Dim response = _Request.CreateResponse(HttpStatusCode.OK)
response.Content = content
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType)
Return response
End If
Catch ibr As InvalidByteRangeException
Return _Request.CreateErrorResponse(ibr)
Catch e As Exception
Return _Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)
End Try
End Function
Public Sub New(request As HttpRequestMessage)
_Request = request
End Sub
Private _Request As HttpRequestMessage
End Class
JS
function Play(sourceUrl) {
var audio = $("#Lecteur");
if ($('#SRC').length == 0) {
audio.append(' <source id="SRC" type="audio/mp4" />');
}
$("#SRC").attr("src",'/api/audio/' + sourceUrl);
/****************/
audio[0].pause();
audio[0].load();//suspends and restores all audio element
//audio[0].play(); changed based on Sprachprofi's comment below
audio[0].oncanplaythrough = audio[0].play();
/****************/
}
当然是HTML
<audio id="Lecteur" controls="controls">
<source id="SRC" type="audio/mp4" />
</audio>
它在FF和IE上工作得很好,当我更改ID文件声音停止和strat immediatlly(文件是complet下载之前),在Chrome上它不太好 但不适用于iOS ... :(
将juste流式音频文件传输到HTML播放器的最佳做法是什么... 也许用jquery插件,但女巫?
感谢