When I push the next button, I want to go next layout and stop still playing music.
If I push the play button and push the next button app is working.
But, if music doesn't play and I push to the next layout button, the app is crashing because my code try to stopped even though music isn't playing. What can i do ?
package com.gamedom.relaxingmusic;
import gif.decoder.GifRun;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
public class Music1 extends Activity {
public static MediaPlayer mp1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music1);
final ImageButton Start1 =(ImageButton) findViewById(R.id.btnStart1);
final ImageButton Stop1 =(ImageButton) findViewById(R.id.btnStop1);
ImageButton SOL1 =(ImageButton) findViewById(R.id.btnSol1);
ImageButton SAG1 =(ImageButton) findViewById(R.id.btnSag1);
Stop1.setVisibility(View.INVISIBLE);
Start1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1=MediaPlayer.create(Music1.this, R.raw.firefurnace);
mp1.start();
mp1.setLooping(true);
Start1.setVisibility(View.INVISIBLE);
Stop1.setVisibility(View.VISIBLE);
}
});
Stop1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.stop();
mp1.release();
Stop1.setVisibility(View.INVISIBLE);
Start1.setVisibility(View.VISIBLE);
}
});
//////NEXT LAYOUT BUTTON \\\\\\\\\
SAG1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC2"));
}
});
////// BACK LAYOUt BUTTON \\\\\\\\\
SOL1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC4"));
}
});
}
Problem is here
SOL1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//It try to stoping the music but it's already stopped program is crashes
if (mp1.isPlaying()) {
mp1.stop();
}
startActivity(new Intent("android.intent.action.MUSIC4"));
}
});
It looks like you initialize your media player object in an onClick. So, if that event is not fired, your mp1 variable will be null. I believe you are getting NullPointerException.
Related
Can you please help me about this game that I created, I want to reload the same activity, like restart game in quiz game. I hope you guys can help me, this is the code:
package com.example.splash;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.widget.ImageViewCompat;
import android.content.Intent;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class stagenumbereasy1 extends AppCompatActivity {
Button congratsback,congratshome,congratsforward,reload;
ImageView congrats,tryagain;
Animation fromsmall,fromnothing;
MediaPlayer click,congrats1,die;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_stagenumbereasy1);
click = MediaPlayer.create(this, R.raw.click);
congrats1 = MediaPlayer.create(this, R.raw.congrats);
die = MediaPlayer.create(this, R.raw.die);
final Button butt8=(Button)findViewById(R.id.button8);
final Button butt9=(Button)findViewById(R.id.button9);
final Button butt10=(Button)findViewById(R.id.button10);
final Button butt11=(Button)findViewById(R.id.button11);
final Button back=(Button)findViewById(R.id.button12);
final LinearLayout overbox=(LinearLayout)findViewById(R.id.overbox);
final LinearLayout congratsbox=(LinearLayout)findViewById(R.id.congratsbox);
final LinearLayout tryagainbox = (LinearLayout)findViewById(R.id.tryagainbox);
final LinearLayout trybox=(LinearLayout)findViewById(R.id.trybox);
fromsmall = AnimationUtils.loadAnimation(this, R.anim.fromsmall);
fromnothing = AnimationUtils.loadAnimation(this, R.anim.fromnothing);
congratsbox.setAlpha(0);
overbox.setAlpha(0);
tryagainbox.setAlpha(0);
trybox.setAlpha(0);
congratsback=(Button)findViewById(R.id.congratsback);
congratshome=(Button)findViewById(R.id.congratshome);
congratsforward=(Button)findViewById(R.id.congratsforward);
congrats=(ImageView)findViewById(R.id.congrats);
reload=(Button)findViewById(R.id.reload);
tryagain=(ImageView)findViewById(R.id.try1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent back=new Intent (stagenumbereasy1.this,stagenumber.class);
click.start();
startActivity(back);
}
});
butt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
congrats1.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
overbox.setAlpha(1);
overbox.startAnimation(fromnothing);
congratsbox.setAlpha(1);
congratsbox.startAnimation(fromsmall);
}
});
congratshome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent home = new Intent(stagenumbereasy1.this, Home.class);
click.start();
startActivity(home);
}
});
congratsforward.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent easy2 = new Intent(stagenumbereasy1.this, stagenumbereasy2.class);
click.start();
startActivity(easy2);
}
});
butt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent reload = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(reload);
finish();
}
});
}
});
butt11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
click.start();
die.start();
butt8.setEnabled(false);
butt9.setEnabled(false);
butt10.setEnabled(false);
butt11.setEnabled(false);
back.setEnabled(false);
trybox.setAlpha(1);
trybox.startAnimation(fromnothing);
tryagainbox.setAlpha(1);
tryagainbox.startAnimation(fromsmall);
reload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent refresh = new Intent(stagenumbereasy1.this, stagenumbereasy1reload.class);
click.start();
startActivity(refresh);
finish();
}
});
}
});
}
}
Image of my game
I actually create another activity to make it happens but when I click the reload button it is going back to home. This is my thesis game.
I don't know if exists a recommended answer in Android docs. But, I think you can call startActivity() and finish(), like:
// Java
startActivity(new Intent(MyActivity.class));
finish();
// Kotlin
startActivity(Intent(MyActivity::java.class));
finish();
This will create a new instance of the activity and destroy the current.
For better performance, you should make tests with Fragments instead resetting an Activity.
You could try recreate(); which would recreate the activity
You could also try Intent intent = new Intent(MyActivity.this, MyActivity.class);
startActivity(intent);
Just simply use intent flags one is INTENT_FLAGS_NEW_TASK
it restart activity
Intent.setflags()
In your current activity recall it
Intent(main.this,main.class)
I am a very beginner in Android, I am trying to make a MediaPlayer works but I have some errors.
1-If I click on play again , start playing twice at the same time.
2-If click on puse, doesn't happen anything.
3-My intention is to do a reproduction list with play, pause and stop.
Thanks in advance.
package com.example.android.allmusic;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.media.MediaPlayer;
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (!mp.isPlaying()) {mp.start();}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
if (mp.isPlaying()) { mp.pause(); }
}
});
}
}
Try declaring your MediaPlayer as a member variable, it should be enough to set it on your onCreate, but just declare it as an attribute of your class to test.
Since you are creating a new reference to the MediaPlayer every time you click on the ImageView or the TextView, you don't the old reference to pause it or to know if it has already been started.
Code:
public class RomanticActivity extends AppCompatActivity {
boolean firstSongBoolean;
//MediaPlayer as a member variable
private MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_romantic);
mp = MediaPlayer.create(getApplicationContext(), R.raw.primero);
TextView firstSong = (TextView) findViewById(R.id.first_song);
firstSong.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mp.isPlaying()) {
mp.start();
}
}
});
ImageView firstSongPause = (ImageView) findViewById(R.id.first_song_pause);
firstSongPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mp.isPlaying()) {
mp.pause();
}
}
});
}
}
I want to either be able to have a single button that you click to start and click again to stop or a start button that does not start multiple sounds each time it clicks. I have an audio file that if you click start 3 times, it has 3 different tracks going all at once. And the stop button stops only one, the other tracks keep playing until I kill the app.
Here is my code. I am a noob
package com.example.mitch_000.button;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player=MediaPlayer.create(MyActivity.this,R.raw.lw);
player.start();}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
}
When you call player=MediaPlayer.create(MyActivity.this,R.raw.lw); in your OnClickListener, you are creating a new instance of the audio file and then playing it every time the button is clicked.
You need to create a single instance of the player object outside of the OnClickListener (in OnCreate) and then you can use this reference to play, pause and stop the one audio file.
Here is the code you can use:
package com.example.mitch_000.button;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
player=MediaPlayer.create(MyActivity.this,R.raw.lw);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
put
MediaPlayer mp = MediaPlayer.create(this, R.raw.mamacita_zero);
outside of setOnClickListener
like this
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
MediaPlayer mp = MediaPlayer.create(MyActivity.this,R.raw.lw);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player.stop();
}
});
}
}
You seem to be doing per click than you need to. You're creating and adding a new onClickListener for every click in the Activity's View, not the Button. You only need to set the listener once, and for the Button rather than the overarching View; I tend to do that in the constructor of the Activity.
package project.kalmas;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class one extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
MediaPlayer mp= MediaPlayer.create(this,R.raw.one);
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
When i click button it play sound then again i click button to stop it wont stop and play sound again which results in double sound playing at one time.Please help
You are creating a new MediaPlayer with every click, instead of keeping a reference to the first one. The MediaPlayer that is playing the sound is different than the MediaPlayer that you are calling isPlaying() on. You need to turn the mp variable into a field so you can keep a reference to it.
I imagine something like this would work:
public class one extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
mp = MediaPlayer.create(this,R.raw.one);
}
public void onclick2(View view)
{
Intent i=new Intent("project.two");
startActivity(i);
}
public void onclick3(View view)
{
if(mp.isPlaying()){
mp.stop();
} else {
mp.start();
}
}
}
I'm trying to create an app that has many pages, every page has button.
When I click on the button it takes me to next page.
What i want to do is when I press on the button it plays the sound that i have included in "raw folder?
At this moment when I manipulated the code and I press on the button to change the page it close the app. Can anyone please help. Thanks
here is my code:
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button button;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp;
#Override
public void onClick(View view) {
mp.seekTo(0);
mp.start();
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.test);
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
}
#Override
public void onPause() {
super.onPause();
// Release the MediaPlayer if going into background
if(mp != null) mp.release();
} }
If this is how your code is laid out:
button.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp;
#Override
public void onClick(View view) {
mp.seekTo(0);
mp.start();
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.test);
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
Then the reason is because you're trying to use the MediaPlayer before actually initializing it. Try this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
final MediaPlayer mp = MediaPlayer.create(context, R.raw.test);
mp.start();
}
catch(Exception e) { e.printStackTrace(); } // Eat it
Intent intent = new Intent(context, MainAct2.class);
startActivity(intent);
}
});
If that doesn't work, well, something is definitely wrong somewhere else.