why I don't call url from Http request when onCompletion - android

I want to call new url after My custom Music Player complete played but It do not call and send new url to my player. Help me please thank you.
public void onCompletion(MediaPlayer arg0)
{
playing = "Stopped";
new Thread(new Runnable()
{
#Override
public void run()
{
now++;
try
{
onCompleteURL = new URL(musicUrl[now]);
onCompleteURLConnection = onCompleteURL.openConnection();
onCompleteReadIn = new BufferedReader(new InputStreamReader(onCompleteURLConnection.getInputStream()));
onCompleteUrl = onCompleteReadIn.readLine();
onCompleteReadIn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
waitingGetNextDataHandler1.sendEmptyMessage(0);
}
}).start();
waitingGetNextDataHandler1 = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
try
{
/*mMediaPlayer.reset();
sendHTTPRequest();
startPlayer();*/
mMediaPlayer.reset();
mMediaPlayer.setDataSource(onCompleteUrl);
mMediaPlayer.prepare();
mMediaPlayer.start();
playing = "Started";
running = mMediaPlayer.isPlaying();
playPauseImageButton.setBackgroundResource(R.drawable.pause_button);
}
catch (Exception e)
{
e.printStackTrace();
}
if(mMediaPlayer != null)
{
if(mMediaPlayer.isPlaying() == true)
{
playPauseImageButton.setBackgroundResource(R.drawable.pause_button);
}
else
{
playPauseImageButton.setBackgroundResource(R.drawable.play_button);
}
setValue();
}
}
};
}

Related

Loading audio files in android application

I have a server with a number of audio files which from time to time more files are added. I want those files to be loaded in my android app so they an be played. How would I go about doing this?
I am using native android
Simple AudioHandler class to play, pause, resume audio etc
public class AudioHandler {
private SimpleExoPlayer simpleExoPlayer;
//Start Playing
public void playAudio(String URL) {
try {
if(simpleExoPlayer == null) {
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(mContext),
new DefaultTrackSelector(),
new DefaultLoadControl());
}
// Preparing from url
Uri uri = Uri.parse(URL);
MediaSource mediaSource = buildMediaSource(uri);
simpleExoPlayer.prepare(mediaSource, true, false);
simpleExoPlayer.setPlayWhenReady(true);
simpleExoPlayer.addListener(new Player.DefaultEventListener() {
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
super.onPlayerStateChanged(playWhenReady, playbackState);
switch(playbackState) {
case Player.STATE_READY:
// Update UI -- Audio has start playing
break;
case Player.STATE_ENDED:
ReleaseMediaPlayer();
// Update UI -- Audio has ended
break;
default:
break;
}
}
#Override
public void onPlayerError(ExoPlaybackException error) {
super.onPlayerError(error);
ReleaseMediaPlayer();
// Update UI -- error
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void pauseAudio(){
try{
if (simpleExoPlayer != null) {
simpleExoPlayer.setPlayWhenReady(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void resumeAudioa(){
try{
if (simpleExoPlayer != null) {
simpleExoPlayer.setPlayWhenReady(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void ReleaseMediaPlayer(){
try{
if (simpleExoPlayer != null) {
simpleExoPlayer.release();
simpleExoPlayer = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-audios")).
createMediaSource(uri);
}
// Constructor and other methods...
}

Get Video Play Time android from videoplayer

I write this function to get video time . This functions works fine but when I backpress then it throws exception .
public void videoTimer ()
{
try
{
running = true;
final int duration = mVideoView.getDuration();
thread = new Thread(new Runnable()
{
public void run()
{
do
{
tvVideoTimer.post(new Runnable()
{
public void run()
{
int time = (duration - mVideoView.getCurrentPosition()) / 1000;
tvVideoTimer.setText(getTimeString(mVideoView.getCurrentPosition()));
}
});
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if (!running) break;
}
while (mVideoView.getCurrentPosition() <= duration);
}
});
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}

Resolve preparedAsync() MediaPlayer

I writting a music application play music with url get from website. In code of me, i using method prepared() and app work. But method prepared() block UI this makes me uncomfortable... I want using method prepareAsync()... But i don't know use it :(. I'm newbie with Android Programing. Please edit my code...!
I speak English bad. Sorry for the inconvenience
Code here :
Function playSong()
private void playSong(String urlData) {
btnPlay = (ImageButton) findViewById(R.id.btnPlay);
seekBar = (SeekBar) findViewById(R.id.seekBar);
btnPlay.setOnClickListener(this);
seekBar.setMax(99);
seekBar.setOnTouchListener(this);
mPlay = new MediaPlayer();
mPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlay.setOnBufferingUpdateListener(this);
mPlay.setOnCompletionListener(this);
try {
mPlay.setDataSource(urlData);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onPrepared(MediaPlayer mp) {
if (mPlay != null) {
mPlay.start();
btnPlay.setImageResource(R.drawable.pause_icon);
} else {
mPlay.pause();
btnPlay.setImageResource(R.drawable.play);
}
mPlay.start();
primaryUpdateSeekBar();
}
Function onClick use play
#Override
public void onClick(View view) {
if (view.getId() == R.id.btnPlay) {
try {
mPlay.prepare();
} catch (Exception e) {
e.printStackTrace();
}
LenghData = mPlay.getDuration();
if (!mPlay.isPlaying()) {
mPlay.start();
btnPlay.setImageResource(R.drawable.pause_icon);
} else {
mPlay.pause();
btnPlay.setImageResource(R.drawable.play);
}
primaryUpdateSeekBar();
}
}
Functions of Seekbar
private void primaryUpdateSeekBar() {
seekBar.setProgress((int) (((float) mPlay.getCurrentPosition() / LenghData) * 100));
if (mPlay.isPlaying()) {
Runnable notification = new Runnable() {
#Override
public void run() {
long totalDuration = mPlay.getDuration();
long currentDuration = mPlay.getCurrentPosition();
tvTotalTime.setText(Utils.getTimeString(totalDuration));
tvCurrentTime.setText(Utils.getTimeString(currentDuration));
primaryUpdateSeekBar();
}
};
handler.postDelayed(notification, 1000);
}
}
#Override
public void onBackPressed() {
if (mPlay.isPlaying()) {
mPlay.stop();
}
super.onBackPressed();
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
seekBar.setSecondaryProgress(percent);
}
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
btnPlay.setImageResource(R.drawable.play);
seekBar.setProgress(0);
seekBar.setSecondaryProgress(0);
tvTotalTime.setText("00:00");
tvCurrentTime.setText("");
try {
playSong(ifChangeCheck);
mPlay.prepareAsync();
mPlay.setOnPreparedListener(this);
mPlay.setLooping(true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
if (view.getId() == R.id.seekBar) {
SeekBar seekbar = (SeekBar) view;
int playPositioninMiliseconds = (LenghData / 100)
* seekbar.getProgress();
mPlay.seekTo(playPositioninMiliseconds);
}
return false;
}
Final
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (group.getCheckedRadioButtonId()) {
case R.id.rad32Kb:
String aftercheck32 = "";
int checkLinkIsPlay32 = urlDataSource.lastIndexOf("/") - 3;
String subString32 = urlDataSource.substring(checkLinkIsPlay32);
if (subString32.contains("128")) {
aftercheck32 = urlDataSource.replace("/128/", "/32/").replace(
".mp3", ".m4a");
} else if (subString32.contains("320")) {
aftercheck32 = urlDataSource.replace("/320/", "/32/").replace(
".mp3", ".m4a");
} else if (subString32.contains("m4a")) {
aftercheck32 = urlDataSource.replace("/m4a/", "/32/");
}
ifChangeCheck = aftercheck32;
try {
if (mPlay != null) {
mPlay.stop();
mPlay.reset();
}
playSong(ifChangeCheck);
mPlay.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
}
break;
case R.id.rad128Kb:
try {
if (mPlay != null) {
ifChangeCheck = urlDataSource;
mPlay.stop();
mPlay.reset();
}
playSong(urlDataSource);
mPlay.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
}
break;
case R.id.rad320Kb:
String aftercheck320 = "";
int checkLinkIsPlay320 = urlDataSource.lastIndexOf("/") - 3;
String subLink320 = urlDataSource.substring(checkLinkIsPlay320);
if (subLink320.contains("/32")) {
aftercheck320 = urlDataSource.replace("/32/", "/320/").replace(
".m4a", ".mp3");
} else if (subLink320.contains("128")) {
aftercheck320 = urlDataSource.replace("/128/", "/320/");
} else if (subLink320.contains("m4a")) {
aftercheck320 = urlDataSource.replace("/m4a/", "/320/")
.replace(".m4a", ".mp3");
}
ifChangeCheck = aftercheck320;
try {
if (mPlay != null) {
mPlay.stop();
mPlay.reset();
}
playSong(ifChangeCheck);
mPlay.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
}
break;
case R.id.rad500:
String aftercheck500 = "";
int checkLinkIsPlay500 = urlDataSource.lastIndexOf("/") - 3;
String subLink500 = urlDataSource.substring(checkLinkIsPlay500);
if (subLink500.contains("/32")) {
aftercheck500 = urlDataSource.replace("/32/", "/m4a/");
} else if (subLink500.contains("128")) {
aftercheck500 = urlDataSource.replace("/128/", "/m4a/")
.replace(".mp3", ".m4a");
} else if (subLink500.contains("320")) {
aftercheck500 = urlDataSource.replace("/320/", "/m4a/")
.replace(".mp3", ".m4a");
}
ifChangeCheck = aftercheck500;
try {
if (mPlay != null) {
mPlay.stop();
mPlay.reset();
}
playSong(ifChangeCheck);
mPlay.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
Above is all code in class PlayMusicActivity of me... And thanks for helping me
Call the prepareAsync() method from the playsong method, as it will starting buffering the audio immediately. When enough audio has buffered the onPrepared method will be called and audio will play
private void playSong(String urlData) {
btnPlay = (ImageButton) findViewById(R.id.btnPlay);
seekBar = (SeekBar) findViewById(R.id.seekBar);
btnPlay.setOnClickListener(this);
seekBar.setMax(99);
seekBar.setOnTouchListener(this);
mPlay = new MediaPlayer();
mPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlay.setOnBufferingUpdateListener(this);
mPlay.setOnCompletionListener(this);
try {
mPlay.setDataSource(urlData);
mPlay.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

How to get duration of video when it is recording-Android

I'm facing with a problem. I want to get duration of video when i 'm recording it.
I can get duration of video when it's finish by code
MediaPlayer mp = MediaPlayer.create(mContext, Uri.parse(video));
if(mp == null)
return -1;
int duration = mp.getDuration();
mp.release();
But i want to get duration everytime when i record video to update to progressbar.
Hope your reply!
private class PlaybackObserver extends Thread {
public void run() {
currentPosition = 0;
try {
while (!killObserverThread) {
Thread.sleep(1000);
currentPosition = (int) mediaPlayer.getCurrentPosition();
runOnUiThread(new Runnable() {
public void run() {
yourProgressBar.setProgress(currentPosition);
}
});
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
protected void onDestroy() {
super.onDestroy();
killObserverThread = false;
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (task == null || !task.isAlive()) {
task = new PlaybackObserver();
task.start();
}
startPlaying();
}
add a private class for your UiThread to update your seekbar/progressbar. Then start it from onResume()

problem in using seekbar for android media player

in my app i am using media player and i want to place a seek bar there. In my code the song is playing but the seek bar is not moving. Following is the part of my code...
sv = (SurfaceView)findViewById(R.id.surfaceView1);
sv.postDelayed(runnable,200);
final Runnable runnable = new Runnable()
{
#Override
public void run()
{
if (mediaPlayer != null)
{
if (isPlaying)
{
try
{
int currentPosition = mediaPlayer.getCurrentPosition();
audioProgress.setProgress((int) ((currentPosition / (float) mediaPlayer.getDuration()) * 100f));
} catch (Exception e) {
e.printStackTrace();
}
sv.postDelayed(runnable, 150);
}
}
}
};
Please anyone help me in clearing the errors...
Make these changes to your code:
sv = (SurfaceView)findViewById(R.id.surfaceView1);
audioProgress.setMax(mediaPlayer.getDuration()); // this is one change
sv.postDelayed(runnable,200);
final Runnable runnable = new Runnable()
{
#Override
public void run()
{
if (mediaPlayer != null)
{
if (isPlaying)
{
try
{
int currentPosition = mediaPlayer.getCurrentPosition();
// the other change:
audioProgress.setProgress(currentPosition);
} catch (Exception e) {
e.printStackTrace();
}
sv.postDelayed(runnable, 150);
}
}
}
};

Categories

Resources