Android 按钮声音有问题
当我启动我的应用程序时,主菜单会出现,当我点击两个按钮中的任何一个时,按钮会发出声音,然后进入指定的布局。我遇到的问题是,如果我从布局返回到主屏幕,并尝试再次点击按钮,它仍然会将我带到布局,但不会使按钮发出声音。有什么想法吗?谢谢你的帮助 DragonFutureActivity.javaAndroid 按钮声音有问题,android,eclipse,Android,Eclipse,当我启动我的应用程序时,主菜单会出现,当我点击两个按钮中的任何一个时,按钮会发出声音,然后进入指定的布局。我遇到的问题是,如果我从布局返回到主屏幕,并尝试再次点击按钮,它仍然会将我带到布局,但不会使按钮发出声音。有什么想法吗?谢谢你的帮助 DragonFutureActivity.java package com.Dragon_Fruit; import android.app.Activity; import android.content.Intent; import android.me
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));
}
});
}
}
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));
}
});
}
}
更新的DragonFutureActivity.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));
}
});
}
}
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:错误/配置文件VideoFrameDrops5764:PVMediaOutputNodePort::早期0帧::实时0帧::晚期0帧::总共3帧
08-12 21:29:54.379:错误/配置文件VideoFrameDrops5764:PVMediaOutputNodePort::早期0.000000%::实时19518016.000000%::晚期-0.000000%
08-12 21:29:58.652:错误/配置文件VideoFrameDrops5764:PVMediaOutputNodePort::早期0帧::实时0帧::晚期0帧::总共3帧
08-12 21:29:58.652:错误/配置文件VideoFrameDrops5764:PVMediaOutputNodePort::早期0.000000%::实时19518016.000000%::晚期-0.000000%
08-12 21:30:01.082:错误/配置文件VideoFrameDrops5764:PVMediaOutputNodePort::早期0帧::实时0帧::晚期0帧::总共3帧
08-12 21:30:01.082:ERROR/ProfileVideoFrameDrops5764:PVMediaOutputNodePort::Early 0.000000%::OnTime 19518016.000000%::Late-0.000000%编辑:尝试以下操作:
if(buttonSound.isPlaying()) {
buttonSound.stop();
}
try {
buttonSound.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
buttonSound.start();
好吧,这似乎在几分钟内奏效,但现在又回到了起点。我把我的新密码放上去了。它看起来对吗?当你按下按钮时,你能把你得到的东西放进LogCat吗?我想象每当声音不播放时就会出现堆栈跟踪。我编辑了代码来处理已经播放的声音。再试一次。这也很有效,但我刚刚想出了一些办法。当我认为它没有发出声音时,实际上是这样。真的很安静。如果我听清楚了,我就能听到。你可以尝试一下MediaPlayer的方法,但是你可能想开始一个新的问题,因为这个问题与原来的问题大不相同。