当 EditText 更改时播放声音 - android

发布于 2024-10-22 11:00:33 字数 1287 浏览 2 评论 0原文


我希望我的应用程序在用户在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2024-10-29 11:00:33

(我是 Android 开发的新手,所以认为弄清楚这一点很有趣。)

我发现 关于调用 release() 的内容:“一旦 MediaPlayer 对象处于 End 状态,就无法再使用它,并且无法将其恢复到任何其他状态状态。”因此出现了非法状态异常(我能够重现)。

正确的组合似乎是调用 stop() 然后调用 prepare()

public void onCompletion(MediaPlayer mp) {
    mp.stop();
    try
    {
        mp.prepare();
    }
    catch( IOException ie )
    {
        throw new RuntimeException( ie ) ;
    }
}

(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 then prepare():

public void onCompletion(MediaPlayer mp) {
    mp.stop();
    try
    {
        mp.prepare();
    }
    catch( IOException ie )
    {
        throw new RuntimeException( ie ) ;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文