Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 媒体播放器工作不正常_Java_Android - Fatal编程技术网

Java 媒体播放器工作不正常

Java 媒体播放器工作不正常,java,android,Java,Android,我正在制作一个应用程序,你可以在其中按按钮播放声音。起初它似乎可以工作,但按5-6下后,它就停止播放声音。这是我的密码 public class MainActivity extends ActionBarActivity { private ImageButton pad1, pad2, pad3, pad4, pad5, pad6, pad7, pad8, pad9, pad10, pad11, pad12; @Override protected void onCreate(Bund

我正在制作一个应用程序,你可以在其中按按钮播放声音。起初它似乎可以工作,但按5-6下后,它就停止播放声音。这是我的密码

public class MainActivity extends ActionBarActivity {


private ImageButton pad1, pad2, pad3, pad4, pad5, pad6, pad7, pad8, pad9, pad10, pad11, pad12;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pad1 = (ImageButton) findViewById(R.id.pad1);
    pad2 = (ImageButton) findViewById(R.id.pad2);
    pad3 = (ImageButton) findViewById(R.id.pad3);
    pad4 = (ImageButton) findViewById(R.id.pad4);
    pad5 = (ImageButton) findViewById(R.id.pad5);
    pad6 = (ImageButton) findViewById(R.id.pad6);
    pad7 = (ImageButton) findViewById(R.id.pad7);
    pad8 = (ImageButton) findViewById(R.id.pad8);
    pad9 = (ImageButton) findViewById(R.id.pad9);
    pad10 = (ImageButton) findViewById(R.id.pad10);
    pad11 = (ImageButton) findViewById(R.id.pad11);
    pad12 = (ImageButton) findViewById(R.id.pad12);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}



    public void onPad1Click(View view) {
        MediaPlayer mp = MediaPlayer.create(this, R.raw.p1);
        mp.start();
    }

public void onPad2Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p2);
    mp.start();
}

public void onPad3Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p3);
    mp.start();

}

public void onPad4Click(View view) {

    MediaPlayer mp = MediaPlayer.create(this, R.raw.p4);
    mp.start();
}

public void onPad5Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p5);
    mp.start();
}

public void onPad6Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p6);
    mp.start();
}

public void onPad7Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p7);
    mp.start();
}

public void onPad8Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p8);
    mp.start();
}

public void onPad9Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p9);
    mp.start();
}

public void onPad10Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p10);
    mp.start();
}

 public void onPad11Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p11);
     mp.start();
}

 public void onPad12Click(View view) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.p12);
   mp.start();
}
}

我不知道可能是什么声音很短,所以我不认为这是问题所在。我希望有人能帮助解决这个问题。谢谢大家!

我建议使用SoundPool而不是MediaPlayer进行短时间播放

添加以下变量:

SoundPool soundPool = null;
int sound1_id;
int sound2_id;
添加到onCreate()-方法(而不是MediaPlayer):

在特定的onClick()处:


可能与我添加的mp.release()重复;在mp.start()之后;但现在它甚至不播放声音。知道吗?祝你好运。它的性能也比MediaPlayer好。非常感谢!!成功了。我还有一个问题与此无关,如果通过某种方式你知道如何解决它,那将是非常棒的。因此,我需要根据不同的屏幕大小调整按钮的大小,并在每个按钮中加入安卓:weight,这适用于宽度,但不适用于高度。如果你知道我会非常感激的
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
sound1_id = soundPool.load(this, R.raw.p1, 1);
sound2_id = soundPool.load(this, R.raw.p2, 1);
soundPool.play(sound1_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);