【问题标题】:Can't seek audio files prepared with HTTP source (Android MediaPlayer)无法查找使用 HTTP 源准备的音频文件 (Android MediaPlayer)
【发布时间】:2019-06-11 18:32:42
【问题描述】:

所以我正在尝试构建某种媒体播放器,应用程序从外部源(位于另一台设备上的 HTTP 服务器)准备音频,当尝试寻找 MediaPlayer 时返回“流没有持续时间,因此不是可寻”

注意:我看到了关于相同错误的问题,但它们是关于直播的,这是一个静态 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();
            }

【问题讨论】:

    标签: c# android xamarin xamarin.android android-mediaplayer


    【解决方案1】:

    我不确定这段代码如何没有抛出“无法访问关闭的流”异常:

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

    但是...在关闭流之前检索长度:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多