MediaPlayer
权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
开始-结束-暂停-继续
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(list.get(position).getData());
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.stop();
mediaPlayer.pause();
mediaPlayer.start();
Main页面
OnCreate方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mediaPlayer = new MediaPlayer();
list = new ArrayList<>();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(list.get(position).getData());
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
onRequestPermissionsResult
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
ContentResolver contentResolver = getContentResolver();
Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (query!=null){
while (query.moveToNext()){
String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String duration = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
String albumId = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Music music = new Music(title,artist,duration,data,size,albumId);
list.add(music);
}
query.close();
}
MyAdapter myAdapter = new MyAdapter(this, list);
listView.setAdapter(myAdapter);
}else{
finish();
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/item_pic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/item_artist"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_margin="10dp"
android:text="1"
android:background="@mipmap/ic_launcher"
android:textSize="18sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="1"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:text="歌曲名"
android:textSize="18sp" />
<TextView
android:id="@+id/item_artist"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/item_duration"
android:gravity="bottom"
android:text="歌手"
android:ellipsize="end"
android:maxLines="1"
android:layout_marginBottom="5dp"
android:textSize="16sp" />
<TextView
android:id="@+id/item_duration"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="bottom"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:text="歌曲时间"
android:textSize="16sp" />
</RelativeLayout>
</LinearLayout>