简体   繁体   English

使用Android媒体播放器播放m3u8音频文件

[英]Play m3u8 audio file using android media player

I am developing an android application for android OS > 4.0 (including and post OS). 我正在为Android OS> 4.0(包括和发布操作系统)开发一个Android应用程序。 I have a sample m3u8 file as follows : 我有一个示例m3u8文件如下:

#EXTM3U
#EXT-X-TARGETDURATION:56
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:28, no desc
ulr/audio/file.mp3
#EXTINF:28, no desc
ulr/audio/file.mp3
#EXT-X-ENDLIST

and i am trying to play that file, using the following code 我正在尝试播放该文件,使用以下代码

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try
        {
            mMediaPlayer.setDataSource(uri);
        } catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        } catch (SecurityException e)
        {
            e.printStackTrace();
        } catch (IllegalStateException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        mMediaPlayer.prepareAsync();

and my onPrepared() method is as follows : 我的onPrepared()方法如下:

public void onPrepared(MediaPlayer player)
    {
        player.start();
    }

But the code first comes to onPrepared() and then immediately goes to onError() , with the what=1 and the extra=-1010 . 但代码首先来到onPrepared()然后立即进入onError() ,其中what=1extra=-1010

I know this question has been asked various times ( here , here and here for instance) and I also know about Vitamio , but i want to find out what is wrong with my implementation. 我知道这个问题已被多次询问( 这里这里这里 ),我也知道Vitamio ,但我想知道我的实现有什么问题。 Is there something wrong with the m3u8 file that I created? 我创建的m3u8文件有问题吗? I went through its documentation and everything seems correct. 我浏览了它的文档 ,一切似乎都正确。

Would be really glad if someone could throw some light in this matter. 如果有人可以在这件事上提出一些启示,那真的很高兴。

-Thanks in advance -提前致谢

Error code -1010 matches up with MEDIA_ERROR_UNSUPPORTED which would imply that the device does not have the hardware or software codecs it needs to decode the MP3 files in your playlist. 错误代码-1010与MEDIA_ERROR_UNSUPPORTED匹配,这意味着设备没有解码播放列表中MP3文件所需的硬件或软件编解码器。

Vitamio would work in this situation because it adds software decoding for the media. Vitamio可以在这种情况下工作,因为它为媒体添加了软件解码。 This is slower than hardware decoding and uses more battery. 这比硬件解码慢,并且使用更多电池。 It can also increase your app size significantly. 它还可以显着增加您的应用程序大小。

This seems odd, though, since MP3 has been a supported media format for decoding in Android for a very long time. 然而,这看起来很奇怪,因为MP3已经成为Android中解码的支持媒体格式很长一段时间了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM