我試圖播放音頻once
Enter
關鍵事件發生在EditText
。媒體播放器總是循環不停止
但媒體播放器播放looping
音頻和never stop
。並且Media Player
沒有撥打setOnCompletionListener
方法。
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
//Toast.makeText(LoginActivity.this, editText.getText(), Toast.LENGTH_SHORT).show();
try {
MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor afd = getAssets().openFd("oxygen.ogg");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.setLooping(false);
mp.prepare();
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.i("ONComplete Media palyer", "onComplete hit");
mp.stop();
mp.release();
}
});
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
});