我正在创建钢琴应用程序但是当我点击按钮多次播放时我遇到了问题当我再次点击按钮时我听不到声音。我多次点击按钮后,听不到所有声音如何解决这个问题?
public class Music_piano extends Activity {
Button btn_s_ddo,btn_s_re,btn_s_mi,btn_s_fa,btn_s_so,btn_s_la,btn_s_si,btn_ldo;
MediaPlayer mp = new MediaPlayer();
MediaPlayer mp1,mp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_music_piano);
}
public void btn_sound_ddo(View v){
mp = MediaPlayer.create(this, R.raw.ddo);
mp.start();
}
public void btn_sound_re(View v){
mp = MediaPlayer.create(this, R.raw.re);
mp.start();
}
public void btn_sound_mi(View v){
mp = MediaPlayer.create(this, R.raw.mi);
mp.start();
}
public void btn_sound_fa(View v){
mp = MediaPlayer.create(this, R.raw.fa);
mp.start();
}
public void btn_sound_so(View v){
mp = MediaPlayer.create(this, R.raw.sol);
mp.start();
}
public void btn_sound_la(View v){
mp = MediaPlayer.create(this, R.raw.la);
mp.start();
}
public void btn_sound_si(View v){
mp = MediaPlayer.create(this, R.raw.si);
mp.start();
}
public void btn_sound_ldo(View v){
mp = MediaPlayer.create(this, R.raw.ddo_last);
mp.start();
}
}
答案 0 :(得分:0)
你可以试试下面的代码吗?我试过了,对我来说效果很好。
Button btnOne = (Button)findViewById(R.id.btnOne);
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MediaPlayer mPlayer = MediaPlayer.create(TempActivity.this, R.raw.mysoundfile);
mPlayer.start();
}
});
希望它会对你有所帮助。
答案 1 :(得分:0)
最好创建MediaPlayer
的单个实例并重用它
MediaPlayer mMediaPlayer = new MediaPlayer(); // global variable
//write below code on buttons onclick method
AssetFileDescriptor assetFile = context.getAssets().openFd(audioFile);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(assetFile.getFileDescriptor(),
assetFile.getStartOffset(), assetFile.getLength());
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
答案 2 :(得分:0)
我用一个演示活动编写了一个简单的SoundManager类。没有任何评论,因为我觉得它很容易阅读。有关详细信息,请参阅SoundPool。
SoundManager
上课
package com.makeez.sfx;
import android.content.Context;
import android.media.SoundPool;
import java.util.HashMap;
import java.util.Map;
public class SoundManager {
private final Map<Integer, Integer> sfxId = new HashMap<>();
private SoundPool sfx;
public SoundManager(int maxStreams, int streamType, int srcQuality) {
sfx = new SoundPool(maxStreams, streamType, srcQuality);
}
public void load(Context context, int... resources) {
if (context == null) {
throw new NullPointerException("Context must not be null.");
}
if (resources == null) {
return;
}
for (int resource : resources) {
int soundId = sfx.load(context, resource, 1);
sfxId.put(resource, soundId);
}
}
public void play(int resId) {
Integer soundId = sfxId.get(resId);
if (soundId != null) {
sfx.play(soundId, 1.0f, 1.0f, 0, 0, 1.0f);
}
}
public void release() {
if (sfx != null) {
sfx.autoPause();
sfx.release();
sfx = null;
}
}
}
演示活动
package com.makeez.sfx;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private SoundManager manager;
private int position = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = new SoundManager(1, AudioManager.STREAM_MUSIC, 0);
manager.load(this, R.raw.sfx_0001, R.raw.sfx_0002, R.raw.sfx_0003, R.raw.sfx_0004, R.raw.sfx_0005);
Button button = (Button) findViewById(R.id.play);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (position) {
case 0: manager.play(R.raw.sfx_0001); break;
case 1: manager.play(R.raw.sfx_0002); break;
case 2: manager.play(R.raw.sfx_0003); break;
case 3: manager.play(R.raw.sfx_0004); break;
case 4: manager.play(R.raw.sfx_0005); break;
}
if (++position >= 5) {
position = 0;
}
}
});
}
@Override
protected void onDestroy() {
manager.release();
super.onDestroy();
}
}