My code won't run? - android

I am working with Fragments however this code wont run. can someone help me with this. The problem in the code seems to be with this part "(start.this,".
public class frag extends FragmentActivity {
public MediaPlayer mp = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(start.this, R.raw.praise1);
mp.start();
}else {
mp.stop();
mp = null;
} }
});
}
}

Change your code to
public class frag extends FragmentActivity {
public MediaPlayer mp = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(getApplicationContext(), R.raw.praise1);//==> Here is the change
mp.start();
}else {
mp.stop();
mp = null;
} }
});
}
}

Change start.this to an actual context. You could use getParent() or you could store the context before creating the onClickListener and pass it in:
setContentView(R.layout.activity_main);
final Context context = this;
//inside the onClickListener
mp = MediaPlayer.create(context, R.raw.praise1);

Related

How to make ImageButton repeat a sound each time it's clicked

I am trying to get the Image Button to play a sound each time it's clicked. For example, if the sound is playing for few seconds and I press the button again it should start from the beginning. I managed to get the sound working but it's not repeating it. How can I get it to start over?
ImageButton bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
}
Try this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, R.raw.bird);
}
mp.start();
} catch(Exception e) {
e.printStackTrace();
}
}
Use MediaPlayer.seekto() to seek to the start of the audio file.
ImageButton bt;
private MediaPlayer mp = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (ImageButton) findViewById(R.id.clickme);
mp = MediaPlayer.create(this, R.raw.bird);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.seekTo(0);
} else {
mp.start();
}
}
});
}

Stop and Replay an audio file

What i am trying to accomplish is I stopped the music, then when I press the play button again, the media player will play it.
But the problem I facing now is after I pressed the stop button, I can't play the music again.
Here is my code:
Button play,pause,stop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_beginagain);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
final MediaPlayer sound = MediaPlayer.create(Audio_BeginAgain.this, R.raw.beginagain);
final MediaPlayer mp = new MediaPlayer();
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound.stop();
}
});
}
You can simply add a completion listener and then use mediaPlayer.seekto(0) and start():
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
});
}
It's exactly what Md Abdul Gafur said..
Each time you want re-play the MediaPlayer you must initialize it new with create().
Look at this code:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
final TextView t = (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
Code copied from: Android MediaPlayer Stop and Play

MediaPlayer mp.stop() not stopping

I have a music which I want it to be as a background music for the game I made.
I want it to stop when score becomes empty in order to start another one.
But it does not stop by using mp.stop(); !
My code:
public class countgame extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countgame);
final MediaPlayer mp = MediaPlayer.create(countgame.this, R.raw.game);
mp.start();
final ImageView i1 = (ImageView)findViewById(R.id.imageView1);
final ImageView i2 = (ImageView)findViewById(R.id.imageView2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
incorrect();
}
});
}
public void incorrect(){
final ImageView score = (ImageView) findViewById(R.id.i3);
if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score3).getConstantState())){
score.setBackgroundResource(R.drawable.score2);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score2).getConstantState())){
score.setBackgroundResource(R.drawable.score1);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score1).getConstantState())){
score.setBackgroundResource(R.drawable.empty);
MediaPlayer m = MediaPlayer.create(countgame.this, R.raw.game);
m.stop();//here is the problem
MediaPlayer mp=MediaPlayer.create(countgame.this, R.raw.gameover);
mp.start();
}
}
Change your code to this:
public class countgame extends Activity implements OnClickListener{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countgame);
mp = MediaPlayer.create(countgame.this, R.raw.game);
mp.start();
final ImageView i1 = (ImageView)findViewById(R.id.imageView1);
final ImageView i2 = (ImageView)findViewById(R.id.imageView2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
incorrect();
}
});
}
public void incorrect(){
final ImageView score = (ImageView) findViewById(R.id.i3);
if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score3).getConstantState())){
score.setBackgroundResource(R.drawable.score2);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score2).getConstantState())){
score.setBackgroundResource(R.drawable.score1);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score1).getConstantState())){
score.setBackgroundResource(R.drawable.empty);
mp.stop();//here is the problem
mp=MediaPlayer.create(countgame.this, R.raw.gameover);
mp.start();
}
}
}

Is this a good way to use MediaPlayer?

I think this is a decent way to use MediaPlayer for a Button or one time use. Am I right? Is the try block necessary? And what should I be trying to catch here? I'm really having a hard time finding a rock solid way to play a sound once.
// Button sound
private void playButtonSound() {
try{
final MediaPlayer startPlayer = MediaPlayer.create(
getApplicationContext(), R.raw.sound);
startPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
startPlayer.release();
}
});
startPlayer.start();
} catch(Throwable t){}
}
I use it like that:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.tosse);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}

how do i stop background music when back button is pressed?

i have 2 activities. and 2 different background music plays on both. when i press a button on the first activity it will go to this activity and the changing of music goes smoothly but i want to stop background music when back button is pressed. i tried this but it force closes. is there any other way to do this?
public class Categories extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
mp = MediaPlayer.create(Categories.this, R.raw.looney);
mp.setLooping(true);
mp.start();
Button cartoonButton = (Button) findViewById(R.id.cartoon);
cartoonButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchCartoon = new Intent(Categories.this, Cartoon.class);
startActivity(launchCartoon);
}
});
Button superButton = (Button) findViewById(R.id.hero);
superButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent launchSuperheroes = new Intent(Categories.this, SuperHeroes.class);
startActivity(launchSuperheroes);
}
});
Button singerButton = (Button) findViewById(R.id.singer);
singerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchSinger = new Intent(Categories.this, Singer.class);
startActivity(launchSinger);
}
});
Button famousButton = (Button) findViewById(R.id.famous);
famousButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchFamous = new Intent(Categories.this, Famous.class);
startActivity(launchFamous);
}
});
}
#Override
protected void onResume() {
super.onResume();
mp.start();
}
#Override
protected void onPause() {
super.onPause();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
You can try this code:
#Override
public void onBackPressed() {
if (player.isPlaying()) {
player.stop();
}
player.release();
super.onBackPressed();
}
Hope this help you :)

Categories

Resources