mediaplayer R文件并发症

时间:2014-11-18 14:18:48

标签: java android

我想让它发挥作用

MediaPlayer[] playword = new MediaPlayer[];//Variable must provide either dimension expressions or an array initializer ,on eclipse a big X
 for(int i=0;i<2;i++){
     playword[i] = MediaPlayer.create(this, getResources().getIdentifier("word" + Integer.toString(i), "raw", getPackageName()));
     playword[i].setAudioStreamType(AudioManager.STREAM_MUSIC);
 }

但是我在R文件中出错了

我在数字之前添加了一个单词

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你想做这样的事情:

MediaPlayer[] playword;
  for(int i=0;i<2;i++){

    int tmpSoundIdentifier = this.getResources().getIdentifier("sound_" + i, "raw", this.getPackageName());

    playword[i] = MediaPlayer.create(this, tmpSoundIdentifier);
    playword[i].setAudioStreamType(AudioManager.STREAM_MUSIC);
  }

答案 1 :(得分:0)

你应该替换

MediaPlayer.create(this, R.raw.Integer.toString(i));

MediaPlayer.create(this, getResources().getIdentifier("word" + Integer.toString(i), "raw", getPackageName()));

这将在res/raw文件夹中找到带编号的文件,例如word1word2等。

此外,您的MediaPlayer数组未初始化,以下内容应该可以正常工作

int NUMBER_OF_SOUNDS = 3; // Number of Sounds
MediaPlayer[] playword = new MediaPlayer[NUMBER_OF_SOUNDS];
 for(int i=0;i < NUMBER_OF_SOUNDS; i++){
     playword[i] = MediaPlayer.create(this, getResources().getIdentifier("word" + Integer.toString(i), "raw", getPackageName()));
     playword[i].setAudioStreamType(AudioManager.STREAM_MUSIC);
 }

我不确定您使用MediaPlayer个数组的原因,但如果您觉得这是必需的,我会使用List<MediaPlayer>代替MediaPlayer[]