How to control the audio using Android Media Player seek bar? - android

I am able to use the android media player to play audio files. I can display the progress bar and attached 2 buttons to pause and play the audio track. My problem is I cannot control the track using the progress bar. I want to playing audio track to update depending of where I drag the progress bar to.
Below is my code
public class MyMediaPlayerView extends AppCompatActivity implements View.OnClickListener {
/**
* var for controller
* restore timestamp today 12:11
*/
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
TextView text_shown;
Handler seekHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_webview);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getInit();
seekUpdation();
}
Runnable run = new Runnable() {
#Override public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
onProgressChanged(seek_bar, seek_bar.getProgress());
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button: text_shown.setText("Playing...");
player.start();
break; case R.id.pause_button: player.pause();
text_shown.setText("Paused...");
}
}
public void onProgressChanged(SeekBar seek_Bar, int progress,
boolean fromUser) {
player.seekTo(progress);
}
}

Im doing it like this
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser)
player.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

u forget to add listener
put this in your onCreate() :
seek_bar = (SeekBar)findViewById(R.id.yourseekbarhere);
seek_bar.setOnSeekBarChangeListener(this);
Should help u out

Related

MediaPlayer stops audio sound

When I add the seekTo function inside onProgressChanged a voice interrupt happens after every seekbar jump. How can I prevent this?
final SeekBar seekbar2 = findViewById(R.id.ScrubSeekBar);
seekbar2.setMax(mediaPlayer.getDuration());
seekbar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
seekbar2.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
}

Stop button is not working.play and pause is working but as soon as I click on the stop button play and pause button also stop playing

I am a beginner and I have created a simple media player but the problem is that the stop button is not working. Play and pause is working good but the problem is is only with the stop button. After tapping on the stop button the music stops but play and pause stop working.
Here is the code
public class MainActivity extends AppCompatActivity {
MediaPlayer mymediaplayer;
AudioManager myaudiomanager;
public void playme(View view) {
mymediaplayer.start();
}
public void pauseme(View view) {
mymediaplayer.pause();
}
public void stopme(View view) {
mymediaplayer.stop();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mymediaplayer=MediaPlayer.create(this,R.raw.music);
myaudiomanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
int max=myaudiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int progress=myaudiomanager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volumerocker=(SeekBar)findViewById(R.id.volume);
volumerocker.setMax(max);
volumerocker.setProgress(progress);
volumerocker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
myaudiomanager.setStreamVolume(AudioManager.STREAM_MUSIC,i,0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}

Created simple media player but after clicking each button five or six time it stop playing

I have created a simple media player. After clicking each button five or six times, it stops playing sound. But the app is not crashing and not showing any error. The app is playing sound without any problem at the first time. Plus, in my function where I have created onClick method, can I apply the stop or pause function to stop the previous playing sound?
Here is the code:
public class MainActivity extends AppCompatActivity {
MediaPlayer mymediaplayer;
AudioManager myaudiomanager;
public void playone(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.one);
mymediaplayer.start();
}
public void playtwo(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.two);
mymediaplayer.start();
}
public void playthree(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.three);
mymediaplayer.start();
}
public void playfour(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.four);
mymediaplayer.start();
}
public void playfive(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.five);
mymediaplayer.start();
}
public void playsix(View view)
{
mymediaplayer=MediaPlayer.create(this,R.raw.six);
mymediaplayer.start();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myaudiomanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
int max=myaudiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int progress=myaudiomanager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volume=(SeekBar)findViewById(R.id.seekBar);
volume.setMax(max);
volume.setProgress(progress);
volume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
myaudiomanager.setStreamVolume(AudioManager.STREAM_MUSIC,i,0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
You should release media player before starting to play new item. I did change playone as example:
public void playone(View view)
{
releaseMediaPlayer(mymediaplayer);//release previous player if there are any
mymediaplayer=MediaPlayer.create(this,R.raw.one);
mymediaplayer.start();
}
void releaseMediaPlayer(MediaPlayer mPlayer){
if(mPlayer!=null)
{
mPlayer.release();
}
}

Using seekbar with music in android

I am actually creating an app with a button, a seekbar and an an image. The button is used to Play/Pause the music. the music played smooth before adding the seekbar. But, after writing the code for seekbar to progress with the position of music, the music seems to be a bit choppy.
Also, i want to reset the position of seekbar to start and change the text on button to "Play" after the music is finished playing.
Can any one tell me why the music is choppy after implementing seekbar?
Also, suggest me how to deal after the music is finished playing.
The coded i used is below:
public class MainActivity extends ActionBarActivity {
private MediaPlayer song;
private SeekBar seekbar;
private final Handler handler = new Handler();
private final Runnable updatePositionRunnable = new Runnable() {
public void run() {
updatePosition();
}
};
//private boolean isPlaying = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
seekbar = (SeekBar)findViewById(R.id.musicSeekBar);
song = MediaPlayer.create( MainActivity.this, R.raw.xyz);
seekbar.setMax(song.getDuration());
final Button playButton = (Button)findViewById(R.id.playButton);
playButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(song.isPlaying()){
playButton.setText("Play");
song.pause();
//isPlaying = false;
handler.removeCallbacks(updatePositionRunnable);
}
else{
playButton.setText("Pause");
song.start();
//isPlaying = true;
updatePosition();
}
}
});
song.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playButton.setText("Play");
//seekbar.setProgress(0);
//mp.stop();
}
});
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
song.seekTo(progress);
}
});
private void updatePosition(){
handler.removeCallbacks(updatePositionRunnable);
seekbar.setProgress(song.getCurrentPosition());
handler.postDelayed(updatePositionRunnable, 2000);
}
#Override
public void onBackPressed() {
super.onBackPressed();
song.stop();
song.release();
}
When you update the progress on the seekbar from your updatePosition method every 2 seconds, the onProgressChanged method in your listener will be called, which will set the position of the playback and cause a slight blip. You might expect onProgressChanged to be called only when the user interacts with the SeekBar but unfortunately that isn't the case.
I'm not aware of a clean solution to this. What you can do is set a boolean flag in your code immediately before calling seekbar.setProgress that indicates the next onProgressChanged callback was not caused by the user, so you can ignore it. You should use an AtomicBoolean for this purpose because the events may come from different threads.
this worked for me:
sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
boolean userTouch;
#Override
public void onStopTrackingTouch(SeekBar arg0) {
userTouch = false;
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
userTouch = true;
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
if(mPlayer.isPlaying() && arg2)
mPlayer.seekTo(arg1);
}
});

Android SeekBar to control MediaPlayer progress

I have a SeekBar, it displays correctly MediaPlayer progress. However, I have troubles with seeking - if I seek scroll box somewhere it just returns on the position where audio file is playing.
public class EntityPageActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
private SeekBar seekBar;
private Button startMedia;
private Button pauseMedia;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entity_page);
AudioControl();
}
public void AudioControl(){
seekBar = (SeekBar) findViewById(R.id.seekBar);
startMedia = (Button) findViewById(R.id.play);
pauseMedia = (Button) findViewById(R.id.pause);
startMedia.setOnClickListener(this);
pauseMedia.setOnClickListener(this);
}
public void run() {
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBar.setProgress(currentPosition);
}
}
public void onClick(View v) {
if (v.equals(startMedia)) {
if (mp != null && mp.isPlaying()) return;
if(seekBar.getProgress() > 0) {
mp.start();
return;
}
mp = MediaPlayer.create(EntityPageActivity.this, R.raw.gl1);
mp.start();
seekBar.setProgress(0);
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
if (v.equals(pauseMedia) && mp!=null) {
mp.pause();
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser) mp.seekTo(progress);
}
}
Thus, onProgressChanged isn't enough to allow SeekBar to control MediaPlayer?
What should I add/correct to allow seeking?
Thanks
UPDATE
I found out that onProgressChanged is never invoked by some reason. However I can't figure out why
The problem is seekBar.setOnSeekBarChangeListener(this); So put it in Your AudioControl() Method.....
You dont set oncheckchangedlistner to your seekbar....
Try with the following code:
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser){
mp.seekTo(progress);
seekbar.setProgress(progress);}
}
You have to include the seekbar methods inside the onCreate. I had the same problem, and it worked after that.
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
mediaPlayer.seekTo(progress);
seekBar.setProgress(progress);
}
Try this code in onCreate() method and run the project.I hope this is working.
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mediaPlayer.seekTo(i);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
if you want to resume media player to specific position from seekbar change progress, I recommend you to do that in onStopTrackingTouch() method. that method will notify that the user has finished a touch gesture, then set media player position from seekbar's current position.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.seekTo(seekBar.getProgress());
}
I am using this simple code to do the trick.
package in.bhartisoftwares.amit.allamitapps;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class audioControllerTwo extends AppCompatActivity {
MediaPlayer mPlayer;
SeekBar audioTraverse;
here I have defined My SeekBar and MediaPlayer in public class. Then on OnCreate Method I have code like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_controller_two);
mPlayer = MediaPlayer.create(this, R.raw.song);
Here I am creating mPlayer variable, that finds my audio file called "song" which is present in "raw" folder.
Then I grabbed my SeekBar called "audioTraverse" and assigned it to "audioTraverse" variable and grabbing the "Duration" of audio file and assigning that duration to my "audioTraverse" SeekBar as follows:
audioTraverse = (SeekBar) findViewById(R.id.audioTraverse);
audioTraverse.setMax(mPlayer.getDuration());
Now I am running a timer that will run after every second and it will update the SeekBar position accordingly. See the code below:
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
audioTraverse.setProgress(mPlayer.getCurrentPosition());
}
},0, 1000); // here 0 represents first time this timer will run... In this case it will run after 0 seconds after activity is launched
// And 1000 represents the time interval afer which this thread i.e., "run()" will execute. In this case, it will execute after every 1 second
Now I am adding "SeekBarChangeListener", this will look for any changes on SeekBar and in "OnProgressChange()" method I am using ".seekTo()" method to go to a particular duration in audio file. Code is as follows:
audioTraverse.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("Tranverse Change: ", Integer.toString(i));
mPlayer.seekTo(i);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}

Categories

Resources