我无法删除这两个前两个值,我正在从res文件夹中读取mp3文件并在此处显示。最后三个文件是歌曲的名称,但不需要前两个。帮助我如何删除它们。这是我的代码,因为我正在从res读取值并向ListView显示然后播放该音频:
public class MainActivity extends AppCompatActivity {
ArrayList<String> songNames;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.Mylistview);
songNames = new ArrayList<String>();
final ArrayList<String> list = new ArrayList<String>();
Field[] fields = R.raw.class.getFields();
list.clear();
for(Field f : fields)
try {
list.add(f.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
// songNames.add(getResources().getClass(R.raw.song1));
// songNames.add(getResources().getResourceEntryName(R.raw.song2));
// songNames.add(getResources().getResourceEntryName(R.raw.song3));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Uri mp3 = Uri.parse("android.resource://"
+ getApplicationContext().getPackageName() + "/raw/"
+ list.get(position));
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(),mp3);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare(); // might take long! (for buffering, etc)
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(onCompletionListener);
}
});
}
private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.release();
mediaPlayer = null;
}
};
}
答案 0 :(得分:0)
而不是for (Field f : fields)
,只需写下:
for (int i = 2; i < fields.length; i++) {
Field f = fields[i];
在try-catch块之后添加一个结束的大括号}
。