Button Sound Volume Variation?
Hey guys so when I run my app it goes to the main screen. From there I hit one of my two image buttons and a sound plays. The problem I'm having is that when i first push the button the sound is normal and loud and the button activity takes me to the next layout. If I go back to the main screen from the layout and try to hit the button again i can still hear the sound but just barely. It is almost inaudible sometimes. How would I get the sound to be consistent at all times? Thanks!
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) findVi开发者_C百科ewById(R.id.playbutton);
playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
arg0.setBackgroundResource(R.drawable.playbuttonselected);
// TODO Auto-generated method stub
if(buttonSound.isPlaying()) {
buttonSound.stop();
}
try {
buttonSound.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
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
if(buttonSound.isPlaying()) {
buttonSound.stop();
}
try {
buttonSound.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
buttonSound.start();
startActivity(new Intent(DragonFruitActivity.this,
settingsbutton.class));
}
});
}
}
How long is the sound? If is is really short (<250ms), I've seen an issue on some phones (Motorola Atrix) where the audio driver goes to sleep if no sounds have played in a while. In this case, really short sounds are either not played or are really quiet when you first play them because the audio driver is still waking up.
My fix for this, which I'm not happy about at all, is to pad the front of the sound with a few milliseconds of low frequency sound (~20 Hz).
You can verify if this is the issue you are seeing by playing your sound 2-3 times in quick succession when you press the button. If it is louder subsequent times, then this is probably the problem.
You might also want to consider using a SoundPool
for playing small sounds. It loads the sound into memory so that it can be played quicker.
精彩评论