标签:width ping 歌曲 loop return setw duration roi 路径
效果图:
1.获取本地音乐
2.播放音乐
MediaPlayer其实是一个封装的很好的音频、视频流媒体操作类,如果查看其源码,会发现其内部是调用的native方法,所以它其实是有C++实现的。
既然是一个流媒体操作类,那么必然涉及到,播放、暂停、停止等操作,实际上MediaPlayer也为我们提供了相应的方法来直接操作流媒体。
通过上面三个方法,只要设定好流媒体数据源,即可在应用中播放流媒体资源,为了更好的操作流媒体,MediaPlayer还为我们提供了一些其他的方法,这里列出一些常用的,详细内容参阅官方文档。
一,本地音乐获取
package com.hejun.music.biz; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.content.res.Resources; import android.database.Cursor; import android.database.CursorIndexOutOfBoundsException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.ParcelFileDescriptor; import android.provider.MediaStore; import com.hejun.music.R; import com.hejun.music.bean.Song; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SongBIZ { private static SongBIZ songBiz = null; public static SongBIZ getInstangce() { if (songBiz == null) { songBiz = new SongBIZ(); } return songBiz; } public List<Song> getAllMusic(Context context) { List<Song> songs = new ArrayList<>(); Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID, }, MediaStore.Audio.Media.MIME_TYPE + "=? or " + MediaStore.Audio.Media.MIME_TYPE + "=?", new String[]{"audio/mpeg", "audio/x-ms-wma"}, null); if (cursor.moveToFirst()) { Song song = null; do { song = new Song(); // 文件名 song.setFileName(cursor.getString(1)); // 歌曲名 song.setTitle(cursor.getString(2)); // 时长 long time = cursor.getInt(3); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");//12小时制 Date date = new Date(); date.setTime(time); String time1 = simpleDateFormat.format(date); song.setDuration(time1); // 歌手名 song.setSinger(cursor.getString(4)); // 专辑名 song.setAlbum(cursor.getString(5)); song.setAlbumID(cursor.getInt(10)); // 年代 if (cursor.getString(6) != null) { song.setYear(cursor.getString(6)); } else { song.setYear("未知"); } // 歌曲格式 if ("audio/mpeg".equals(cursor.getString(7).trim())) { song.setType("mp3"); } else if ("audio/x-ms-wma".equals(cursor.getString(7).trim())) { song.setType("wma"); } // 文件大小 if (cursor.getString(8) != null) { float size = cursor.getInt(8) / 1024f / 1024f; song.setSize((size + "").substring(0, 4) + "M"); } else { song.setSize("未知"); } // 文件路径 if (cursor.getString(9) != null) { song.setFileUrl(cursor.getString(9)); } songs.add(song); } while (cursor.moveToNext()); cursor.close(); } return songs; } public Bitmap getAlbumArt(Context context,int album_id) { String mUriAlbums = "content://media/external/audio/albums"; String[] projection = new String[]{"album_art"}; Cursor cur = context.getContentResolver().query(Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)), projection, null, null, null); String album_art = null; if (cur.getCount() > 0 && cur.getColumnCount() > 0) { cur.moveToNext(); album_art = cur.getString(0); } cur.close(); Bitmap bm = null; if (album_art != null) { bm = BitmapFactory.decodeFile(album_art); } else { /* int[] pic = new int[]{ R.drawable.aelbum, R.drawable.first, R.drawable.second, R.drawable.third, }; int i = (int) (Math.random()*4);*/ bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.first); } return bm; } }
Service类
package com.hejun.music; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.net.Uri; import android.os.Binder; import android.os.Environment; import android.os.IBinder; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class MusicService extends Service { public MediaPlayer mediaPlayer; private String path; public MusicService() { } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { mediaPlayer = new MediaPlayer(); path = intent.getStringExtra("path"); try { path = intent.getStringExtra("path"); File file = new File(path); FileInputStream fis = new FileInputStream(file); mediaPlayer.setDataSource(fis.getFD()); mediaPlayer.prepare(); mediaPlayer.setLooping(true); // mediaPlayer.start(); } catch (Exception e) { e.printStackTrace(); } return super.onStartCommand(intent, flags, startId); } // 通过 Binder 来保持 Activity 和 Service 的通信 public MyBinder binder = new MyBinder(); public class MyBinder extends Binder { MusicService getService() { return MusicService.this; } } public void play() { Log.e("tag", "play: " + mediaPlayer ); mediaPlayer.start(); } public void pause() { if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); } } public void playOrPause() { if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); } else { mediaPlayer.start(); } } public void stop() { if (mediaPlayer != null) { mediaPlayer.stop(); try { mediaPlayer.reset(); mediaPlayer.setDataSource(path); mediaPlayer.prepare(); mediaPlayer.seekTo(0); } catch (Exception e) { e.printStackTrace(); } } } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } @Override public IBinder onBind(Intent intent) { return binder; } }
主要播放流程:
启动activity ---> 获取歌曲信息-----> 绑定,启动服务(MediaPlayer加载资源)----->调用MediaPlayer.start()方法
标签:width ping 歌曲 loop return setw duration roi 路径
原文地址:https://www.cnblogs.com/conglingkaishi/p/9968869.html