I can not update the screen components - android

I have a list of players (ListView). Each item is have two buttons "start" and "stop". When I click on "start" color "Start" button change red and begins to move seekbar. When click on the "stop" button color "start" start is black and seekbar progress becomes zero. when the song ends with the color button "start" also becomes black and seekbar progress becomes zero.
holder.start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
releaseMP();
startPlayProgressUpdater(holder.seekBar, holder.start);
try {
mediaPlayer = new MediaPlayer();
global_position = position;
mediaPlayer.setDataSource(recordBeans.get(global_position).getFile());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
holder.seekBar.setMax(mediaPlayer.getDuration());
mediaPlayer.start();
recordBeans.get(global_position).setPlay(true);
holder.start.setTextColor(Color.RED);
startPlayProgressUpdater(holder.seekBar, holder.start);
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
releaseMP();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
This is implemented in this method:
public void startPlayProgressUpdater(final SeekBar seekBar, final Button start) {
if (mediaPlayer != null) {
recordBeans.get(global_position).setSeekPos(mediaPlayer.getCurrentPosition());
if ((Integer) seekBar.getTag() == global_position) {
seekBar.setProgress(recordBeans.get(global_position).getSeekPos());
}
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater(seekBar, start);
}
};
handler.postDelayed(notification, 1000);
} else {
recordBeans.get(global_position).setPlay(false);
start.setTextColor(Color.BLACK);
recordBeans.get(global_position).setSeekPos(0);
seekBar.setProgress(0);
}
}
When I click the "stop" or the song ends, the player becomes the NULL and color button again changes. it is implemented in all of these methods:
holder.stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ((Integer) v.getTag() == global_position && mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
});
and
private void releaseMP() {
if (mediaPlayer != null) {
try {
mediaPlayer.release();
mediaPlayer = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
I want the same functionality if I worked while playing the song click on the "start" another song. ie a new song starts playing, the color buttons "start" becomes A red color and previous button "start" again black.
for this I when you click on "start" call methods:
releaseMP();
startPlayProgressUpdater(holder.seekBar, holder.start);
and the method works but the data is not cleared. but if I click on "Stop" then the data of all previous songs cleared. what I'm doing wrong?

I think you meant to call reset(), not release() in the stop button OnClick. Try to call release() in your main container OnStop()
reset()
This method resets the media player
release()
This method releases any resource attached with MediaPlayer object

Related

OnCompletionListener not called once media player has been reset and initialised

In my code, I have a start/stop button.I am trying to play an audio clip from the raw folder. On completion of an audio clip, the start/stop button has to change from stop mode to start mode. I don't want to loop the audio. For this,I have called OnCompletionListener. However, the listener is called only once. If the media player is reset and created again, OnCompletionListener is no longer called.
public void playsong(View view) {
if (mySound != null)
{
if (!pause) {mySound.start();
btnStartStop.setBackgroundResource(R.drawable.button);
pause = true;
} else {
if (mySound.isPlaying()) {
mySound.stop();
do_it();
}
}
}
}
mySound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mySound) {
do_it();
}
});
public void do_it() {
mySound.reset();
mySound = MediaPlayer.create(this, R.raw.a);
pause = false;
btnStartStop.setBackgroundResource(R.drawable.button2);
}
Try this code ?
In this example oncompletionlistner is called everytime
btnDemo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(objPlayer!=null){
if(objPlayer.isPlaying())
stopPlaying(objPlayer);
}
objPlayer = MediaPlayer.create(getApplicationContext(),R.raw.demoaudio);
if(objPlayer.isPlaying())
objPlayer.pause();
else
objPlayer.start();
Log.d("null", "Media Player started!");
if(objPlayer.isLooping() != true){
Log.d("null", "Problem in Playing Audio");
}
objPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
objPlayer.release();
objPlayer = null;
};
});
}
});

How to combine play & pause button into a single button

Here I have 2 buttons for the audio controller, play and pause. How can I combine both buttons into one? I also need the image icon to change as well.
In onCreate, I have initially disabled Pause button.
pauseButton.setEnabled(false);
Below is the implementation for Play button.
public void play(View view){
mediaPlayer.start();
...
pauseButton.setEnabled(true);
playButton.setEnabled(false);
}
Below is the implementation for Pause button.
public void pause(View view){
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
Make a single button and take a flag as
Boolean buttonflag=true;//initially true
Suppose true means play and false will mean pause.
public void button(View view)
{
if(buttonflag)//when true play
{
mediaPlayer.play();
buttonflag=false;//set to false so that on next click else will work
}
else//when false pause
{
mediaPlayer.pause();
buttonflag=true;//set to true so that on next click if will work
}
}
You can do this by 2 ways :
1. Take a frame layout and put two buttons overlapping each other (by default set visible to play button and hide the pause button) and set visibility according to it like
public void play(View view) {
if(pauseButton.isVisible()) {
pauseButton.setVisibility(View.GONE);
}
playButton.setVisibility(View.VISIBLE);
}
public void pause(View view) {
if(playButton.isVisible()) {
playButton.setVisibility(View.GONE);
}
pauseButton.setVisibility(View.VISIBLE);
}
2. You can take only one button and change it backgroundResource (by default set background resource play) like
public void play(View view) {
Button.setBackgroundResourec(R.drawable.pause);
}
public void pause(View view) {
Button.setBackgroundResourec(R.drawable.play);
}
MediaPlayer m1 = null;
playAndStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
m1=MediaPlayer.create(MainActivity.this, R.raw.sound1);
m1.start();
}
});
private void stopPlaying() {
if (mp1 != null) {
m1.stop();
m1.release();
m1 = null;
}
This should work for you! Good luck.
I know it is quite late to answer this question, but i have searched for this question and google shown me this at the top of result, but as i see there is not satisfactory result is available here, So here i am posting my code so that it could help other
fabBtnPlayAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!playPause) {
fabBtnPlayAudio.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
if (intialStage){
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource("URL for audio file.. ");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
fabBtnPlayAudio.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
playPause = false;
}
}
});

Stop media player in BaseAdapter android

I have a Fragment with a ListView which is a list of sounds, that is populated in this way:
MediaPlayer mMediaPlayer;
listview.setAdapter(new ListAdapter(getActivity(), tit, desc, songs, filenames, mMediaPlayer));
MediaPlayer is given as parameter because i would like that when back button is pressed or app goes in background, the player would stop playing.
So i have override onPause and onDestroy method in my Fragment, but this just doesn't work. Sound continues playing even if app is closed. This is my code:
#Override
public void onDestroy() {
// TODO Auto-generated method stub
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
super.onDestroy();
}
#Override
public void onPause() {
// TODO Auto-generated method stub
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
super.onPause();
}
I play sounds in my BaseAdapter class, in this way:
public void playSound2(int pos){
if(isPlaying){
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
isPlaying = false;
}
mMediaPlayer = MediaPlayer.create(context, songs[pos]);
mMediaPlayer.start();
isPlaying = true;
}
Do you have any suggestion? Thank you.
My first suggestion is: don't use the adapter to interact with the media player. Instead, attach an item click listener to your listview, like this
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
}});
and do whatever you want with the media player in the onItemClick callback.
That being said, you should reset your media player when inside your onclicklistener, like so
try {
mPlayer.reset();
mPlayer.setDataSource(trackToBePlayed); //trackToBePlayed is dependent on the select position
mPlayer.setOnPreparedListener(mPreparedListener);
mPlayer.prepareAsync();
} catch (IOException e) {
} catch(IllegalArgumentException e) {
}
And in the mPreparedListener, when ready, you do
mPreparedListener = new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
};
This should be enough to point you in the right direction :)

How do I destroy an activity?

I have a loop-all button and a stop button. Both buttons work fine while I'm still on the page. The problem is when I hit the loop-all button, it plays a series of audio files as it is supposed to, but when I leave the page (i.e. hit the phone's back button) and come back to the page, the audio doesn't stop! I hit the stop button, but it does nothing. The only way to stop it is to go into task manager and end the program. It seems to me that the reference of mp2 gets lost once I leave the page...Does anyone have any suggestions on how to fix this. Any help would be appreciated. Here is the code:
public class OneVoc extends ActionBarActivity {
private ListView lv;
private MediaPlayer mp;
private MediaPlayer mp2;
int[] myAudio = {R.raw.v_1100, R.raw.v_1101, R.raw.v_1102, R.raw.v_1103, R.raw.v_1104, R.raw.v_1105,
R.raw.v_1113, R.raw.v_1106, R.raw.v_1107, R.raw.v_1108, R.raw.v_1109, R.raw.v_1110, R.raw.v_1112,
R.raw.v_1114, R.raw.v_1115, R.raw.v_1116};
int mCompleted = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_voc);
Button btnLoop = (Button) findViewById(R.id.button1);
Button btnStop = (Button) findViewById(R.id.button2);
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp2 != null) {
if (mp2.isPlaying()) {
mp2.setOnCompletionListener(null);
mp2.stop();
}
mp2.reset();
mp2.release();
mp2 = null;
}
}
});
btnLoop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mp2 = MediaPlayer.create(getBaseContext(), myAudio[0]);
mp2.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp2)
{
mCompleted++;
mp2.reset();
if (mCompleted < myAudio.length)
{
try
{
AssetFileDescriptor afd = getResources().openRawResourceFd(myAudio[mCompleted]);
if (afd != null)
{
mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp2.prepare();
mp2.start();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else if (mCompleted == myAudio.length)
{
mCompleted =0;
try
{
AssetFileDescriptor afd = getResources().openRawResourceFd(myAudio[mCompleted]);
if (afd != null)
{
mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp2.prepare();
mp2.start();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else
{
mCompleted=0;
mp2.release();
mp2 = null;
}
}
});
mp2.start();
}
});
try overriding onbackpressed method
#Override
public void onBackPressed()
{
// code here to stop and finish()
super.onBackPressed();
}
I think it's an It's an Audio buffer issue, memory is loaded in audio buffer and persist later on,finish() might not help u.
When u press back button on an android button it simply calls the finish() for current activity, while when u press home button it calls onPause(),
You need to override onBackPressed() method, and before line super.onbackPressed();
Put your code to stop playing.
When u go to TakMgr it simply get's the PID refer to ur App process and kills the process.
Which is like System.exit(0); or android.os.process.kill(getMyPid());
Which simply ends ur app which u do not want.
Can you pause your mp in onPause() and restart it in onResume()?
Maybe you want also to store in savedInstance at which point you were!
Hope it's helpful
when you back press the activity just stop the mediaplayer and free the resources and finish the activity.that's it..!
#Override
public void onBackPressed()
{
btnStop.performClick();
super.onBackPressed();
}

How to add soundButton(on&off) on a activity in android apps?

I Have created a branch activity .Now i wanted to add two button on that branch activity.
When i click on 'sound on' button then my beep sound on start and when i clicked on 'sound off' then my beep sound off. and also they hide simultaneously.
Thank's
MY Code on Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_layout);
soundBttnOn =(Button) findViewById(R.id.soundBttnOn);
soundBttnOn.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
startMediaPlayer();
}
}
);
soundBttnoff =(Button) findViewById(R.id.soundBttnOff);
soundBttnoff.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
stopMediaPlayer();
}
}
);
}
private void startMediaPlayer() {
mediaPlayer = MediaPlayer.create(SoundLayout.this,R.raw.keybutton5);
mediaPlayer.start();
}
private void stopMediaPlayer() {
if( mediaPlayer != null ) {
MediaPlayer mp = null;
mp.stop();
mp.release();
}
}
It showing no problem but it is not working too..:P..I am not able to implement sound.
You can do simple google search to find tons of samples for adding button. However for playing sound file check out the MediaPlayer class.
Button startBtn, stopBtn;
//get the reference of Button
....
final MediaPlayer mp = new MediaPlayer();
startBtn.onClickListener() {
public void onClick(View v) {
try {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
stopBtn.onClickListener() {
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
//hide buttons
stopBtn.setVisibiltiy(View.GONE);
startBtn.setVisibility(View.GONE);
}
}
};
PS: For only on and off, you don't need two buttons, you could do with one button.
EDIT:
For single button, just use the playing state of Media player for deciding about the action to take on button click.
singleBtn.onClickListener() {
public void onClick(View v) {
try {
if(mp.isPlaying()) mp.stop();
else {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};

Categories

Resources