Opening app without any error. But can’t play music. But can play music from local machine. Internet connection is fine.
I have enabled internet permission in AndroidManiFest.xml
<uses-permission android:name="android.permission.INTERNET" />
I have enabled http permission.
android:usesCleartextTraffic="true"
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("http://penguinradio.dominican.edu/Sound%20FX%20Collection/Motorbike.mp3");
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mP) {
Toast.makeText(MainActivity.this, "Ready to Play", Toast.LENGTH_SHORT).show();
mP.start();
}
});
mediaPlayer.prepareAsync();
Full Code:
https://paste.ubuntu.com/p/dsjbg7YMNn/
2
Answers
I have tested your code and sometimes it works, sometimes it doesn’t. If you search on
Stack Overflow
you will find plenty of similar problems.You have 2 options:
ExoPlayer2
which is far superior thatMediaPlayer
.OnPreparedListener
, but you can play it on button click, after it’s prepared:try this: