从URL流式传输和下载音频

时间:2013-09-21 06:04:46

标签: android android-mediaplayer

我正在开发搜索歌曲并能够播放和下载音乐/歌曲的应用程序。我使用了此网址http://tinysong.com/Phdj,这是重定向到音乐网页并播放其播放的歌曲。我用下面的代码来播放歌曲。

    //play music
    MediaPlayer mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try{
         mMediaPlayer.setDataSource("http://tinysong.com/Phdj");
            mMediaPlayer.prepareAsync();
                }catch(IOException e){
        e.printStackTrace();
        }
        catch (IllegalArgumentException e){
                e.printStackTrace();    
                }catch (IllegalStateException e){
                e.printStackTrace();
            mMediaPlayer.setOnPreparedListener(new   MediaPlayer.OnPreparedListener() 
                            {

                                @Override
                                public void onPrepared(MediaPlayer mp) 
                                {
                                    mp.start();
                                }
                            });

错误: 09-21 11:26:49.674:I / MediaPlayer(6735):setLPAflag()in 09-21 11:26:49.674:I / MediaPlayer(6735):mContext为null,无法getMirrorDisplayStatus !!! 09-21 11:26:49.674:I / MediaPlayer(6735):setLPAflag()输出 09-21 11:26:58.693:E / MediaPlayer(6735):错误(1,-2147483648) 09-21 11:26:58.693:E / MediaPlayer(6735):错误(1,-2147483648)

AndroidMedia Library是否只播放.mp3格式?我可以在Url上面播放音乐吗?我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您尚未通过setDataSource()

中的上下文

试试这个

String path="http://tinysong.com/Phdj";

    Uri myUri = Uri.parse(path);

            MediaPlayer mp = new MediaPlayer();
            try {

                mp.setDataSource(this, myUri);
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mp.setOnPreparedListener(new OnPreparedListener() {

                    @Override
                    public void onPrepared(MediaPlayer mp) {

                        mp.start();
                        // TODO Auto-generated method stub

                    }
                });
                mp.prepareAsync();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }