gpt4 book ai didi

java - Android 中 MediaPlayer 数组导致空指针

转载 作者:行者123 更新时间:2023-12-02 00:38:47 27 4
gpt4 key购买 nike

谁能明白为什么我会用这段代码得到 NullPointerException 吗?该代码基本上是创建一个 MediaPlayer 数组,以便我可以循环遍历它并立即停止/启动所有媒体播放器。它们还链接到搜索栏来控制音量。

private MediaPlayer[] media;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


//media players

media[0] = MediaPlayer.create(this, R.raw.drums);



media[1] = MediaPlayer.create(this, R.raw.bass);


media[2] = MediaPlayer.create(this, R.raw.synth);


media[3] = MediaPlayer.create(this, R.raw.snare);


media[4] = MediaPlayer.create(this, R.raw.wobble);


for(int i=0;i<media.length;i++){
media[i].start();
media[i].setLooping(true);
media[i].setVolume(0,0);
}


//drums seekbar

final SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
volControl.setMax(maxVolume);
volControl.setProgress(0);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {



@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}


@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub

media[0].setVolume(arg1, arg1);

}

});

最佳答案

使用类似

 media=new MediaPlayer[5]; 

然后

media[0] = MediaPlayer.create(this, R.raw.drums);
media[1] = MediaPlayer.create(this, R.raw.bass);
media[2] = MediaPlayer.create(this, R.raw.synth);
media[3] = MediaPlayer.create(this, R.raw.snare);

关于java - Android 中 MediaPlayer 数组导致空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977511/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com