Android: MediaPlayer 不会播放 MP3

Android: MediaPlayer won't stream MP3

我一直无法让特定流在我的 MediaPlayer 应用程序中工作。我知道该应用程序可以运行,但是当我决定更改我的流媒体服务时,新的 URL 将无法运行。将此 URL 插入网络浏览器确实有效。这是我的相关代码:

String url = "http://smoke.wavestreamer.com:7562/listen.asx?sid=1";
mediaPlayer.setOnPreparedListener(this); // ...and other listeners...
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();

然后,在 onPrepared() 中:

mediaPlayer.start();

这是我的 LogCat(来自 运行 我的三星 Galaxy Nexus Android 4.3):

01-23 14:32:08.994    8726-8726/com.mycompany.myapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
01-23 14:32:08.994    8726-8726/com.mycompany.myapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11370: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
01-23 14:32:08.994    8726-8726/com.mycompany.myapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-23 14:32:08.994    8726-8726/com.mycompany.myapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
01-23 14:32:08.994    8726-8726/com.mycompany.myapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11376: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
01-23 14:32:08.994    8726-8726/com.mycompany.myapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-23 14:32:09.002    8726-8726/com.mycompany.myapp I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
01-23 14:32:09.002    8726-8726/com.mycompany.myapp W/dalvikvm﹕ VFY: unable to resolve virtual method 9064: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
01-23 14:32:09.002    8726-8726/com.mycompany.myapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
01-23 14:32:09.017    8726-8726/com.mycompany.myapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
01-23 14:32:09.017    8726-8726/com.mycompany.myapp W/dalvikvm﹕ VFY: unable to resolve virtual method 377: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
01-23 14:32:09.025    8726-8726/com.mycompany.myapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-23 14:32:09.025    8726-8726/com.mycompany.myapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
01-23 14:32:09.025    8726-8726/com.mycompany.myapp W/dalvikvm﹕ VFY: unable to resolve virtual method 399: Landroid/content/res/TypedArray;.getType (I)I
01-23 14:32:09.025    8726-8726/com.mycompany.myapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-23 14:32:09.244    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Binding to service
01-23 14:32:09.244    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ in startAndBindToService()
01-23 14:32:09.533    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ service.onStartCommand()
01-23 14:32:09.533    8726-8726/com.mycompany.myapp D/MediaPlayerService﹕ onStartCommand()
01-23 14:32:09.541    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ intent.getAction() is null (started from MainActivity); returning now
01-23 14:32:09.799    8726-8726/com.mycompany.myapp D/libEGL﹕ loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-23 14:32:09.892    8726-8726/com.mycompany.myapp D/libEGL﹕ loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-23 14:32:09.900    8726-8726/com.mycompany.myapp D/libEGL﹕ loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-23 14:32:10.088    8726-8726/com.mycompany.myapp D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 14:32:10.088    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Bound to service
01-23 14:32:10.096    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Initializing service.
01-23 14:32:10.096    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ setting up mediaPlayer
01-23 14:32:10.408    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Service set to run in foreground.
01-23 14:32:10.510    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ WifiLock acquired.
01-23 14:32:10.635    8726-8726/com.mycompany.myapp I/Choreographer﹕ Skipped 31 frames!  The application may be doing too much work on its main thread.
01-23 14:32:10.931    8726-8739/com.mycompany.myapp E/MediaPlayer﹕ error (1, -2147483648)
01-23 14:32:11.041    8726-8726/com.mycompany.myapp E/MediaPlayer﹕ Error (1,-2147483648)
01-23 14:32:11.041    8726-8726/com.mycompany.myapp E/MyMediaPlayer﹕ There has been an error of type MEDIA_ERROR_UNKNOWN, with no extras
01-23 14:32:11.041    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ There was an error initializing media player
01-23 14:32:11.056    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Restarting after error
01-23 14:32:11.056    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Initializing service.
01-23 14:32:11.056    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ setting up mediaPlayer
01-23 14:32:11.221    8726-8726/com.mycompany.myapp D/dalvikvm﹕ GC_FOR_ALLOC freed 231K, 3% free 9144K/9412K, paused 136ms, total 137ms
01-23 14:32:11.517    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ resetting mediaPlayer
01-23 14:32:11.627    8726-8856/com.mycompany.myapp E/MediaPlayer﹕ error (1, -2147483648)
01-23 14:32:11.838    8726-8726/com.mycompany.myapp E/MediaPlayer﹕ Error (1,-2147483648)
01-23 14:32:11.846    8726-8726/com.mycompany.myapp E/MyMediaPlayer﹕ There has been an error of type MEDIA_ERROR_UNKNOWN, with no extras
01-23 14:32:11.846    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ There was an error initializing media player
01-23 14:32:11.861    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Restarting after error
01-23 14:32:11.861    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Initializing service.
01-23 14:32:11.861    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ setting up mediaPlayer
01-23 14:32:11.900    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Service set to run in foreground.
01-23 14:32:11.971    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ resetting mediaPlayer
01-23 14:32:11.978    8726-8739/com.mycompany.myapp E/MediaPlayer﹕ error (1, -107)
01-23 14:32:12.064    8726-8726/com.mycompany.myapp E/MediaPlayer﹕ Error (1,-107)
01-23 14:32:12.064    8726-8726/com.mycompany.myapp E/MyMediaPlayer﹕ There has been an error of type MEDIA_ERROR_UNKNOWN, with no extras
01-23 14:32:12.064    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ There was an error initializing media player
01-23 14:32:12.080    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Restarting after error
01-23 14:32:12.080    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Initializing service.
01-23 14:32:12.080    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ setting up mediaPlayer
01-23 14:32:12.166    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Service set to run in foreground.
01-23 14:32:12.306    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ resetting mediaPlayer
01-23 14:32:12.471    8726-8740/com.mycompany.myapp E/MediaPlayer﹕ error (1, -2147483648)
01-23 14:32:12.525    8726-8726/com.mycompany.myapp E/MediaPlayer﹕ Error (1,-2147483648)
01-23 14:32:12.525    8726-8726/com.mycompany.myapp E/MyMediaPlayer﹕ There has been an error of type MEDIA_ERROR_UNKNOWN, with no extras
01-23 14:32:12.525    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ There was an error initializing media player
01-23 14:32:12.541    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Restarting after error
01-23 14:32:12.541    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Initializing service.
01-23 14:32:12.541    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ setting up mediaPlayer
01-23 14:32:12.603    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Service set to run in foreground.
01-23 14:32:12.666    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ resetting mediaPlayer
01-23 14:32:12.674    8726-8740/com.mycompany.myapp E/MediaPlayer﹕ error (1, -107)
01-23 14:32:12.721    8726-8726/com.mycompany.myapp E/MediaPlayer﹕ Error (1,-107)
01-23 14:32:12.728    8726-8726/com.mycompany.myapp E/MyMediaPlayer﹕ There has been an error of type MEDIA_ERROR_UNKNOWN, with no extras
01-23 14:32:12.728    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ There was an error initializing media player
01-23 14:32:12.744    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Restarting after error
01-23 14:32:12.744    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Initializing service.
01-23 14:32:12.744    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ setting up mediaPlayer
01-23 14:32:12.806    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ Service set to run in foreground.
01-23 14:32:12.869    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ resetting mediaPlayer
01-23 14:32:13.158    8726-8739/com.mycompany.myapp E/MediaPlayer﹕ error (1, -2147483648)
01-23 14:32:14.619    8726-8726/com.mycompany.myapp D/MyMediaPlayer﹕ resetting mediaPlayer
01-23 14:32:23.588    9136-9136/com.mycompany.myapp D/MyMediaPlayer﹕ service.onStartCommand()
01-23 14:32:23.588    9136-9136/com.mycompany.myapp D/MediaPlayerService﹕ onStartCommand()
01-23 14:32:23.588    9136-9136/com.mycompany.myapp D/MyMediaPlayer﹕ intent is null (restarted after service crashed); returning now
01-23 14:38:12.939    9136-9136/com.mycompany.myapp D/dalvikvm﹕ GC_EXPLICIT freed 49K, 1% free 8874K/8960K, paused 5ms+4ms, total 47ms
01-23 14:39:58.627  10507-10507/com.mycompany.myapp D/MyMediaPlayer﹕ service.onStartCommand()
01-23 14:39:58.627  10507-10507/com.mycompany.myapp D/MediaPlayerService﹕ onStartCommand()
01-23 14:39:58.627  10507-10507/com.mycompany.myapp D/MyMediaPlayer﹕ intent is null (restarted after service crashed); returning now
01-23 14:45:06.166  11567-11567/com.mycompany.myapp D/MyMediaPlayer﹕ service.onStartCommand()
01-23 14:45:06.166  11567-11567/com.mycompany.myapp D/MediaPlayerService﹕ onStartCommand()
01-23 14:45:06.166  11567-11567/com.mycompany.myapp D/MyMediaPlayer﹕ intent is null (restarted after service crashed); returning now
01-23 14:48:12.556  11567-11567/com.mycompany.myapp D/dalvikvm﹕ GC_EXPLICIT freed 40K, 1% free 8874K/8952K, paused 5ms+3ms, total 42ms
01-23 14:50:54.744  11567-11567/com.mycompany.myapp D/dalvikvm﹕ GC_EXPLICIT freed 1K, 1% free 8873K/8952K, paused 3ms+3ms, total 32ms
01-23 14:52:52.205  11567-11567/com.mycompany.myapp D/dalvikvm﹕ GC_EXPLICIT freed <1K, 1% free 8873K/8952K, paused 4ms+3ms, total 36ms

有什么想法吗?

编辑

我做了更多的挖掘,并设置了一个到上面提供的 link 的 HttpURL 连接。当我解析 headers 时,这是我发现的 (key=value):

null=[HTTP/1.1 200 OK]
Connection=[close]
Content-Length=[244]
Content-Type=[video/x-ms-asf]

当我在 Content-Type 上搜索时,我在 Wikipedia 上找到了这个:

...and .WMV (files containing video, using the Windows Media Audio and Video codecs, with MIME-type 'video/x-ms-asf')

这是否意味着流被编码为 .WMV 文件? (因此不受支持?)

编辑 2

我确信流被编码为 .MP3。不知道MediaPlayer为什么不读

使用这个:

            MediaPlayer Music = new MediaPlayer();
            Music = MediaPlayer.create(G.context, Uri.parse("PUT YOUR URL HERE"));
            Music.prepare();
            Music.start();

非常感谢 this answer SO,尽管 URL 坚持其 Content-Type 是媒体,但我像解析文本文件一样解析它并发现它一个文本文件——基本上,一个包含 link 到实际媒体流的播放列表。

对于可能遇到此问题的任何其他人,这是我用来解析 URL 并了解实际流媒体的代码 link:

        String url = "http://smoke.wavestreamer.com:7562/listen.asx";
        String charset = "UTF-8";
        String param1 = "1";

        try {
            String query = String.format("sid=%s", URLEncoder.encode(param1, charset));

            // Open a connection and InputStream to URL
            URLConnection connection = new URL(url + "?" + query).openConnection();
            connection.setRequestProperty("Accept-Charset", charset);
            InputStream response = connection.getInputStream();

            // Get status
            int status = ((HttpURLConnection) connection).getResponseCode();
            System.out.println("HttpURLConnection STATUS = " + status);

            // Get headers
            for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
                System.out.println(header.getKey() + "=" + header.getValue());
            }

            // Read response InputStream
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(response/*, charset*/))) {
                System.out.println("Reading 'response' InputStream:");
                for (String line; (line = reader.readLine()) != null;) {
                    System.out.println(line);
                }
            }

        } catch (UnsupportedEncodingException e) {
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }