当我启动我的应用程序时,主菜单出现,当我单击2个按钮中的任何一个时,它会使按钮发声,然后转到指定的布局。我遇到的问题是,如果我从布局返回到主屏幕并尝试再次按下按钮,它仍然会将我带到布局但不会使按钮发出声音。有任何想法吗?谢谢你的帮助。
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
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));
}
});
}
}
更新了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 ::早期0.000000%:: OnTime 19518016.000000%:: Late -0.000000% 08-12 21:29:58.652:ERROR / ProfileVideoFrameDrops(5764):PVMediaOutputNodePort ::早期0帧:: OnTime 0帧::延迟0帧::总共3帧 08-12 21:29:58.652:ERROR / ProfileVideoFrameDrops(5764):PVMediaOutputNodePort ::早期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 ::早期0.000000%:: OnTime 19518016.000000%:: Late -0.000000%
答案 0 :(得分:0)
编辑:试试这个:
if(buttonSound.isPlaying()) {
buttonSound.stop();
}
try {
buttonSound.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
buttonSound.start();