当 EditText 更改时播放声音 - android
我希望我的应用程序在用户在 EditText 字段中输入内容时播放声音。希望这会产生一种错觉,即每当按下按键时键盘都会发出声音。
我针对此特定事件的代码如下所示:
package com.textField.android;
public class textField extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittext = (EditText) findViewById(R.id.edittext);
final MediaPlayer mp = MediaPlayer.create(getBaseContext(),
R.raw.pistol);
edittext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}); } 我可以很好地安装并打开应用程序,并且
我可以输入一个字母并播放声音,但是当按第二个字母时,应用程序崩溃。我不知道出了什么问题。旋转我的设备也会播放声音,无论我旋转多少次。因此,当向后旋转时,它会再次播放声音。但按下某个键后,应用程序崩溃而不播放声音。
有人可以帮我解决这个问题吗?我们将非常感激:)
i want my application to play a sound whenever the user makes an input the EditText field. Hopefully this will create the illusion that the keyboard makes the sound whenever a key is pressed.
My code for this specific event lookes like this:
package com.textField.android;
public class textField extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittext = (EditText) findViewById(R.id.edittext);
final MediaPlayer mp = MediaPlayer.create(getBaseContext(),
R.raw.pistol);
edittext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
});
}
}
I can install and open the application just fine, and i can enter ONE letter and it plays the sound, but when pressing a second letter the application crashes. I don't know what's wrong. Rotating my device plays the sound aswell no matter how many times i do it. So when rotating back it plays the sound again. But then pressing a key the application crashes WITHOUT playing the sound.
Can someone help me get this right? That would be very much appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(我是 Android 开发的新手,所以认为弄清楚这一点很有趣。)
我发现 关于调用
release()
的内容:“一旦 MediaPlayer 对象处于 End 状态,就无法再使用它,并且无法将其恢复到任何其他状态状态。”因此出现了非法状态异常(我能够重现)。正确的组合似乎是调用
stop()
然后调用prepare()
:(I'm brand-new to Android development, so thought this interesting to figure out.)
I found this about calling
release()
: "Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state." Hence the illegal state exception (which I was able to reproduce).The right combination instead seems to be to call
stop()
and thenprepare()
: