Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android media player error(1, -1004) while playing a stream from external website

I am trying to play an audio file from a website in my android App but it encounters media player error(1, -1004).

The streaming link is working fine when i use it in windows media player or vlc player. why android media player is generating this error? my code is

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_file);
    // Show the Up button in the action bar.
    setupActionBar();

     playSong("http://WEBSITE/001.mp3");
}

private void playSong(String songPath) {
    MediaPlayer mP = new MediaPlayer();
    try {
        mP.setDataSource(songPath);
        mP.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mP.prepare();
        mP.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();

            }
        });

    } catch (IllegalArgumentException e) {
          e.printStackTrace();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }


}
like image 717
yrahman Avatar asked Nov 02 '22 07:11

yrahman


1 Answers

Use Below Code

MediaPlayer mP = new MediaPlayer();
        mP.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mP.setDataSource(songPath);
            mP.setOnErrorListener(this);
            mP.setOnPreparedListener(this);
            mP.prepareAsync();
        } catch (IllegalArgumentException e) {

            e.printStackTrace();
        } catch (IllegalStateException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
like image 78
jyomin Avatar answered Nov 11 '22 11:11

jyomin