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
}
Related
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()
i've created a playlist based on Media Player class. The thing is that it succeed in reading some mp3 url and it doesn't for another ones.
For example here is a directory of mp3 i've uploaded but no one is working:
http://rajaapp12.0fees.us/gbv3/
in the other hands an mp3 which work:
http://mp3.mp3zik.com/music/Music-Rap/Casa%20Verde%202009/track%20(14).mp3
I don't think there is a problem with the code since it works already witch some url, but here is the most iportant part of it :
public void beginLrcPlay(){
killMediaPlayer();
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(songEnCours.getSource_128());
mPlayer.setLooping(true);
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
if (mTimer == null) {
mTimer = new Timer();
mTask = new LrcTask();
mTimer.scheduleAtFixedRate(mTask, 0, mPalyTimerDuration);
}
}
});
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//fin musique ajouter action !
stopLrcPlay();
chantSuivant();
btnPlay.setImageResource(R.drawable.bg_selector_btn_play);
}
});
mPlayer.setOnBufferingUpdateListener(onBufferingUpdateListener);
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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)
Don't get the wrong sound when playing, when I enter text in English then I'll get everything and the result gives sootvetstvuyushaya word,
but when I change the url to: http://translate.google.com/translate_tts?tl=ru&q=привет and open computer browser then I'm fine but when I enter the url in the source code that gives me no intelligible speech, give that to me to do
This is my code:
public void onClick(View v) {
MediaPlayer player = new MediaPlayer();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://translate.google.com/translate_tts?tl=ru&q=привет");
player.prepare();
player.start();
} catch (Exception e)
{
Toast.makeText(this, "speaking error!!", Toast.LENGTH_LONG).show();
}
}
Thread x;
MediaPlayer mediaPlayer;
x=new Thread(){
public void run(){
try{
url1="http://www.translate.google.com/translate_tts?ie=UTF-8&q="this is word which is speech"%0A&tl="this is language"&prev=input";
mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url1);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
} catch (IllegalArgumentException e) {
mediaPlayer.reset();
} catch (IllegalStateException e) {
mediaPlayer.reset();
} catch (IOException e) {
mediaPlayer.reset();
}
finally{
// x.suspend();
}
}
};
then you can call like this :
x.start();
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.