Carla Cosey
Lurker
Hello, I'm getting no errors. My app contains two buttons, where each, when clicked plays a different sound located in the raw folder. The first button plays, but the second button does not.
Below is the updated code for two buttons that plays a sound onclick, it now works. I hope this helps! thank you LV426, for your help.
package com.example.student.blessyou;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
AdView adView;
Button btn_bless_you;
Button buttonThankYou;
private MediaPlayer mp;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonThankYou = (Button) findViewById(R.id.buttonThankYou);
btn_bless_you = (Button) findViewById(R.id.btn_bless_you);
btn_bless_you.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(MainActivity.this,R.raw.bless_you);
mp.start();
}
});
buttonThankYou.setOnClickListener(new View.OnClickListener() {
@override
public void onClick (View v){
stopPlaying();
mp = MediaPlayer.create(MainActivity.this, R.raw.thank_you);
mp.start();
}
});
}
private void stopPlaying() {
if (mp !=null){
mp.stop();
mp.release();
mp = null;
}
adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
adView.loadAd(adRequest);
}
}
Below is the updated code for two buttons that plays a sound onclick, it now works. I hope this helps! thank you LV426, for your help.
package com.example.student.blessyou;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
AdView adView;
Button btn_bless_you;
Button buttonThankYou;
private MediaPlayer mp;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonThankYou = (Button) findViewById(R.id.buttonThankYou);
btn_bless_you = (Button) findViewById(R.id.btn_bless_you);
btn_bless_you.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(MainActivity.this,R.raw.bless_you);
mp.start();
}
});
buttonThankYou.setOnClickListener(new View.OnClickListener() {
@override
public void onClick (View v){
stopPlaying();
mp = MediaPlayer.create(MainActivity.this, R.raw.thank_you);
mp.start();
}
});
}
private void stopPlaying() {
if (mp !=null){
mp.stop();
mp.release();
mp = null;
}
adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
adView.loadAd(adRequest);
}
}
Last edited: