I try to play a wav file streaming from the web.
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
Log.e("SoundPlayerService", "Error preparing music");
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(Tag, "Audio is playing from: " + url);
mp.start();
}
});
But it does not playing anything. What's the problem with the code?
You should use mediaPlayer.prepareAsync() instead mediaPlayer.prepare()
Related
I am working with MediaPlayer in Android. I have all the list of songs name and URLs of songs from server. when I click on any item a song from server is played in MediaPlayer. Here is my code for playing song on click of "Listview" item.
txtEndTimingForMediaPlayer.setText("");
txtStarTimingForMediaPlayer.setText("");
seekBarPlayer.setProgress(0);
// this code is for stop current playing song and release media player
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
}
// start new song for play
mediaPlayer=new MediaPlayer();
Uri myUri1 = Uri.parse(url);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(getActivity(), myUri1);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
playPause=true;
player.start();
mediaFileLengthInMilliseconds = player.getDuration();
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playPause=false;
btnPlayPause.setBackgroundResource(R.drawable.icon_play);
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Now issue with this code is when I click continuously on song, then same song plays multiple time. how to avoid this multiple instance issue.
I think if(mediaPlayer!=null && mediaPlayer.isPlaying()) is not true when you click too fast to play same song and then mediaPlayer=new MediaPlayer(); is creating new instance and play song, which will result in hearing same song multiple times. debug and check if code enter that if (condition)
mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
try {
mediaPlayer.setDataSource("192.168.191.1/test.mp3");
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
above is my code. It works well from such url: http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3
But it don't work when I try to access mp3 file in my server, 192.168.191.1/test.mp3, and I use wamp for my server.
I had the same requirement and it is working on my end :
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("mp3 link"
);
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}
I have an app where I want, as soon as it starts, a little background music (opa gangam style!) to be played (from the sd card). I use the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/mymusic/gangamstyle.mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
But when I test it, no music is played. I see everything is ok however. What could I do wrong? Thanks a lot
You have to use setOnPreparedListener in order to know when the media player is ready to play:
MediaPlayer player = new MediaPlayer();
player.setDataSource(filePath);
player.setVolume(100, 100);
player.setLooping(false);
player.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.start();
}
});
player.prepare();
There is some problem in your file path. Media player gives this kind of error when the file path is not correct. So, Please check your file path and then try it.
I'm trying to play a sound in android using media player with no success,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "https://dl.dropboxusercontent.com/u/108022472/5041046.mp3";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();
}
I'm getting this from logcat:
prepareAsync called in state 1
Anything wrong in the code above?
You're already prepared by calling prepare(). There's no need to call prepareAsync() if you already called prepare(). Drop one of those two calls.
My media player delays its start when kept idle for some time, also some time it does not play the audio.
Earlier I used setDataSource(), but this time I am using create().
For reference this is the code I am using:
AssetFileDescriptor afd = _context.getResources().openRawResourceFd(this._soundResource.get(this._toBePlayed));
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
mp.prepareAsync();
I also had the delay problem for start(). I fixed it with mp.seekTo(0) in onPrepared like this:
mp.setOnPreparedListener(new OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mp.seekTo(0);
mp.start();
}
});
I use a FileInputStream. You didn't set the AudioStreamType. Here is how I setup my MediaPlayer.
public void setupMediaPlayer(){
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
fis = new FileInputStream(tempFilePath);
mp.setDataSource(fis.getFD());
mp.prepare();
mp.setOnPreparedListener(this);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
tempFilePath is the path to the file.
I also implement setOnPreparedListener in this class which makes your code a lot more readable in my opinion.
Also Great link to reference: MediaPlayer State Diagram
I think if you would change
mp.prepareAsync();
to
mp.prepare();
and put
mp.start();
after the mp.prepare(); your code will work. Give it a try.