Android MediaPlayer 播放指定文件夾
public void PlayList(File file) {
if (file.isFile()) {
if (file.isFile()) {
fileDir = file.getParentFile();
fileName = file.getName();
} else {
fileDir = file;
fileName = null;
}
fileArray = new ArrayList<String>();
fileDir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.endsWith(".mp3") || name.endsWith(".mp4") || name.endsWith(".wav")) {
if ((new File(dir, name)).isFile()) {
fileArray.add(name);
return true;
} else {
return false;
}
} else {
return false;
}
}
});
PlayFile(fileName);
} else {
Log.i(TAG, "nofindthefile...................................................");
}
}
public void PlayFile(final String fileName) {
Log.i(TAG, "FileName" + fileName);
intNum = fileArray.indexOf(fileName);
if (intNum == -1) {
intNum = 0;
}
Uri uri = Uri.fromFile(new File(fileDir, fileArray.get(intNum)));
if (intNum < fileArray.size() - 1) {
intNum++;
} else {
intNum = 0;
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(context, uri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
PlayFile(fileArray.get(intNum));
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
PlayFile(fileArray.get(intNum));
}
});
}
0 件のコメント:
コメントを投稿