1.扫描sdcard的mp3文件,点击能够播放
2. 扫描到文件显示在listview中
<1> 配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="myapplication.com.sdmp3"> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<2> 布局文件
<ListView android:id="@+id/mListView" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_file" android:layout_width="match_parent" android:layout_height="60dp" android:textColor="#000000" android:gravity="center_vertical" android:text="" /> <TextView android:id="@+id/tc_fileName" android:layout_width="match_parent" android:layout_height="30dp" android:textColor="#000000" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#ff0000"/> </LinearLayout>
<3> .adapter
package myapplication.com.sdmp3; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; import java.util.zip.Inflater; /** * Created by Administrator on 2016/10/11. */ public class adapter extends BaseAdapter { Context context; List<MuiDao> datas; public adapter(Context context, List<MuiDao> datas) { this.context = context; this.datas = datas; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if(convertView==null){ convertView = View.inflate(context, R.layout.sd_list, null); holder = new ViewHolder(); holder.textView1 = (TextView) convertView.findViewById(R.id.tv_file); holder.textView2 = (TextView) convertView.findViewById(R.id.tc_fileName); convertView.setTag(holder); } else{ holder= (ViewHolder) convertView.getTag(); } MuiDao bean=datas.get(position); String str=bean.getAddress(); String name1[]=str.split("-"); holder.textView1.setText(name1[0]); holder.textView2.setText(bean.getAddress()); return convertView; } class ViewHolder{ TextView textView1,textView2; } }
.bean
package myapplication.com.sdmp3; /** * Created by Administrator on 2016/10/11. */ public class MuiDao { public String name; public String address; public MuiDao(String address, String name) { this.address = address; this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
.MainActivity
package myapplication.com.sdmp3; import android.content.Intent; import android.media.MediaPlayer; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView mListView; private ArrayList name; List<MuiDao> datas; String address; MediaPlayer player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.mListView); name = new ArrayList(); player = new MediaPlayer(); datas=new ArrayList<>(); address=Environment.getExternalStorageDirectory()+""; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File path = Environment.getExternalStorageDirectory(); // 获得SD卡路径 File[] files = path.listFiles();// 读取 getFileName(files); } // SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, name, // R.layout.sd_list, new String[] { "Name" }, // new int[] { R.id.tc_fileName }); adapter adapter1=new adapter(getApplicationContext(),datas); mListView.setAdapter(adapter1); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { MuiDao bean = (MuiDao) parent.getItemAtPosition(position); String path=bean.getName(); player.stop(); Toast.makeText(MainActivity.this,path,Toast.LENGTH_SHORT).show(); try { player=new MediaPlayer(); player.setDataSource(path); player.prepare(); player.start(); } catch (IOException e) { e.printStackTrace(); } } }); } private void getFileName(File[] files) { if (files != null)// 先判断目录是否为空,否则会报空指针 { for (File file : files) { if (file.isDirectory()) { getFileName(file.listFiles()); } else { String fileName = file.getName(); if (fileName.endsWith(".mp3")) { address=file.getAbsolutePath(); HashMap map = new HashMap(); String s = fileName.substring(0, fileName.lastIndexOf(".")); System.out.println("***"+address); datas.add(new MuiDao(fileName.substring(0,fileName.lastIndexOf(".")),address)); map.put("Name", fileName.substring(0,fileName.lastIndexOf("."))); name.add(map); } } } } } }
后面呢:
<1>. 播放进度条实现
<2>. 断点播放
<3>.播放完毕自动顺序的播放
<4>.播放模式的实现
<5>.数据库的加入,避免扫描浪费时间