0

我正在开发搜索歌曲并能够播放和下载音乐/歌曲的应用程序。我使用了这个网址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 is null, can't getMirrorDisplayStatus!!! 09-21 11:26:49.674: I/MediaPlayer(6735): setLPAflag() out 09-21 11:26:58.693: E/MediaPlayer(6735): error (1, -2147483648) 09-21 11:26: 58.693: E/MediaPlayer(6735): 错误 (1,-2147483648)

AndroidMedia Library 是否只能播放 .mp3 格式?我可以从 Url 上方播放音乐吗?我该怎么做?

4

1 回答 1

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();
            }
于 2013-09-21T09:46:54.807 回答