Unable to stop mediaplayer - android

I've two radio buttons in a radiogroup and on selection of one radio button it will start music and stop the other sound and on selection of other it will start the next song and stop the previous song. The problem is I'm unable to stop the music. The song keeps on playing in the background. I'm using dialog box to show radio buttons and getting the input from user on button'c click.
The length of each song is greater than 3 minutes.
I've searched the internet and tried multiples solution but no solution is working. Below is the code I've used:
btnDone.setOnClickListener(view.onClickListener)
{
#Override
public void onClick(View view)
{
boolean checked = radioButton1.isChecked();
boolean checked1 = radioButton2.isChecked();
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.song1);
MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.song2);
if(checked && !checked1)
{
mediaPlayer.start();
}else if(checked1 && !checked)
{
try{
if(mediaPlayer !=null)
{
if(mediaPlayer.isPlaying())
{mediaPlayer.stop;
mediaPlayer.release;
mediaPlayer = null;
}
}
}
catch(Exception e)
{e.printStackTrace(); }
}
}
}

IMO your else if(checked1 && !checked1) condition not working properly so try else if(checked1 && !checked) try below code
btnDone.setOnClickListener(view.onClickListener)
{
#Override
public void onClick(View view)
{
boolean checked = radioButton1.isChecked();
boolean checked1 = radioButton2.isChecked();
MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.song1);
MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.song2);
if(checked && !checked1)
{
mediaPlayer.start();
}else if(checked1 && !checked) //change here
{
try{
if(mediaPlayer !=null)
{
if(mediaPlayer.isPlaying())
{mediaPlayer.stop(); // also change here
mediaPlayer.release(); //change here
mediaPlayer = null;
}
}
}
catch(Exception e)
{e.printStackTrace(); }
}
}
}

Related

Android Media Player crash when changing datasource

I`m using a service to play the music in the background. the user have a list of songs that he can listen online or downloading them and listen offline.
the problem appears when the user is in offline mode. if he downloads a song he can play it with no problems, but if he skip to the next one(not downloaded) and there is not internet, it does not work as expected and this is not a problem. the problem appears when the user skip to previous one which is downloaded, this time its not playing
private void initMediaPlayer() {
int sura_number = activeSuraModel.getSura_number();
String formatted_sura_number = String.format("%03d", sura_number); //001
String path = activeSuraModel.getReciter_server() + "/" + formatted_sura_number + ".mp3";
mediaPlayer = new MediaPlayer();
//Set up MediaPlayer event listeners
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnInfoListener(this);
//Reset so that the MediaPlayer is not pointing to another data source
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
String downloadedFilePath = downloadedFiles.getDownloadedFilePath(downloaded_files_map, activeSuraModel, reciterName);
if (downloadedFilePath == null) {
mediaPlayer.setDataSource(path); // get online
} else {
mediaPlayer.setDataSource(downloadedFilePath);// get from storage
}
} catch (Exception e) {
Log.i("pat", "error " + e.getMessage());
stopSelf();
}
mediaPlayer.prepareAsync();
}
private void playMedia() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
private void skipToNext() {
if (suraIndex == suraModels.size() - 1) {
//if last in playlist
suraIndex = 0;
activeSuraModel = suraModels.get(suraIndex);
} else {
//get next in playlist
activeSuraModel = suraModels.get(++suraIndex);
}
//Update stored index
new StorageUtil(getApplicationContext()).storeAudioIndex(suraIndex);
stopMedia();
//reset mediaPlayer
mediaPlayer.reset();
initMediaPlayer();
}
private void skipToPrevious() {
if (suraIndex == 0) {
//if first in playlist
//set index to the last of audioList
suraIndex = suraModels.size() - 1;
activeSuraModel = suraModels.get(suraIndex);
} else {
//get previous in playlist
activeSuraModel = suraModels.get(--suraIndex);
}
//Update stored index
new StorageUtil(getApplicationContext()).storeAudioIndex(suraIndex);
stopMedia();
//reset mediaPlayer
mediaPlayer.reset();
initMediaPlayer();
}
#Override
public void onCompletion(MediaPlayer mp) {
if (isRepeat) {
mp.start();
} else if (isShuffle) {
Random random = new Random();
int random_number = random.nextInt(suraModels.size());
activeSuraModel = suraModels.get(random_number);
initMediaPlayer();
} else {// play next song
skipToNext();
}
}
private void stopMedia() {
if (mediaPlayer == null) return;
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}

how can I send view with parameters

I want to send view to another class with parameters
for example :
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EventHandlerClass.startMediaPlayer(view, soundID);
}
});
you can see this line where i'm sending View
EventHandlerClass.startMediaPlayer(view, soundID);
Service Class
public static void startMediaPlayer(View view, Integer soundID){
try {
// Check if the sound id was set correctly
if (soundID != null){
// Check if the MediaPlayer maybe is in use
// If so the MediaPlayer will be reset
if (mp != null && mp.isPlaying()){
mp.stop();
mp = null;
view.setVisibility(View.INVISIBLE);
}
else{
mp = MediaPlayer.create(view.getContext(), soundID);
mp.start();
}
// Create and start the MediaPlayer on the given sound id
}
} catch (Exception e){
// Log error if process failed
Log.e(LOG_TAG, "Failed to start the MediaPlayer: " + e.getMessage());
}
}
It only sends when you press the button , I want to send the offer but without pressing the button how?

How to detect button background resource (drawable) android?

I am currently building an app that has a button that once the user clicks the button, media recorder is initiated and records for five seconds and then stops. This recording is then loaded into media player which the user can then playback by pressing the button once again. Here is my code so far:
OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/tempRecord.3gpp";
audioSample1 = (Button) findViewById(R.id.sample1);
// button background drawable is grey_button
final Drawable buttonBackground = audioSample1.getBackground();
audioSample1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (audioSample1.getBackground().equals(buttonBackground)) {
Log.d("Button", "Start Record");
try {
startRecord();
} catch (Exception e) {
e.printStackTrace();
}
}
else if (audioSample1.getBackground() != null) {
Log.d("Button", "Start Playback");
try {
startPlayback();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
public void startRecord() throws Exception {
if (recorder != null) {
recorder.release();
}
File fileOut = new File(OUTPUT_FILE);
if (fileOut != null) {
fileOut.delete();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.setAudioSamplingRate(44100);
recorder.setMaxDuration(5000);
recorder.prepare();
recorder.start();
audioSample1.setBackgroundResource(R.drawable.red_button);
recorder.setOnInfoListener(new OnInfoListener() {
public void onInfo(MediaRecorder recorder, int timer, int extra) {
if (timer == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
Log.v("Recorder", "Maximum Duration Reached");
recorder.stop();
audioSample1.setBackgroundResource(R.drawable.green_button);
}
}
});
}
public void startPlayback() throws Exception {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}
}
Basically the issue I am having is the onClick method detects the state of the button and depending on the background drawable it either start recording or starts playback. If the button is in its first state drawable.grey_button the media recorder will start recording and change the background drawable to a red_button and once it is finished recording it will change to a green_button for playback. This code works fine generally but if the user presses the button while the media recorder is recording the media player will be unable to create a media player. I know the problem is because I am calling audioSample1.getBackground() != null but I can't figure out any other way to detect the backgroundResources that has been changed
Please can someone give me some advice on how to detect each backgroundResource?
Also once the Media Player is loaded with a audio sample I now want the same button to be able to implement an onTouchListener and gesture detector. Is this possible and how do you implement such a method?
Thanks
you approach is really tedious, but this is it getBackgroundDrawable(); use this instead getBackground() Returns the drawable set for the button.
but you can also use the string for the button, that might be simple...
EDIT 1 this approach is better
Button b = new Button(this); // now this is your button
b.setText("Play"); //the button has a name as 'play' when its not clicked
// and in your case is drawable.grey_button
b.setBackgroundResource(R.drawable.grey_button);
b.setOnClickListener(new OnClickListener() {//setting your onclick
#Override
public void onClick(View arg0) {
Button bb = (Button) arg0; // getting your clicked button
if(bb.getText().toString().equals("play")){ // checking if the text on your bb is play
//start your playback and change the background
bb.setText("pause"); // setting the text to pause
}else if(bb.getText().toString().equals("pause")){
//pause playback and change your background
bb.setText("play");
}else{
//stop play and reset mediaplayer and change your background..
// this is not even captured
}
}
});
this approach is better.. let me know if it helps

How to define the button to play Z if Y is already playing?

the idea is that i have 1 button and 2 states.
1. On press is playing sound Z (from a random sound list)
2. On second press: if Z.isPlaying play Y
All seems fine, till i press it 3 times in a row, and get a nullpointerexception
Here is the onClick code:
#Override
public void onClick(View view) {
if (view.getId() == R.id.goat) {
if(Piciu != null) {
if(mp2.isPlaying()){mp2.stop();mp2.reset();mp2.release();mp2 = null;}
butGoat.setBackgroundResource(R.drawable.goat96);
mp3 = MediaPlayer.create(this, R.raw.cow);
mp3.start();
butGoat.setBackgroundResource(R.drawable.goat96);
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp3) {
butGoat.destroyDrawingCache();
butGoat.setBackgroundResource(R.anim.clipeala);
yourAnimation = (AnimationDrawable) butGoat.getBackground();
yourAnimation.start();
Piciu = null;
}
});
toast.setText("I can`t yell so Fast!");
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();
} else {
Piciu = mp2;
butGoat.destroyDrawingCache();
butGoat.setBackgroundResource(R.drawable.goat96);
Random r = new Random();
int Low = 0;
int High = 16;
int rndm = r.nextInt(High-Low) + Low;
mp2 = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
mp2.start();
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp2) {
butGoat.setBackgroundResource(R.anim.clipeala);
yourAnimation = (AnimationDrawable) butGoat.getBackground();
yourAnimation.start();
mp2.reset();
mp2.release();
mp2 = null;
Piciu = null;
}
});
}
}
}
i just can't understand Why do i get nullpointerexception if Sound Z is not playing.
I see you didn't check for mp2 != null before calling mp2.isPlaying():
if (view.getId() == R.id.goat) {
if(Piciu != null) {
if(mp2.isPlaying()){mp2.stop();mp2.reset();mp2.release();mp2 = null;}
change it to if (Piciu != null && mp2 != null) Also not sure why you need the Piciu variable at all. I'd clear up the logic first, then cleanup the code if it's still crashing.

How can I know if the MediaPlayer is in Stopped state?

I am building an app which plays several videos, and I have two different user scenarios :
Scenario 1. While video 'A' is playing, if user clicks next button, then it stops and play the next video 'B'.
Scenario 2. Play video 'A', and if it's done, user clicks next button and it plays video 'B'.
For the first scenario, I used mediaPlayer.isPlaying() method to detect if it is in Started state and it works fine. However, if I use the same code for the second scenario, isPlaying() throws IllegalStateException.
Here's my code for playing videos :
private void playVideos(SurfaceHolder holder) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(holder);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + video_files[mCounter]);
mediaPlayer.setDataSource(this, uri);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mCounter <= 8) {
onVideoCompletion(mediaPlayer);
} else {
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
} catch (IOException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
}
}
Also, here's my button listener to play next video :
nextBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
mCounter += 1;
if (mCounter <= 8) {
playVideos(holder);
}
}
});
One way that I tried to hack this issue was using a boolean variable instead of isPlaying() method. For example,
boolean mIsPlaying = false;
...
// in button listener
if(mIsPlaying) {
mediaPlayer.stop();
mediaPlayer.release();
}
...
// in playVideos() function
mediaPlayer.start();
mIsPlaying = true;
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mCounter <= 8) {
onVideoCompletion(mediaPlayer);
} else {
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
That works for my both of scenario, but I'm not sure if it's the correct way to do it. Isn't there any way to detect whether mediaPlayer is in Stopped state?
I took a look at Google's Documentation which you can find here. You can only know if the player isPlaying(); or isLooping(); ... So no, there is not an "easy" or "short" way to achieve what you want. Hope it helped.

Categories

Resources