Stop MediaPlayer file - android

I'd just like to stop my MediaPlayer file and I really don't understand why is that so complicated. I have this method to start to music:
public void startMusic() {
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
mediaPlayer1.start();
mediaPlayer1.setVolume(0.2f, 0.2f);
mediaPlayer1.setLooping(true);
play = true;
}
And I'd like to stop music with this method:
public void stopMusic(){
mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
if(play){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}
When I call the startMusic method the music starts but when I call the stopMusic method then nothing happens.If anyone knows how to do that please response.

mediaPlayer1 = MediaPlayer.create(context, R.raw.zenenegy);
On this line you are creating a new MediaPlayer object and you lose the old object reference (it will continue playing until gc). Use same object, as listed below
public void stopMusic(){
if (mediaPlayer1 != null){
if(mediaPlayer1.isPlaying()){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
}
}

You are again creating MediaPlayer instance and stopping the new instance. But what you need to do is to stop the existing MediaPlayer instance which is running and playing audio. Change your stopMusic() -
public void stopMusic(){
if(play && mediaPlayer1 != null){
mediaPlayer1.stop();
mediaPlayer1.reset();
mediaPlayer1.release();
mediaPlayer1 = null;
play = false;
}

Related

MediaPlayer is not playing?

when the sound playback is repeated for a long time. when the example is clicked 10 times He does not play. what is the reason why it plays when you restart the application
mediaPlayer = MediaPlayer.create(mContext, R.raw.click);
mediaPlayer.setVolume(7f, 7f);
mediaPlayer.start();
mediaPlayer.reset() will help you!
Ex:
public static void startSound(Context context, int soundResId) {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer = null;
}
mMediaPlayer = MediaPlayer.create(context, soundResId);
mMediaPlayer.start();
}

Android - How to remove MediaPlayer loop delays?

I set a music file to loop on MediaPlayer for my game, but it causes a 2 sec delay when it loops.
My code:
boolean activateSounds = getIntent().getBooleanExtra("Activate sounds", true);
if(mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(StartGame.this, R.raw.music1);
mp.setVolume(8f, 8f);
mp.setLooping(true); // This is causing delays
if (activateSounds) mp.start();
For a game, this is not interesting. Is there a way to make MediaPlayer run out of loop delays?
I was not able to make setLooping work without a gap.
Only solution that worked for me was to use setNextMediaPlayer call (which is able to start next loop without a gap) and two MediaPlayers.
'pseudo' code:
class Abc implements MediaPlayer.OnCompletionListener {
private final MediaPlayer[] mps = new MediaPlayer[2];
public Abc() {
mps[0] = new MediaPlayer();
mps[1] = new MediaPlayer();
mps[0].setOnCompletionListener(this);
mps[1].setOnCompletionListener(this);
}
public void start()
initMediaPlayer(mps[0]);
initMediaPlayer(mps[1]);
mps[0].setNextMediaPlayer(mps[1]);
mps[0].start();
}
private void initMediaPlayer(MediaPlayer mp)
{
if (mp.isPlaying()){
mp.stop();
}
mp.reset();
final float volume = 0.07f;
mp.setDataSource(MY_SOURCE);
mp.setVolume(volume, volume);
mp.setLooping(false);
try {
mp.prepare();
}catch(Exception error){
Log.d("BackgroundMusic", error.toString());
}
}
#Override
public void onCompletion(MediaPlayer mp)
{
MediaPlayer cur = mps[0] == mp ? mps[1] : mps[0];
initMediaPlayer(mp);
cur.setNextMediaPlayer(mp);
}
}

Android: How can I prevent MediaPlayer from playing overlapping audio?

I'm building a thug life sound effect player and I wanted to know how can I stop the MediaPlayer from playing 2 or more tracks the same time.
This is the method that's called when a button is clicked:
int sequence = 1;
public void thugPlay (View view) {
// assign a media player to an audio file
MediaPlayer nothingButThatGthang = MediaPlayer.create(MainActivity.this, R.raw.g_thang);
MediaPlayer nextEpisode = MediaPlayer.create(MainActivity.this, R.raw.next_episode);
MediaPlayer fuckThePolice = MediaPlayer.create(MainActivity.this, R.raw.ftp);
MediaPlayer hipnotize = MediaPlayer.create(MainActivity.this, R.raw.hipnotize);
MediaPlayer moveBitch = MediaPlayer.create(MainActivity.this, R.raw.move_bitch);
MediaPlayer ridin = MediaPlayer.create(MainActivity.this, R.raw.ridin);
MediaPlayer still = MediaPlayer.create(MainActivity.this, R.raw.still);
// Plays according to sequence's value
if (sequence == 1) {
still.stop();
nothingButThatGthang.start();
sequence++ ;
}
else if (sequence == 2) {
nothingButThatGthang.stop();
nextEpisode.start();
sequence++ ;
}
else if (sequence == 3) {
nextEpisode.stop();
fuckThePolice.start();
sequence++ ;
}
else if (sequence == 4) {
fuckThePolice.stop();
hipnotize.start();
sequence++ ;
}
else if (sequence == 5) {
hipnotize.stop();
moveBitch.start();
sequence++ ;
}
else if (sequence == 6) {
moveBitch.stop();
ridin.start();
sequence++ ;
}
else {
ridin.stop();
still.start();
sequence = 1 ;
}
}
As you can see, before playing the current audio file it should stop the previous one, however, it doesn't happen.
Thank you!
Create one MediaPlayer variable as class member:
MediaPlayer mp;
Then in your click event:
if (mp != null && mp.isPlaying()){
mp.stop();
}
if (sequence == 1) {
mp = MediaPlayer.create(MainActivity.this, R.raw.still);
mp.start();
sequence++;
}
You should off-course complete the If statment.

Android MediaPlayer will not play different songs

I'm trying to use one MediaPlayer to play a number of songs in succession. The first song will play as needed, but afterwards, one particular song (the first song in alphabetic order) will play over and over again. I followed this: Android Mediaplayer play different songs after eachother
public void startPlayer(View view) {
game = new Game();
TextView textView = (TextView) findViewById(R.id.title);
// start first song
Music firstSong = game.getNextSong();
textView.setText(firstSong.getID());
mediaPlayer = MediaPlayer.create(view.getContext(), firstSong.getID());
// make sure rest of songs play
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
goToNextSong();
}
});
// actually start!
mediaPlayer.start();
}
public void goToNextSong() {
Music nextSong = game.getNextSong();
TextView textView = (TextView) findViewById(R.id.title);
// if we still have music to play
if (nextSong != null) {
try {
// set the new title
textView.setText(nextSong.getID());
mediaPlayer.stop();
mediaPlayer.reset();
// get the music file
FileDescriptor fd = getResources().openRawResourceFd(
nextSong.getID()).getFileDescriptor();
mediaPlayer.setDataSource(fd);
// play it!
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Even when I set fd to a particular song, it will still play the first song in alphabetical order. nextSong.getID() returns R.raw.somesong. The textview (which is set to the song ID) changes to the correct song. Help?
So I haven't found a way to keep the same MediaPlayer and play different songs, so I just made a new one each time. It works!
public void startPlayer() {
game = new Game();
goToNextSong();
}
public void goToNextSong() {
Music nextSong = game.getNextSong();
TextView textView = (TextView) findViewById(R.id.title);
// if we still have music to play
if (nextSong != null) {
try {
// set the new title
textView.setText(nextSong.getID());
// stop old music player
if (mediaPlayer != null) {
mediaPlayer.stop();
}
// create new music player
mediaPlayer = MediaPlayer.create(textView.getContext(),
nextSong.getID());
// make sure rest of songs play
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
goToNextSong();
}
});
// actually start!
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
} else {
// we're done!
mediaPlayer.release();
mediaPlayer = null;
}
}

Null pointer exception on getting value from onRetainNonConfigurationInstance() - Android

I am developing an application which plays 2 audio files simultaneously with different Mediaplayer instance. To retain the state of mediaplayer on screen rotation implemented the method
public Object onRetainNonConfigurationInstance()
{
HashMap<String,Object> player = new HashMap<String,Object>();
MediaPlayer instance = mp;
mp = null;
MediaPlayer instance1 = mp1;
mp1 = null;
player.put("mp", instance);
player.put("mp1", instance1);
return player;
}
In onCreate(),
HashMap<String, Object> playerhandle = (HashMap<String, Object>)getLastNonConfigurationInstance();
mp = (MediaPlayer)playerhandle.get("mp");
if (mp == null)
{
mp = new MediaPlayer();
}
mp1 = (MediaPlayer)playerhandle.get("mp1");
if (mp1 == null)
{
mp1 = new MediaPlayer();
}
Showing NullPointerException in logcat........
You never check if playerhandle is null

Categories

Resources