我正在尝试播放设备中的歌曲,其中歌曲的部分名称由用户提供。 如果歌曲命名为“demosong.mp3”,如果用户在文本框中搜索“demo”,则必须播放“demosong.mp3”。
我的代码在这里:
fileList=getfile(new File(Environment.getExternalStorageDirectory()
.getAbsolutePath()));
String
ss=fileList.toString();
chk="Song Name i.e.demo";
String direc,totpath,root1;
root1=file21;
direc="";
name="";
String fla="True";
for (int i = 0; i < fileList.size(); i++) {
TextView textView = new TextView(this);
if (fileList.get(i).isDirectory()) {
direc="";
direc=fileList.get(i).getName();
//textView.setTextColor(Color.parseColor("#FF0000"));
}
else
{
name=direc+"//"+fileList.get(i).getName();
String name1=name.toLowerCase();
if(name1.contains(chk))
{
String pth,nnn;
Uri myUri11 = Uri.parse(pth);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try
{
mPlayer.setDataSource(getApplicationContext(),myUri11);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e)
{
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
break;
}
else if(r[0].compareTo("stop")==0){
mPlayer.stop();
mPlayer.release();
}
else if(r[0].compareTo("pause")==0){
mPlayer.pause();
mPlayer.release();
}
}
}
}
}
catch (Exception e) {
// TODO: handle exception
}
}
检查文件扩展的代码
public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
fileList.add(listFile[i]);
getfile(listFile[i]);
} else {
if ( listFile[i].getName().endsWith(".mp3"))
{
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}
}
感谢