在開發音樂播放器時除了網絡音樂咱們不少時候須要讀取本地的音頻文件,而且把歌曲的名字、歌手、時間等內容顯示出來,可是本地儲存的格式是不規範的,因此咱們在把音頻文件從本地獲取出來以後還須要進行格式的轉換java
效果圖:android
步驟一:建立一個歌曲類,定義變量,這些變量用來儲存須要獲取的內容網絡
package com.contentprovide.liuliu.get_demo2; /** * Created by liuliu on 2018/1/14. */ public class Song { public String song;//歌曲名 public String singer;//歌手 public long size;//歌曲所佔空間大小 public int duration;//歌曲時間長度 public String path;//歌曲地址 }
步驟二:定義一個工具類,在這個類中獲取音頻文件,而且對歌曲名、歌手和時間等進行格式規範app
package com.contentprovide.liuliu.get_demo2; import android.content.Context; import android.database.Cursor; import android.provider.MediaStore; import java.util.ArrayList; import java.util.List; /** * Created by liuliu on 2018/1/14. */ //工具類 public class Utils { //定義一個集合,存放從本地讀取到的內容 public static List<Song> list; public static Song song; public static List<Song> getmusic(Context context) { list = new ArrayList<>(); Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI , null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC); if (cursor != null) { while (cursor.moveToNext()) { song = new Song(); song.song = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); song.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)); // 把歌曲名字和歌手切割開 if (song.size > 1000 * 800) { if (song.song.contains("-")) { String[] str = song.song.split("-"); song.singer = str[0]; song.song = str[1]; } list.add(song); } } } cursor.close(); return list; } // 轉換歌曲時間的格式 public static String formatTime(int time) { if (time / 1000 % 60 < 10) { String tt = time / 1000 / 60 + ":0" + time / 1000 % 60; return tt; } else { String tt = time / 1000 / 60 + ":" + time / 1000 % 60; return tt; } } }
步驟三:在主佈局文件activity_main.xml中放一個ListView顯示獲取到的音頻文件異步
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>
步驟四:定義一個佈局text.xml用於展現每個音頻文件的內容,並把這個佈局添加到主佈局文件中的ListView中去,在這裏須要寫一個適配器把內容映射到ListView中ide
text.xml佈局:
工具
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/t_postion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" /> <TextView android:id="@+id/t_song" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" /> <TextView android:id="@+id/t_singer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" /> <TextView android:id="@+id/t_duration" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Java代碼:佈局
package com.contentprovide.liuliu.get_demo2; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { ListView mylist; List<Song> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mylist = (ListView) findViewById(R.id.mylist); list = new ArrayList<>(); list = Utils.getmusic(this); MyAdapter myAdapter = new MyAdapter(this, list); mylist.setAdapter(myAdapter); } class MyAdapter extends BaseAdapter { Context context; List<Song> list; public MyAdapter(MainActivity mainActivity, List<Song> list) { this.context = mainActivity; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { Myholder myholder; if (view == null) { myholder = new Myholder(); view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.text, null); myholder.t_position = view.findViewById(R.id.t_postion); myholder.t_song = view.findViewById(R.id.t_song); myholder.t_singer = view.findViewById(R.id.t_singer); myholder.t_duration = view.findViewById(R.id.t_duration); view.setTag(myholder); } else { myholder = (Myholder) view.getTag(); } myholder.t_song.setText(list.get(i).song.toString()); myholder.t_singer.setText(list.get(i).singer.toString()); String time = Utils.formatTime(list.get(i).duration); myholder.t_duration.setText(time); myholder.t_position.setText(i + 1 + ""); return view; } class Myholder { TextView t_position, t_song, t_singer, t_duration; } } }
若是本地資源成功顯示以後須要實現另一個功能,點擊每首歌對應的列表項,歌曲會進行播放post
步驟一:在MainActivity.java中定義一個方法play()進行音頻的處理this
public void play(String path) { try { // 重置音頻文件,防止屢次點擊會報錯 mediaPlayer.reset(); // 調用方法傳進播放地址 mediaPlayer.setDataSource(path); // 異步準備資源,防止卡頓 mediaPlayer.prepareAsync(); // 調用音頻的監聽方法,音頻準備完畢後響應該方法進行音樂播放 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mediaPlayer.start(); } }); } catch (IOException e) { e.printStackTrace(); } }
步驟二:在MainActivity.java中的onCreat()方法中調用剛纔定義的play方法,而且將獲取到的歌曲地址傳遞給這個方法
// 給ListView添加點擊事件,實現點擊哪首音樂就進行播放 mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String p = list.get(i).path;//得到歌曲的地址 play(p); } });