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();
}
}
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With