Problem with Button Sound
When I start up my app the main menu comes up and when I click either of the 2 buttons it makes the button sound and then goes to the designated layout. The problem I'm having is that if I go back from the layout to the main screen and try to hit the buttons again it still takes me to the layout but without making the button sound. Any ideas? Thanks for the help.
DragonFruitActivity.java
package com.Dragon_Fruit;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ***BUTTON SOUND***//
final MediaPlayer buttonSound = MediaPlayer.create(
DragonFruitActivity.this, R.raw.button_click);
ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton);
playbutton.setOnClickListener(new View.OnClickListener() {
@Override
开发者_如何学C public void onClick(View arg0) {
arg0.setBackgroundResource(R.drawable.playbuttonselected);
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent(DragonFruitActivity.this,
playbutton.class));
}
});
ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton);
settingsbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent(DragonFruitActivity.this,
settingsbutton.class));
}
});
}
}
Updated DragonFruitActivity.java
package com.Dragon_Fruit;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ***BUTTON SOUND***//
final MediaPlayer buttonSound = MediaPlayer.create(
DragonFruitActivity.this, R.raw.button_click);
ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton);
playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
arg0.setBackgroundResource(R.drawable.playbuttonselected);
// TODO Auto-generated method stub
try {
buttonSound.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buttonSound.start();
startActivity(new Intent(DragonFruitActivity.this,
playbutton.class));
}
});
ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton);
settingsbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
buttonSound.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buttonSound.start();
startActivity(new Intent(DragonFruitActivity.this,
settingsbutton.class));
}
});
}
}
LogCat
08-12 21:29:54.379: ERROR/ProfileVideoFrameDrops(5764): PVMediaOutputNodePort :: Early 0 frames :: OnTime 0 frames :: Late 0 frames :: Total 3 frames 08-12 21:29:54.379: ERROR/ProfileVideoFrameDrops(5764): PVMediaOutputNodePort :: Early 0.000000 % :: OnTime 19518016.000000 % :: Late -0.000000 % 08-12 21:29:58.652: ERROR/ProfileVideoFrameDrops(5764): PVMediaOutputNodePort :: Early 0 frames :: OnTime 0 frames :: Late 0 frames :: Total 3 frames 08-12 21:29:58.652: ERROR/ProfileVideoFrameDrops(5764): PVMediaOutputNodePort :: Early 0.000000 % :: OnTime 19518016.000000 % :: Late -0.000000 % 08-12 21:30:01.082: ERROR/ProfileVideoFrameDrops(5764): PVMediaOutputNodePort :: Early 0 frames :: OnTime 0 frames :: Late 0 frames :: Total 3 frames 08-12 21:30:01.082: ERROR/ProfileVideoFrameDrops(5764): PVMediaOutputNodePort :: Early 0.000000 % :: OnTime 19518016.000000 % :: Late -0.000000 %
EDIT: Try this:
if(buttonSound.isPlaying()) {
buttonSound.stop();
}
try {
buttonSound.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
buttonSound.start();
(check out Google MediaPlayer state diagram)
精彩评论