在按键上播放自定义声音/在EditText内实现?

时间:2015-09-12 12:58:51

标签: android android-edittext android-mediaplayer

当用户在编辑文本内部输入时,我尝试播放自定义MP3声音,我为此创建了简单项目并将click.mp3放在res/row文件夹中,但问题是我听不到但是,当我实现onClickListener(用于调试)时,我听到了声音。

这是我的java代码:

import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity implements View.OnKeyListener{

    MediaPlayer mediaPlayer;
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer = MediaPlayer.create(this, R.raw.click);
        editText = (EditText) findViewById(R.id.editText);
        editText.setOnKeyListener(this);
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        mediaPlayer.setLooping(false);
        mediaPlayer.start();
        return true;
    }
}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您好请尝试以下代码希望它能帮到您

将以下代码放在主要活动中

 MediaPlayer m_player;
EditText editText;

editText = (EditText) findViewById(R.id.editText);

        editText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                playSound("beep_new.mp3");
            }

            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });


         public void playSound(final String p_fileName)
    {
        try
        {

            if (m_player != null && m_player.isPlaying())
            {
                m_player.stop();
                m_player.release();
                m_player = null;
            }
            else
            {
                m_player.release();
            }

            m_player = new MediaPlayer();
            m_player.reset();
            AssetFileDescriptor m_descriptor = getAssets().openFd(p_fileName);
            m_player.setDataSource(m_descriptor.getFileDescriptor(), m_descriptor.getStartOffset(), m_descriptor.getLength());
            m_descriptor.close();

            m_player.prepare();
            m_player.setVolume(1f, 1f);
            m_player.setLooping(false);

           m_player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
               @Override
               public void onCompletion(MediaPlayer mp) {

               }
           });
            m_player.start();
        }
        catch (Exception e)
        {
           e.printStackTrace();
        }
    }

    /**
     * This method is use to stop media player
     */
    private void stopMediaPlayer()
    {
        if (m_player != null)
        {
            if (m_player.isPlaying())
            {
                m_player.stop();
                m_player.release();
                m_player = null;
            }
        }
    }

beep_new.mp3是我的文件,位于资产文件夹