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
Related
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();
}
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);
}
}
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;
}
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.
I am trying to play next song with mediaPlayer.setOnCompletionListener() but it only plays one next song and after that song ends mediaplayer stops.How to make mediaplayer play all the time?
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList)b.getParcelableArrayList("songList");
position = b.getInt("pos",0);
u = Uri.parse(mySongs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), u);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.stop();
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
u = Uri.parse(mySongs.get(position + 1).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), u);
mediaPlayer.start();
}
});
You don't need to wait until onCompletion. Just create both players at once and use setNextMediaPlayer to queue the second one
u1 = Uri.parse(mySongs.get(position).toString());
u2 = Uri.parse(mySongs.get(position + 1).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), u1);
mediaPlayerNext = MediaPlayer.create(getApplicationContext(), u2);
mediaPlayer.setNextMediaPlayer(mediaPlayerNext);
And you are not receiving onCompletion after the second song because you replace mediaPlayer value with a reference to a new player that doesn't have the listener set. Add mediaPlayer.setOnCompletionListener() to the onCompletion handler. You also don't need to prepare old player