Why doesn't my song work in my android app? - java

My audio doesn't play.
What is the problem ??
public class AudioActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
final MediaPlayer mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
Button play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
mp.start();
}
});
Button bause = (Button) findViewById(R.id.bause);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
mp.pause();
}
});
}
}

You called setOnClickListener() on the play button twice. You meant to call bause.setOnClickListener(). When you hit the play button, instead it is pausing.

You are setting OnClickListener of play button twice.
So when you click the play button it will pause the media player instead of playing it.
change it to following:
Button bause = (Button) findViewById(R.id.bause);
bause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
mp.pause();
}
});

This has to do with method scopes. You're creating the MediaPlayer instance inside the onCreate method. When that method ends, the mp variable goes away as well since it was created in the method's scope. When the onClickListener is executed, the variable doesn't exist anymore. Move the variable declaration to the class' scope, so that it remains available for the lifetime of the class. Something like:
public class AudioActivity extends AppCompatActivity {
private MediaPlayer mp;
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
//...
}

Related

Why doubleclick on a custom button crashes an android app

I implemented a custom button and added a task to it with delay so it shows the animation.
When I double click it, it crashes. I want to make so it's only clickable once.
i have tried setEnabled(false);
i have tried setClickable(false);
i tried a variable that check if a button has been clicked and disables it.
public class Login extends AppCompatActivity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
}
}
As I wrote, I want that if the button has been clicked that it becomes unclickable.
Try this:
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
...
Explanation:
Your enable/disable block is outside of the onClick listener and it's in the onCreate method: in this way you call setEnable() method only when the activity was created.
But some time setEnable() can not work in case of very rapid click like explained in the second response here. In that case you can use a timer to check the elapsed time.
By the way, I think that your app crash because you don't handle in the right way the Handler. I suggest you also to add:
#Override
protected void onStop(){
handler.removeCallbacksAndMessages(null);
}
Try...
view.setOnClickListener(null);
...after your click event.

How to stop playing sounds on button click to start another sound file?

I have two buttons and two songs. If I click the first button, the first sound plays. But if I click the second button while the first sound is playing the second sound starts playing too.
How can I stop other sounds?
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer johnCenaPlayer = MediaPlayer.create(this, R.raw.john_cena);
Button johnCenaButton = (Button) findViewById(R.id.john_cena_button);
johnCenaButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
johnCenaPlayer.start();
}
});
final MediaPlayer haGayPlayer = MediaPlayer.create(this, R.id.ha_gay_button);
Button haGayButton = (Button) findViewById(R.id.ha_gay_button);
haGayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
haGayPlayer.start();
}
});
}
Stop the other MediaPlayer in clickListener using stop() method.
public void onClick(View view) {
ha_gay.stop()
john_cena.start();
}
If you have many audio files use a single MediaPlayer and change the sources dynamically.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mediaPlayer = new MediaPlayer();
Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
john_cena_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stopAndPlay(R.raw.john_cena, mediaPlayer);
}
});
Button ha_gay_button = (Button) findViewById(R.id.ha_gay_button);
ha_gay_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stopAndPlay(R.raw.ha_gay, mediaPlayer);
}
});
}
// This resets the mediaPlayer and starts the given audio
private void stopAndPlay(int rawId, MediaPlayer mediaPlayer) {
mediaPlayer.reset();
AssetFileDescriptor afd = this.getResources().openRawResourceFd(rawId);
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
}
Try this one. Do it same for the other.
final MediaPlayer john_cena = MediaPlayer.create(this, R.raw.john_cena);
Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
john_cena_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (he_gay.isPlaying()){
he_gay.stop();
}
else if (john_cena.isPlaying()){
john_cena.seekTo(0);
} else{
john_cena.start();
}
}
});

2 buttons in my android layout, only 1 will work

In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}

How to play and stop an mp3 file in an android app

I have created an app in eclipse to play and stop an mp3 file. Everything is just fine except that when I play the audio file and stop it and I want to replay it, the play btn does not wort! I was wondering if anybody can help me? Thanks in advance.
Here comes the code:
package ir.polyglotcenter.childrenmostfrequentwords;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Related to the media player
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.stop();
}
});
}
}
if you want to start /stop sound on Button Click then use MediaPlayer.start() and MediaPlayer.pause() to pause currently playing sound which start again on start button click . and override Activity onPause to finally stop and free MediaPlayer. make changes in your code as :
onStop Button Click :
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause(); //>>pause current sound
myMediaPlayer.seekTo(0); //>> seek to start it again
}
});
and override onPause method of Activity :
#Override
protected void onPause{
super.onPause();
myMediaPlayer.stop(); //>>> stop myMediaPlayer
myMediaPlayer.release(); //>>> free myMediaPlayer
}
Try this (just define Media Player in public area)
public class Main extends Activity {
MediaPlayer myMediaPlayer = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Related to the media player
myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
}
}
the Point is implement MediaPlayer in the loop of if or any loop
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.buttonPlay:
//mp.reset();
mp=MediaPlayer.create(Ur Context.this, R.raw."ursong.mp3");
mp.start();
break;
case R.id.buttonStop:
mp.stop();
break;
}
}
You must move this code :
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
in :
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this,R.raw.audio);
myMediaPlayer.start();
}
});

NullPointerException in Java Android App MediaPlayer

HELP please this is just a simple android app I am developing, it's meant to play a sound every time you click the button....it works when I click the button at a slow pace, but always crashes if I click the button at a fast pace due to a runtime error - NullPointerException!.....I don't know what I am doing wrong.
THIS IS MY CODE
public class Main extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button e = (Button) findViewById(R.id.Button1);
e.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==R.id.imageButton1){
final MediaPlayer i = MediaPlayer.create(Main.this, R.raw.sound);
i.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
i.release();
}
});
i.start();
}
}
}
Its only a guess but in the documentation here you will find the following
public static MediaPlayer create (Context context, int resid)
...
Returns a MediaPlayer object, or null if creation failed
This means you should make sure that the line
final MediaPlayer i = MediaPlayer.create(Main.this, R.raw.sound);
does not return a null.
Edit:
Im really not sure how to handle the situation if create(Context context, int resid) is failing. But you have to do, because it is even from the documentation very clear that it can happen.
To be sure this is really the problem just ignore this case and return from your handler.
For example...
public void onClick(View v)
{
if(v.getId() == R.id.imageButton1)
{
final MediaPlayer i = MediaPlayer.create(Main.this, R.raw.sound);
if (i != null)
{
...
i.start();
}
}
}
Try it this way....
public class Blah extends Activity implements OnClickListener {
MediaPlayer mp;
#Override
public void onCreate(Bundle b)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button e = (Button) findViewById(R.id.Button1);
mp = MediaPlayer.create(R.raw.match);
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
i.release();
}
});
e.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mp.seekTo(0);
mp.start();
}
});
}
}
I had the same problem
my solution was to re-encode the mp3 files with the LAME mp3 encoder
You can use this program: http://www.freac.org/index.php/en/downloads-mainmenu-33
apparently android is quite picky about mp3 codecs

Categories

Resources