I'm new to Android programming. I followed a mediaplayer tutorial but I'm now trying to make it more advance, the only problem is thats not working as expected.
When the song is finished it should start playing the next song, but doesn't update my seekbar (only updates Max) and it makes my buttons crash my app. I tried many things but can't fix it myself. If i press the next button however (before music track ends) the buttons and seekbar work.
What I'm want is my seekbar to reset for the new song and go again, and i want my buttons to work after the onCompletion next song.
Here my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
btPlay = (ImageButton) findViewById(R.id.btPlay);
btNxt = (ImageButton) findViewById(R.id.btNxt);
btPv = (ImageButton) findViewById(R.id.btPv);
btPlay.setOnClickListener(this);
btNxt.setOnClickListener(this);
btPv.setOnClickListener(this);
sb = (SeekBar) findViewById(R.id.seekBar);
updateSeekBar = new Thread() {
#Override
public void run() {
int TotalDuration = mp.getDuration();
int currentPosition = 0;
while (currentPosition < TotalDuration) try {
sleep(500);
currentPosition = mp.getCurrentPosition();
sb.setProgress(currentPosition);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
if (mp != null) {
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songlist");
position = b.getInt("pos", 0);
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
try {
mp.stop();
mp.reset();
position = (position + 1) % mySongs.size();
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
sb.setMax(mp.getDuration());
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
}
});
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btPlay:
if (mp.isPlaying()) {
mp.pause();
btPlay.setImageResource(R.drawable.play);
} else {
mp.start();
btPlay.setImageResource(R.drawable.pause);
}
break;
case R.id.btNxt:
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
position = (position + 1) % mySongs.size();
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
sb.setMax(mp.getDuration());
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.btPv:
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
position = (position - 1 < 0) ? mySongs.size() - 1 : position - 1;
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
sb.setMax(mp.getDuration());
}
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
Errors (in android Monitor):
java.lang.IllegalStateException
Related
This question already has answers here:
Android Media player play the song x times [closed]
(2 answers)
Closed 3 years ago.
How can I make this simple music player musics repeat for example 5 times or to loop the music for 1, 2, or 3 hour.
This is my code of a simple music player with a music list:
public class Musica extends AppCompatActivity implements Runnable {
private Button pause;
private Button stop;
private SeekBar mseek;
private MediaPlayer mp;
private Thread soundThread;
private Button play;
//list
AdRequest adRequest;
private AdView adView;
ListView listm;
String[] itemname = {
"music 1",
"music 2",
"music 3",
"music 4",
"music 5",
"music 6",
"Lullaby 1",
"Lullaby 2",
"Lullaby 3",
"Lullaby 4",
"Lullaby 5",
};
Integer[] imgid = {
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
};
#Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_musica);
getSupportActionBar().hide();
adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
adView.setVisibility(View.VISIBLE);
}
#Override
public void onAdFailedToLoad(int error) {
adView.setVisibility(View.GONE);
}
});
CustomListAdapterMusic adapter = new CustomListAdapterMusic(this, itemname, imgid);
listm = (ListView) findViewById(R.id.listmusic);
listm.setAdapter(adapter);
listm.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
if (position == 0) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.babyone);
mp.start();
}
if (position == 1) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.babytwo);
mp.start();
}
if (position == 2) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.water);
mp.start();
}
if (position == 3) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.ocean);
mp.start();
}
if (position == 4) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.rain);
mp.start();
}
if (position == 5) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.sm);
mp.start();
}
if (position == 6) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.classica);
mp.start();
}
if (position == 7) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.relax6);
mp.start();
}
if (position == 8) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.twinkle7);
mp.start();
}
if (position == 9) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.ninar11);
mp.start();
}
if (position == 10) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.lullaby9);
mp.start();
}
play = (Button) findViewById(R.id.bplay);
pause = (Button) findViewById(R.id.bpause);
stop = (Button) findViewById(R.id.bstop);
mseek = (SeekBar) findViewById(R.id.seekBar);
setupListeners();
soundThread = new Thread(Musica.this);
soundThread.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
private void setupListeners()
{
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
mp.stop();
}
});
mseek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mp.seekTo(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
#Override
public void run() {
int currentPosition = 0;
int soundTotal = mp.getDuration();
mseek.setMax(soundTotal);
while (mp != null && currentPosition < soundTotal) {
try {
Thread.sleep(300);
currentPosition = mp.getCurrentPosition();
} catch (InterruptedException SoundException) {
return;
} catch (Exception otherException) {
return;
}
mseek.setProgress(currentPosition);
}
}
}
For part one, implement an setOnCompletionListener as already answered here: Android Media player play the song x times
For repeating for for 1 hour, change the counter with the timer implementation:
// get current time in milliseconds + 1 hour
long tRef = System.currentTimeMillis() + 1 * 60 * 60 * 1000L;
// set listener
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
// current time
long tNow = System.currentTimeMillis();
if (tNow < tEnd) {
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
}});
To trigger the looping again, you just need to set the tRef to a new value.
Side note: If you need both functionalities at the same time, then combine them into one listener as you cannot have two listeners on the same media player at the same time.
Alternative solution would be to set mp.setLooping() and make a timer task to turn it off, but I would not recommend it here, as it might require some lifecycle management.
Timer timerObj = new Timer();
TimerTask timerTaskObj = new TimerTask() {
public void run() {
if (mp != null) {
mp.setLooping(false)
}
}
};
timer.schedule(timerTaskObj, 0, 1 * 60 * 60 * 1000L);
I am playing audio files(MP3) by clicking on Item view, and the previous file stop automatically but the problem is that after tapping 3rd item of recyclerview the 1st one does not play sound on its click and the same problem happened on some other clicks of the list. I have added full Adapter class
public class RingToneAdapter extends RecyclerView.Adapter<RingToneAdapter.RingToneViewHolder> {
//removed declared varaible for the sake of post to edit
static final int[] resID = {R.raw.a48, R.raw.funny_hen};
public RingToneAdapter(Context rcntx, List<RingTone_Items> ringtonelist) {
this.rcntx = rcntx;
this.ringtonelist = ringtonelist;
}
#NonNull
#Override
public RingToneViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
view = LayoutInflater.from(rcntx).inflate(R.layout.ringtone_values, viewGroup, false);
RingToneViewHolder ringToneViewHolder = new RingToneViewHolder(view);
return ringToneViewHolder;
}
//playing sounds on recycler view
#Override
public void onBindViewHolder(#NonNull final RingToneViewHolder ringToneViewHolder, final int i) {
final RingTone_Items ringTone_items = ringtonelist.get(i);
ringToneViewHolder.rtv.setText(ringTone_items.getRintonetv());
if (mSelectedItem == i) {
ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_pause_black_24dp);
} else {
ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_play_arrow_black_24dp);
}
ringToneViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.reset();
mp = null;
ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_play_arrow_black_24dp);
}
//Intent it = new Intent(rcntx, ViewPager_Data.class);
Intent it = new Intent(rcntx, AndroidViewPagerExample.class);
it.putExtra("POS",i);
it.putExtra("name",ringTone_items.getRintonetv());
rcntx.startActivity(it);
}
});
ringToneViewHolder.iconplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSelectedItem == i) {
mSelectedItem = -1;
oldpossssssss = i;
} else {
mSelectedItem = i;
}
notifyDataSetChanged();
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.reset();
mp = null;
if (oldpossssssss == i) {
} else {
mp = new MediaPlayer();
mp = MediaPlayer.create(rcntx, resID[i]);
mp.start();
}
} else {
mp = new MediaPlayer();
mp = MediaPlayer.create(rcntx, resID[i]);
mp.start();
}
}
});
}
#Override
public int getItemCount() {
return ringtonelist.size();
}
class RingToneViewHolder extends RecyclerView.ViewHolder {
private TextView rtv, hello, close;
private ImageView iconplay;
public RingToneViewHolder(#NonNull View itemView) {
super(itemView);
rtv = itemView.findViewById(R.id.ringtitle);
iconplay = itemView.findViewById(R.id.playicon);
}
}
How can I manage this smoothly according to click the play media file accordingly. Where is I am doing wrong please help me Thanks.
I would suggest you to use singletone media player in your viewHolders. Firstly, using multiple prepared mediaPlayers are not very memory efficient. Secondly, it will allow you to resolve problems with played sound on the background because one mediaPlayer will play only one audio at a time.
ringToneViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.reset();
mp.release();
mp = null;
ringToneViewHolder.iconplay.setImageResource(R.drawable.ic_play_arrow_black_24dp);
}
//Intent it = new Intent(rcntx, ViewPager_Data.class);
Intent it = new Intent(rcntx, AndroidViewPagerExample.class);
it.putExtra("POS",i);
it.putExtra("name",ringTone_items.getRintonetv());
rcntx.startActivity(it);
}
});
ringToneViewHolder.iconplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSelectedItem == i) {
oldpossssssss = i;
} else {
mSelectedItem = i;
}
notifyDataSetChanged();
if (oldpossssssss == i) {
if(mp != null){
if(mp.mp.isPlaying()){
mp.pause();
} else{
mp.start();
}
}
} else {
createMediaPlayer(i)
}
}
});
Use this method when you create MediaPlayer
private void createMediaPlayer(int i)
{
if (mp!=null)
{
if(mp.isPlaying())
{
mp.stop();
mp.reset();
mp.release();
mp=null;
}
}
mp = new MediaPlayer();
mp = MediaPlayer.create(rcntx, resID[i]);
mp.start();
}
May be its work.
For some reason, the repeat and shuffle buttons are not doing anything.
The buttons previous and next work perfectly.
In my app I am communicating with my service through broadcasts.
mediaPlayer is a public static in my service class and I am importing it in other activity.
public static MediaPlayer mediaPlayer = new MediaPlayer(); (In Service.class)
Activity
This is the code for the buttons
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
updateProgressBar();
}
}
mBtnShuffle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isShuffle){
isShuffle = false;
Toast.makeText(getContext(), "Shuffle is off", Toast.LENGTH_SHORT ).show();
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}else{
isShuffle = true;
Toast.makeText(getContext(), "Shuffle is on", Toast.LENGTH_SHORT).show();
mBtnShuffle.setImageResource(R.drawable.ic_shuffle_on);
isRepeat = false;
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}
}
});
mBtnRepeat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRepeat){
isRepeat = false;
Toast.makeText(getContext(), "Repeat is off", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}else{
isRepeat = true;
Toast.makeText(getContext(), "Repeat is on", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_repeat_on);
isShuffle = false;
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}
}
});
mBtnPlayPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mBtnPlayPause.setImageResource(R.drawable.ic_action_play);
} else {
mediaPlayer.start();
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
mBtnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextSong();
}
});
mBtnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevSong();
}
});
Code in onCompletionListener
#Override
public void onCompletion(MediaPlayer mp) {
if (isRepeat){
//Store current songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}else if(isShuffle){
Random random = new Random();
songIndex = random.nextInt((songList.size() - 1) + 1);
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
//Store random songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}else if (songIndex < songList.size()-1){
mediaPlayer.reset();
nextSong();
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
}else{
songIndex = 0;
tvSongListSize.setText((1) + "/" + songList.size());
//Store random songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}
}
Broadcast receiver
private BroadcastReceiver NewSongBroadCastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
songList = new StorageUtil(getApplicationContext()).getSongs();
songIndex = new StorageUtil(getApplicationContext()).loadSongIndex();
if (songIndex != -1 && songIndex < songList.size()){
activeSong = songList.get(songIndex);
}else{
stopSelf();
}
stopSong();
mediaPlayer.reset();
if (mMediaSessionManager == null) {
try {
initMediaSession();
initMediaPlayer();
} catch (RemoteException e) {
e.printStackTrace();
stopSelf();
}
}
initMediaPlayer();
updateMetaData();
NotificationBuilder(PlaybackStatus.PLAYING);
}
};
The buttons mBtnShuffle and mBtnRepeat do not trigger onCompletion. They are just 2 ordinary buttons that you use to set the flags isShuffle and isRepeat.
onCompletion is triggered when a song has been completed.
So if you want something to happen when the 2 buttons are tapped you should put some code in their listeners.
this is my code, i am almost certain the problem is with the while loop.
public void start(View view) {
int currentPosition = 0;
player = MediaPlayer.create(this, R.raw.sound);
player.start();
int total = player.getDuration();
progressBar.setProgress(0);
progressBar.setMax(player.getDuration());
while (player != null && currentPosition < total) {
// try {
// Thread.sleep(500);
currentPosition = player.getCurrentPosition();
// } catch (InterruptedException e) {
// return;
// } catch (Exception e) {
// return;
// }
progressBar.setProgress(currentPosition);
}
}
public void stop(View view) {
player.stop();
}
whether or not i have the sleep intervals my result is the same; the sound starts playing but i cant stop it, and the progress bar doesn't move.
i would appreciate some help
I used this code in my app for using seekBar with MediaPlayer. It implements Runnable to keep the seekBar updating as the music plays. Take a look at the code:
public class MainActivity extends Activity implements Runnable,
OnClickListener, OnSeekBarChangeListener {
private SeekBar seekBar;
private Button startMedia;
private Button stopMedia;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (Button) findViewById(R.id.button1);
stopMedia = (Button) findViewById(R.id.button2);
startMedia.setOnClickListener(this);
stopMedia.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setEnabled(false);
}
public void run() {
int currentPosition = mp.getCurrentPosition();
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 = MediaPlayer.create(getApplicationContext(), R.raw.song2);
seekBar.setEnabled(true);
}
if (mp.isPlaying()) {
mp.pause();
startMedia.setText("play");
} else {
mp.start();
startMedia.setText("pause");
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
}
if (v.equals(stopMedia) && mp != null) {
if (mp.isPlaying() || mp.getDuration() > 0) {
mp.stop();
mp = null;
startMedia.setText("play");
seekBar.setProgress(0);
}
}
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
try {
if (mp.isPlaying() || mp != null) {
if (fromUser)
mp.seekTo(progress);
} else if (mp == null) {
Toast.makeText(getApplicationContext(), "Media is not running",
Toast.LENGTH_SHORT).show();
seekBar.setProgress(0);
}
} catch (Exception e) {
Log.e("seek bar", "" + e);
seekBar.setEnabled(false);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
Thanks to: Sridhar Kulkarni
i want to reset the seekbar to start position once the music is played and button showing "play"...The problem with the code is that the seek bar stays at the end position after its done playing the music and button still shows "pause"...
Here's my code
ekdanta.java
public class ekdanta extends AppCompatActivity implements Runnable, View.OnClickListener,SeekBar.OnSeekBarChangeListener {
TextView tv4;
Button b9, b10,but19;
int count = 0;
MediaPlayer play;
SeekBar seek_bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ekdanta);
tv4 = (TextView) findViewById(R.id.textView9);
tv4.setTextSize((float)21.5);
tv4.setText(Html.fromHtml(getString(R.string.thirteen)));
b9 = (Button) findViewById(R.id.b9);
b10 = (Button) findViewById(R.id.b10);
seek_bar = (SeekBar) findViewById(R.id.seekBar);
seek_bar.setOnSeekBarChangeListener(this);
seek_bar.setEnabled(false);
but19 = (Button) findViewById(R.id.button19);
but19.setOnClickListener(this);
}
public void run() {
int currentPosition= play.getCurrentPosition();
int total = play.getDuration();
while (play!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= play.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seek_bar.setProgress(currentPosition);
}
}
public void onClick(View v) {
if (v.equals(but19)) {
if (play == null) {
play = MediaPlayer.create(getApplicationContext(), R.raw.ekadanta);
seek_bar.setEnabled(true);
}
if (play.isPlaying()) {
play.pause();
but19.setText("Play");
} else {
play.start();
but19.setText("Pause");
seek_bar.setMax(play.getDuration());
new Thread(this).start();
}
}
}
#Override
protected void onPause() {
if(play!=null){
play.stop();
}
super.onPause();
}
public void increase(View inc) {
count++;
if (count == 1) {
tv4.setTextSize(25);
} else if (count == 2) {
tv4.setTextSize(30);
} else if (count >= 3) {
count = 3;
tv4.setTextSize(40);
}
}
public void decrease(View dec) {
count--;
if (count <= 0) {
tv4.setTextSize((float)21.5);
count = 0;
}
if (count == 1) {
tv4.setTextSize(25);
} else if (count == 2) {
tv4.setTextSize(30);
} else if (count == 3) {
tv4.setTextSize(40);
}
}
#Override
public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
try{
if(play.isPlaying()||play!=null){
if (fromUser)
play.seekTo(progress);
}
else if(play==null){
Toast.makeText(getApplicationContext(),"First Play", Toast.LENGTH_SHORT).show();
seek_bar.setProgress(0);
}
}
catch(Exception e){
Log.e("seek bar",""+e);
seek_bar.setEnabled(false);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
MediaPlayer has got onCompletionListener(), you can use that. Your code with handlers is not too reliable, it would be better to refactor it.
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// do whatever you want
}
});