簡體   English   中英

無法搜索使用HTTP源准備的音頻文件(Android MediaPlayer)

[英]Can't seek audio files prepared with HTTP source (Android MediaPlayer)

所以我正在嘗試構建某種媒體播放器,應用程序從外部源(坐在另一台設備上的HTTP服務器)准備音頻,當試圖尋找MediaPlayer返回“Stream沒有持續時間因此不可尋找”

注意:我看到有關同一錯誤的問題,但它們是關於實時流,這是一個靜態MP3文件。

來自其他設備的代碼(音頻文件的服務器)

private static void WriteFile(HttpListenerContext ctx, Android.Net.Uri uri, Activity activity)
        {
            var response = ctx.Response;
            Debug.Print(ctx.Request.RemoteEndPoint.ToString());
            using (var file = activity.ContentResolver.OpenInputStream(uri))
            {
                string fileName = "Audio.mp3";
                ICursor cursor = activity.ContentResolver.Query(uri, null, null, null, null);
                try
                {
                    if (cursor != null && cursor.MoveToFirst())
                        fileName = cursor.GetString(cursor.GetColumnIndex(OpenableColumns.DisplayName));
                }
                finally
                {
                    cursor.Close();
                }
                response.SendChunked = false;
                response.ContentType = "audio/mpeg";
                response.AddHeader("Content-disposition", "attachment; filename=" + fileName);

                byte[] buffer = new byte[64 * 1024];
                int read;
                using (BinaryWriter bw = new BinaryWriter(response.OutputStream))
                {
                    while ((read = file.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        bw.Write(buffer, 0, read);
                        bw.Flush();
                    }
                    bw.Close();
                    response.ContentLength64 = bw.BaseStream.Length;
                }

                response.StatusCode = (int)HttpStatusCode.OK;
                response.StatusDescription = "OK";
                response.OutputStream.Close();
            }

我不確定這段代碼是如何拋出“無法訪問封閉流”的異常:

bw.Close();
response.ContentLength64 = bw.BaseStream.Length;

但是......在關閉流之前檢索長度:

response.ContentLength64 = bw.BaseStream.Length;
bw.Close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM