Android Playing MediaPlayer in SeparateThread - android

I have a Mp3Link need to play it on my Device in a Separate Thread
I have tried this,But when i execute my code ,I'm not able to play the Music,Could any one look at My Code,What Went Wrong?
Here My Code:
Thread trd = new Thread(new Runnable(){
public void run(){
//code to do the HTTP request
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(mp3Link);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
Log.d("Mediaplyer>>>>>>>>", "Mediaplyer>>>>>>>>");
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
}
});
trd.start();

How about put
mediaPlayer.prepareAsync();
at the end of run() function ?
Thread trd = new Thread(new Runnable(){
public void run(){
//code to do the HTTP request
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(mp3Link);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// mediaPlayer.prepareAsync(); // <== Marked
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
Log.d("Mediaplyer>>>>>>>>", "Mediaplyer>>>>>>>>");
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
mediaPlayer.prepareAsync(); // <== Add
}
});
trd.start();
Hope this helps.

Related

android Media player not reading some mp3 url

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();
}
}

How to Play Web Address URL to Media Player in android?

My URL contains number medial related URLs which we have to play in media player. When i am passing my any media url with extension .mp3 or .mp4. it plays but whn i am passing any url that holds list of URLs then it is not playing in medi payer.
Please tell me how can i resolve this issue ?
this will help you:
private void PlayOnlineUrl() {
String url = "http://www.gaana.mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
}

Android mediaplayer not progress seeking completely

Am trying to play a .wav file using media-player. Actually, I had done so far shown below. It's playing an updating the progress correctly but the problem media player is not seeking completely. Media-player stops giving out progress before the total duration of the file and stops updating progress. I have also implemented OnComplete listener also am not getting the callback. Here is my code can anyone spot me with where am missing.
public void playAudioFile() {
try {
mMediaPlayer.reset();
setMediaPlayerSource();
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
Graphbar.setProgress(mMediaPlayer.getDuration());
}
});
bPlay.setBackgroundResource(R.drawable.pause_selector);
new Thread(new Runnable() {
#Override
public void run() {
System.out.println("Playing");
updateProgressBar();
}
}).start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateProgressBar() {
Graphbar.setProgress(0);
Graphbar.setMax(mMediaPlayer.getDuration());
mHandler.postDelayed(mUpdateTimeTask, 0);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if(mMediaPlayer.isPlaying()){
long totalDuration = Graphbar.getMax();
long currentDuration =mMediaPlayer.getCurrentPosition();
String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
tvTimeRight.setText(TotalDur);
tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration));
System.out.println(currentDuration+"/"+totalDuration);
Graphbar.setProgress((int)currentDuration);
mHandler.postDelayed(this, 1);
}
}
};
private void setMediaPlayerSource() {
String audioFile = getFilename();
try {
mMediaPlayer.setDataSource(audioFile);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
for total duration use mediaplayer.getduration. Remove unnecessary code from the runnable handler , because it will create load to ur seek bar . Because at every second u are calculating something and updating layout based on that calculation .
I would suggest you to use chronometer view of android.
public void playAudioFile() {
try {
mMediaPlayer.reset();
setMediaPlayerSource();
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
Graphbar.setProgress(mMediaPlayer.getDuration());
}
});
bPlay.setBackgroundResource(R.drawable.pause_selector);
//set this only for once , because for one song total duration is always constant.
updateProgressBar();
}
public void updateProgressBar() {
Graphbar.setProgress(0);
Graphbar.setMax(mMediaPlayer.getDuration());
mHandler.postDelayed(mUpdateTimeTask, 0);
long totalDuration = mMediaPlayer.getDuration();
String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
tvTimeRight.setText(TotalDur);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if(mMediaPlayer.isPlaying()){
//long totalDuration = mMediaPlayer.getDuration();=> no need of calculating total time and updating it to seekbar at every second
long currentDuration =mMediaPlayer.getCurrentPosition();
tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration));
System.out.println(currentDuration+"/"+totalDuration);
Graphbar.setProgress((int)currentDuration);
mHandler.postDelayed(this, 1);
}
}
};
private void setMediaPlayerSource() {
String audioFile = getFilename();
try {
mMediaPlayer.setDataSource(audioFile);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Mediaplayer stream online MP3 file

I have an Android app that streams an MP3 file and plays this file in player,
but the problem is mediaPlayer.prepare(); takes a long time buffering and the app freezes
so I tried to use prepareAsync();, but with this function I can't make the player play the next file.
It just plays a single file online; if I need to play another file I have to close and restart the activity when play ends. This is my code:
public void playMp3(String _link)
{
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
Progressbar.setVisibility(View.INVISIBLE);
play.setVisibility(View.GONE);
stop.setVisibility(View.VISIBLE);
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
}
updateProgressBar();
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.reset();
songProgressBar.setProgress(0);
songProgressBar.setSecondaryProgress(0);
play.setVisibility(View.VISIBLE);
stop.setVisibility(View.GONE);
link = "http://server11.mp3quran.net/hawashi/002.mp3";
playMp3(link);
}
});
mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Toast.makeText(getApplicationContext(), "n" + percent, Toast.LENGTH_LONG).show();
songProgressBar.setSecondaryProgress(percent);
if(percent==100)
{
Progressbar.setVisibility(View.INVISIBLE);
}else if(percent > songProgressBar.getProgress())
{
Progressbar.setVisibility(View.INVISIBLE);
}else
{
Progressbar.setVisibility(View.VISIBLE);
}
}
});
mediaPlayer.reset();
Progressbar.setVisibility(View.VISIBLE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(_link);
//mediaPlayer.prepare(); // might take long! (for buffering, etc) //##
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block///
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
Actually I don't know where is error in your code but I will explain how I did that in my app
public void playMp3(String _link){
mediaPlayer.reset();
Progressbar.setVisibility(View.VISIBLE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(_link);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnPreparedListener(this);
//mediaPlayer.prepare(); // might take long! (for buffering, etc) //##
mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block///
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Then implements OnCompletionListener, OnPreparedListener and OnBufferingUpdateListener in your class
public class PlayerActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnBufferingUpdateListener{
.
.
.
and implements all methodes
public void onPrepared(MediaPlayer mediaplayer) {
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
Progressbar.setVisibility(View.INVISIBLE);
play.setVisibility(View.GONE);
stop.setVisibility(View.VISIBLE);
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
}
updateProgressBar();
}
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
//mediaPlayer.reset();
songProgressBar.setProgress(0);
songProgressBar.setSecondaryProgress(0);
play.setVisibility(View.VISIBLE);
stop.setVisibility(View.GONE);
link = "http://server11.mp3quran.net/hawashi/002.mp3";
playMp3(link);
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
songProgressBar.setSecondaryProgress(percent);
if(percent==100)
{
Progressbar.setVisibility(View.INVISIBLE);
}else if(percent > songProgressBar.getProgress())
{
Progressbar.setVisibility(View.INVISIBLE);
}else
{
Progressbar.setVisibility(View.VISIBLE);
}
}
I wish this helped you.

Media player not working in Android version >=4.0

I am developing a simple radio streaming application to play radio using a URL. This application is working in all the versions except for V>=4.0
Do anyone have any Idea over this.
initializeUIElements();
StartPlaying();
private void initializeUIElements() {
buttonPlay = (ImageView) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
playSeekBar=(ProgressBar)findViewById(R.id.progressBar1);
}
private void startPlaying() {
initializeMediaPlayer();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
Log.i("on prepared", "on prepared");
mp.start();
}
});
player.setOnErrorListener(new OnErrorListener(){
public boolean onError(MediaPlayer arg0, int arg1,
int arg2) {
Toast.makeText(getApplicationContext(),"An error happened while preparing radio",Toast.LENGTH_LONG).show();
player.reset();
initializeMediaPlayer();
return false;
}
});
private void initializeMediaPlayer() {
player = new MediaPlayer();
player.reset();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://***************");
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.i("percent", ""+percent);
if( (percent!=0)||(percent==100) )
{
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay.setVisibility(View.VISIBLE);
}
}
});
In Android version 4.0, It is giving an error as:
11-23 13:06:37.329: E/MediaPlayer(4011): Error (1,-2147483648)
and Player.bufferingUpdateListener() is not called. Here It is showing just that playseekbar revolving and revolving.. Please help on this.
It can be a problem with your particular device. Try running the same on 4.0 emulator.

Categories

Resources