How can I clean up my MediaPlayer after use? - android

Is this the correct way to use the MediaPlayer?
public class MainActivity extends AppCompatActivity{
MediaPlayer sound1, sound2, sound3, sound4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Sound MediaPlayers
sound1= MediaPlayer.create(this, R.raw.sound1);
sound2= MediaPlayer.create(this, R.raw.sound2);
sound3= MediaPlayer.create(this, R.raw.sound3);
sound4= MediaPlayer.create(this, R.raw.sound4);
}
//sound onClick's
public void sound1(View view){
sound1.start();
}
public void sound2(View view){
sound2.start();
}
public void sound3(View view){
sound3.start();
}
public void sound4(View view){
sound4.start();
}
}
I have to clean up the MediaPlayer, but I don't know how I can do that.
I know that I have to use sound1.release() but it doesn't work if I write:
public void sound1(View view){
sound1.start();
sound1.release();
}
Please give me an example.

This should do the work for you.
public class MainActivity extends AppCompatActivity{
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//sound onClick's
public void sound1(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mediaPlayer.start();
}
public void sound2(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound2);
mediaPlayer.start();
}
public void sound3(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound3);
mediaPlayer.start();
}
public void sound4(View view){
cleanUpMediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.sound4);
mediaPlayer.start();
}
public void cleanUpMediaPlayer(){
if(mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}
}

Related

MediaPlayer.setOnCompletionListener not executing

public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
private MediaPlayer mediaPlayer;
private ImageButton playBtn,previousBtn,forwardBtn,rewindBtn,nextBtn;
private SeekBar seekBar;
private int[] songsName = {R.raw.song1,R.raw.song2,R.raw.song3,R.raw.song4};
private int songPosition=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.songProgressBar);
seekBar.setClickable(false);
seekBar.setEnabled(false);
playBtn = (ImageButton) findViewById(R.id.play);
playBtn.setOnClickListener((View.OnClickListener)this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(mediaPlayer != null && b){
mediaPlayer.seekTo(i);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
forwardBtn = (ImageButton) findViewById(R.id.forward);
forwardBtn.setOnClickListener((View.OnClickListener)this);
rewindBtn = (ImageButton) findViewById(R.id.rewind);
rewindBtn.setOnClickListener((View.OnClickListener)this);
nextBtn = (ImageButton) findViewById(R.id.next);
nextBtn.setOnClickListener((View.OnClickListener)this);
previousBtn = (ImageButton) findViewById(R.id.previous);
previousBtn.setOnClickListener((View.OnClickListener)this);
subView = (SubtitleView) findViewById(R.id.subs_box);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaplayer) {
Toast.makeText(getApplicationContext(),"Media Player End",Toast.LENGTH_LONG).show();
nextSong();
}
});
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play:
playSong();
break;
case R.id.forward:
forwardSong(5000);
break;
case R.id.rewind:
rewindSong(5000);
break;
case R.id.next:
if(mediaPlayer !=null)
nextSong();
break;
case R.id.previous:
previousSong();
break;
}
}
public void playSong(){
imageName = (String) playBtn.getTag();
if(imageName.equals("play")) {
playBtn.setTag("stop");
//mediaPlayer = MediaPlayer.create(this, R.raw.chalisa);
Log.d(TAG,""+songPosition);
prepareMediaPlayer(songPosition);
seekBar.setMax(mediaPlayer.getDuration());
seekUpdation();
seekBar.setEnabled(true);
playBtn.setImageResource(R.drawable.stop);
}else{
playBtn.setImageResource(R.drawable.play);
playBtn.setTag("play");
mediaPlayer.pause();
mediaPlayer = null;
seekBar.setProgress(0);
seekBar.setEnabled(false);
subView.setText("");
}
}
public void forwardSong(int time){
if(mediaPlayer != null){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() + time);
}
}
public void rewindSong(int time){
if(mediaPlayer != null){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() - time);
}
}
public void nextSong(){
songPosition++;
if(songPosition < songsName.length){
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}else{
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
songPosition = 0;
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}
}
public void previousSong(){
songPosition--;
if (songPosition != -1){
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}else{
mediaPlayer.pause();
seekBar.setProgress(0);
mediaPlayer.seekTo(0);
songPosition = 0;
mediaPlayer = null;
prepareMediaPlayer(songPosition);
}
}
public void prepareMediaPlayer(int songPosition){
mediaPlayer = MediaPlayer.create(this, songsName[songPosition]);
mediaPlayer.start();
subView.setPlayer(mediaPlayer);
subView.setSubSource(subTitleName[songPosition], MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
}
public void seekUpdation() {
if(mediaPlayer != null) {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
myHandler.postDelayed(run, 1000);
}
}
Runnable run = new Runnable() {
#Override public void run()
{
seekUpdation();
}
};
media player on completion not working, not able to print the log also.
My intention is to play the next song once the current song is completed, but i am not able to check the song is completed or not.
In the logcat, it is showing the below warning
W/MediaPlayer-JNI(16888): MediaPlayer finalized without being released.
not showing any error in the logcat......
What may be the issue?
Anything wrong with the MediaPlayer.onCreate?
setOnCompletionListener Always Work after Allocating Media Resources.
remove It From OnCreate Method And
add this listener to you Method PlaySong() after Preparing You Media Player..
It May Work.
public void playSong(){
imageName = (String) playBtn.getTag();
if(imageName.equals("play")) {
playBtn.setTag("stop");
//mediaPlayer = MediaPlayer.create(this, R.raw.chalisa);
Log.d(TAG,""+songPosition);
prepareMediaPlayer(songPosition);
seekBar.setMax(mediaPlayer.getDuration());
seekUpdation();
seekBar.setEnabled(true);
playBtn.setImageResource(R.drawable.stop);
}else{
playBtn.setImageResource(R.drawable.play);
playBtn.setTag("play");
mediaPlayer.pause();
mediaPlayer = null;
seekBar.setProgress(0);
seekBar.setEnabled(false);
subView.setText("");
}
myMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// do what ever you want
}
});
}
And Also This mediaPlayer = MediaPlayer.create(this, songsName[songPosition]); add Only Single time For Selected Song After Play do not create again..
then Again Create When You Are Doing Nextsong.

How to make ImageButton repeat a sound each time it's clicked

I am trying to get the Image Button to play a sound each time it's clicked. For example, if the sound is playing for few seconds and I press the button again it should start from the beginning. I managed to get the sound working but it's not repeating it. How can I get it to start over?
ImageButton bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
}
Try this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, R.raw.bird);
}
mp.start();
} catch(Exception e) {
e.printStackTrace();
}
}
Use MediaPlayer.seekto() to seek to the start of the audio file.
ImageButton bt;
private MediaPlayer mp = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.seekTo(0);
} else {
mp.start();
}
}
});
}

MediaPlayer does not work properly

I'm using MediaPlayer for play a click sound when user clicks on a button. Sometimes the sound will play fine but other times it is too slow. For example first click is fine but second click is too slow.
Here is my code:
private MediaPlayer mClickSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mClickSound = MediaPlayer.create(this, R.raw.click);
}
#Override
public void onClick(View view) {
try {
if (mClickSound.isPlaying()) {
mClickSound.stop();
mClickSound.release();
mClickSound = MediaPlayer.create(this, R.raw.click);
}
mClickSound.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Try this:
mClickSound.reset();
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.click);
if (afd == null) return;
mClickSound.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mClickSound.start();
afd.close();
setDataSource is taken from here:
https://stackoverflow.com/a/20111291/6159609
The reset method is supposed to be faster.
Please try below code working fine for me...
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button btn;
MediaPlayer mClickSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
mClickSound = MediaPlayer.create(this, R.raw.click);
btn.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickSound.isPlaying()) {
mClickSound.reset();
}
else {
mClickSound = MediaPlayer.create(this, R.raw.click);
mClickSound.start();
}
}
}

Android: Problems with MediaPlayer Class, not play a song

What is the problem with this code, because the music is not played?
It is a song with mp3 file format, weights 4 megabits in res/raw.
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer = new MediaPlayer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer.create(MainActivity.this, R.raw.music);
((Button)findViewById(R.id.playButton)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.start();
Toast.makeText(MainActivity.this, "Play", Toast.LENGTH_SHORT).show();
}
});
((Button)findViewById(R.id.pauseButton)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.pause();
Toast.makeText(MainActivity.this, "Pause", Toast.LENGTH_SHORT).show();
}
});
}
}
// assign the instance to mediaPlayer object
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.music);
Initialize it in OnCreate()
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.music);
//...
}
}

Is this a good way to use MediaPlayer?

I think this is a decent way to use MediaPlayer for a Button or one time use. Am I right? Is the try block necessary? And what should I be trying to catch here? I'm really having a hard time finding a rock solid way to play a sound once.
// Button sound
private void playButtonSound() {
try{
final MediaPlayer startPlayer = MediaPlayer.create(
getApplicationContext(), R.raw.sound);
startPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
startPlayer.release();
}
});
startPlayer.start();
} catch(Throwable t){}
}
I use it like that:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.tosse);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}

Categories

Resources