Android 在安卓系统中点击按钮时如何播放声音?
我试图播放一个声音文件时,一个按钮被点击,但不断得到一个错误 错误是:Android 在安卓系统中点击按钮时如何播放声音?,android,Android,我试图播放一个声音文件时,一个按钮被点击,但不断得到一个错误 错误是: "The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new View.OnClickListener(){}, int)" 这是我的密码: @Override public void onClick(View v) { // TODO Auto-generated method
"The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new View.OnClickListener(){}, int)"
这是我的密码:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button zero = (Button)this.findViewById(R.id.btnZero);
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(this, R.raw.mamacita_zero);
}
});
}
任何帮助或提示都将不胜感激。
Thnx 这里发生了一些事情(免责声明,这正是我习惯使用它的方式,可能有更好的方法):
- 你每次点击所做的工作似乎比你需要的多得多。您正在为活动视图中的每次单击创建并添加一个新的
,而不是按钮。您只需要设置一次侦听器,对于按钮,而不是总体视图;我倾向于在活动的构造函数中这样做onClickListener
- 关于您的错误,当我传递的上下文是覆盖活动时,MediaPlayer对我来说运行良好。当您传递
时,它传递的是您正在创建的this
,并抛出MediaPlayeronClickListener
- 最后,要真正播放声音,必须调用
start()
MediaPlayer
,找到按钮,并附加一个onClickListener
,它将播放刚刚创建的MediaPlayer的声音。它看起来像:
public class MyActivity extends Activity {
public MyActivity(Bundle onSavedStateInstance) {
// eliding some bookkeepping
MediaPlayer mp = MediaPlayer.create(this, R.raw.mamacita_zero);
Button zero = (Button)this.findViewById(R.id.btnZero);
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
}
}
希望有帮助
MediaPlayer mp = MediaPlayer.create(getBaseContext(),
R.raw.yoursoundfile);
mp.start();
声音文件必须位于res/raw文件夹中
这是更好的小声音
-教程我玩过media player,很容易遇到麻烦。我听从了Volodymyr的建议,SoundPool更容易管理 MediaPlayer不喜欢一次播放多个声音,例如,当您的按钮上有许多快速选项卡时。我采用以下方法进行管理:
private void playSound(Uri uri) {
try {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(this, uri);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
// don't care
}
}
在构造函数中,我做了:
mMediaPlayer = new MediaPlayer();
mSoundLess = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.less);
mSoundMore = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.more);
单击后,我将调用playSound(mSoundLess):
相反,我创建了一个声音池助手:
package com.mycompany.myapp.util;
import java.util.HashSet;
import java.util.Set;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundPoolHelper extends SoundPool {
private Set<Integer> mLoaded;
private Context mContext;
public SoundPoolHelper(int maxStreams, Context context) {
this(maxStreams, AudioManager.STREAM_MUSIC, 0, context);
}
public SoundPoolHelper(int maxStreams, int streamType, int srcQuality, Context context) {
super(maxStreams, streamType, srcQuality);
mContext = context;
mLoaded = new HashSet<Integer>();
setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
mLoaded.add(sampleId);
}
});
}
public void play(int soundID) {
AudioManager audioManager = (AudioManager) mContext.getSystemService( Context.AUDIO_SERVICE);
float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (mLoaded.contains(soundID)) {
play(soundID, volume, volume, 1, 0, 1f);
}
}
}
播放这样的声音:
mSoundPoolHelper = new SoundPoolHelper(1, this);
mSoundLessId = mSoundPoolHelper.load(this, R.raw.less, 1);
mSoundMoreId = mSoundPoolHelper.load(this, R.raw.more, 1);
private void playSound(int soundId) {
mSoundPoolHelper.play(soundId);
}
不要忘记调用
mSoundPoolHelper.release()代码>,例如在您的onDestroy()
中。如果您使用MediaPlayer,则需要类似的功能。如果您确实必须以编程方式调用单击,因为视图没有自己的声音,我会这样解决它,这是最简单的解决方案,也是一个简单的解决方案
view.playSoundEffect(SoundEffectConstants.CLICK);
非常简单和有效,如果你想让一个布局播放一个声音,你需要把
android:soundEffectsEnabled="true"
在xml中