本人初学Android,最近做了一个实现安卓简单音乐播放功能的播放器,收获不少,于是便记录下来自己的思路与知识总结,重温自己的探索之路。
暂停(播放),上(下)一首,停止播放
点击列表中歌曲进行播放,点击按钮以弹出歌曲列表
屏幕上显示歌曲名称,演唱者,专辑图片
显示播放进度条,拖拽进度条以控制播放
通知栏中显示相关信息,并可在通知栏上控制播放
一共有两个Activity界面(分别是主界面和播放细节界面),一个用以在后台播放音乐的Service,一个可以和用户进行交互Notificatication。 为了能够使得活动与活动,活动与服务,活动与通知之间通信,采取发送广播的做法,由于我在MainActivity,PlayActivity,MusicService中都获取了歌曲信息列表,所以在他们之间直接传递歌曲下标location和状态isPlaying即可
核心是由MainActivity通过startService方法控制Service来进行播放
Cursor cursor=context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null)
for(int i=0;i<cursor.getCount();i++){
cursor.moveToNext();
Mp3Info mp3Info=new Mp3Info();
mp3Info.setUrl(cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.DATA)));
mp3InfoList.add(mp3Info);
Intent intent=new Intent(MainActivity.this,PlayActivity.class);
intent.putExtra("state",isPlaying);
intent.putExtra("location",location);
startActivity(intent);
//传递歌曲下标location并启动服务6 实现后台自动播放下一首音乐 :只需为MediaPlayer对象设置setOnCompletionListener()方法即可,在方法中使歌曲下标+1,播放即可,自动播放下一首后记得发送广播给
Intent intent=new Intent(MainActivity.this,MusicService.class);
intent.putExtra("tag",0);
intent.putExtra("location",location);
startService(intent);
//设置自动播放下一首7 实现显示播放进度功能:在Service中创建一个继承于AsyncTask的类,在该类子线程中线程每0.5秒发送一次带有当前播放进度的broadcast给活动界面以更新UI
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if(mediaPlayer!=null){
try {
if (++location < mp3InfoList.size()) {
mediaPlayer.reset();
mediaPlayer.setDataSource(MusicService.this, Uri.parse(mp3InfoList.get(location).getUrl()));
mediaPlayer.prepare();
mediaPlayer.start();
}
}catch (Exception e){
e.printStackTrace();
}
}
public Integer doInBackground(Void...parms){
//设置while (true)以达到循环不断的目的 while (true){ try{ Thread.sleep(500);//线程沉睡500毫秒 } catch (Exception e){ e.printStackTrace(); } position=mediaPlayer.getCurrentPosition();//获得mediaPlayer当前播放进度 if(position<mediaPlayer.getDuration()){ Intent mIntent=new Intent("com.example.musicplayer.update_seekbar"); mIntent.putExtra("position",position); sendBroadcast(mIntent);//发送广播 }else{ break; } } return 0; }
//为SeekBar设置点击拖动事件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress=seekBar.getProgress();
Intent intent=new Intent(MainActivity.this, MusicService.class);
intent.putExtra("tag",2);
intent.putExtra("progress",progress);
startService(intent);
}
});
remoteViews = new RemoteViews(getPackageName(), R.layout.remoteviews_item_dl);//设置RemoteViews的布局
remoteViews.setTextViewText(R.id.tv_filename, getPackageName()); // 设置标题
remoteViews.setTextViewText(R.id.tv_content, "this is notification"); // 设置内容
remoteViews.setImageViewBitmap(R.id.notification_imageView,bitmap);//设置控件Bitmap
// 设置点击事件响应结果为发送广播 remoteViews.setOnClickPendingIntent(R.id.notification_next,PendingIntent.getBroadcast(this,0, new Intent("com.example.musicplayer.notification.next").putExtra("tag2",2), PendingIntent.FLAG_UPDATE_CURRENT)); Notification notification=new NotificationCompat.builder(this).setContent(remotoViews).build();//构建出带有特定布局的通知 manager.notify(0,notification);//显示通知
//通过AlertDialog显示歌曲列表
public void showMusicList(){
Log.d("PlayActivity","showMusicList start");
LinearLayout linearLayoutMain = new LinearLayout(this);//自定义一个布局文件
linearLayoutMain.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Log.d("PlayActivity","LinearLayout created");
ListView listView = new ListView(this);//this为获取当前的上下文
listView.setFadingEdgeLength(0);
List<Map<String,String>>musicList=new ArrayList<Map<String, String>>();
for(int i=0;i<mp3InfoList.size();i++){
Map<String,String>item=new HashMap<String,String>();
item.put("musicName",mp3InfoList.get(i).getTitle().toString());
item.put("artist",mp3InfoList.get(i).getArtist().toString());
musicList.add(item);
}
Log.d("PlayActivity","LinearLayout created and itialized");
//
SimpleAdapter adapter=new SimpleAdapter(PlayActivity.this,musicList,R.layout.music_item
,new String[]{"musicName","artist"}
, new int[]{R.id.music_item_musicName,R.id.music_item_artist});
Log.d("PlayActivity","SimpleAdapter created");
listView.setAdapter(adapter);
linearLayoutMain.addView(listView);
Log.d("PlayActivity","listView was added to the linearLayout");
final AlertDialog dialog=new AlertDialog.Builder(this).setTitle("歌曲列表").setView(linearLayoutMain)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).create();
Log.d("PlayActivity","Dialog created");
dialog.show();
Log.d("PlayActivity","Dialog showed");
//响应子项点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
// 注意这里的参数第三个参数Item在适配器中的位置 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { //更新自身UI musicNameView.setText(mp3InfoList.get(i).getTitle()); artistView.setText(mp3InfoList.get(i).getArtist()); durationView.setText(MediaUtil.formatTime(mp3InfoList.get(i).getDuration())); //发送广播附带歌曲下标给MainActivity来控制服务播放歌曲 Intent intent=new Intent("com.example.musicplayer.react_to_playactiity"); intent.putExtra("tag1",4); intent.putExtra("location",i); sendBroadcast(intent); Log.d("PlayActivity","broadcast sended"); dialog.cancel(); Log.d("PlayActivity","Dialog canceled"); } }); }
1 首先新建保存mp3音乐相关属性信息的实体类Mp3Info(保存url,title,artist等等)
ublic class Mp3Info {
private String url;//路径
private String title;//歌曲名
private String artist;//艺术家
private long duration;//歌曲时长
private long id;
private long albumId;//以上两种Id用以获取专辑图片
public Mp3Info(){
}
public Mp3Info(String url,String title,String artist,long duration,long id,long albumId){
this.url=url;
this.title=title;
this.artist=artist;
this.duration=duration;
this.id=id;
this.albumId=albumId;
}
public void setUrl(String url){
this.url=url;
}
public void setTitle(String title){
this.title=title;
}
public void setArtist(String artist){
this.artist=artist;
}
public void setDuration(long duration){
this.duration=duration;
}
public void setId(long id){this.id=id;}
public void setAlbumId(long albumId){this.albumId=albumId;}
public String getUrl(){
return url;
}
public String getTitle(){
return title;
}
public String getArtist(){
return artist;
}
public long getDuration(){
return duration;} public long getId() { return id; } public long getAlbumId() { return albumId; }}2 新建MediaUtil类用于从安卓媒体库中获取歌曲信息并保存在程序的List中,提供静态方法给其他代码来直接复用
public class MediaUtil { //获取专辑封面的Uri private static final Uri albumArtUri = Uri.parse("content://media/external/audio/albumart"); //从安卓媒体库中获取歌曲信息并保存在程序的List中,并提供静态方法给其他类来直接获得生成的歌曲列表信息 public static List<Mp3Info>getMp3InfoList(Context context){ Cursor cursor=context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); List<Mp3Info>mp3InfoList=new ArrayList<>(); for(int i=0;i<cursor.getCount();i++){ cursor.moveToNext(); Mp3Info mp3Info=new Mp3Info(); mp3Info.setUrl(cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.DATA))); mp3Info.setTitle(cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.TITLE))); mp3Info.setArtist(cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.ARTIST))); mp3Info.setDuration(cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.DURATION))); mp3Info.setId(cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media._ID))); mp3Info.setAlbumId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))); mp3InfoList.add(mp3Info); } return mp3InfoList; } /** * 格式化时间,将毫秒转换为分:秒格式//将long类型转化为String型, * @param time * @return */ public static String formatTime(long time) { String min = time / (1000 * 60) + ""; String sec = time % (1000 * 60) + ""; if (min.length() < 2) { min = "0" + time / (1000 * 60) + ""; } else { min = time / (1000 * 60) + ""; } if (sec.length() == 4) { sec = "0" + (time % (1000 * 60)) + ""; } else if (sec.length() == 3) { sec = "00" + (time % (1000 * 60)) + ""; } else if (sec.length() == 2) { sec = "000" + (time % (1000 * 60)) + ""; } else if (sec.length() == 1) { sec = "0000" + (time % (1000 * 60)) + ""; } return min + ":" + sec.trim().substring(0, 2); } /** * 获取默认专辑图片 * @param context * @return */ public static Bitmap getDefaultArtwork(Context context,boolean small) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.RGB_565; if(small){ //返回小图片 return BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.music5), null, opts); } return BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.defaultalbum), null, opts); } /** * 从文件当中获取专辑封面位图 * @param context * @param songid * @param albumid * @return */ private static Bitmap getArtworkFromFile(Context context, long songid, long albumid){ Bitmap bm = null; if(albumid < 0 && songid < 0) { throw new IllegalArgumentException("Must specify an album or a song id"); } try { BitmapFactory.Options options = new BitmapFactory.Options(); FileDescriptor fd = null; if(albumid < 0){ Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart"); ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); if(pfd != null) { fd = pfd.getFileDescriptor(); } } else { Uri uri = ContentUris.withAppendedId(albumArtUri, albumid); ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); if(pfd != null) { fd = pfd.getFileDescriptor(); } } options.inSampleSize = 1; // 只进行大小判断 options.inJustDecodeBounds = true; // 调用此方法得到options得到图片大小 BitmapFactory.decodeFileDescriptor(fd, null, options); // 我们的目标是在800pixel的画面上显示 // 所以需要调用computeSampleSize得到图片缩放的比例 options.inSampleSize = 100; // 我们得到了缩放的比例,现在开始正式读入Bitmap数据 options.inJustDecodeBounds = false; options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; //根据options参数,减少所需要的内存 bm = BitmapFactory.decodeFileDescriptor(fd, null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } return bm; } /** * 获取专辑封面位图对象 * @param context * @param song_id * @param album_id * @param allowdefalut * @return */ public static Bitmap getArtwork(Context context, long song_id, long album_id, boolean allowdefalut, boolean small){ if(album_id < 0) { if(song_id < 0) { Bitmap bm = getArtworkFromFile(context, song_id, -1); if(bm != null) { return bm; } } if(allowdefalut) { return getDefaultArtwork(context, small); } return null; } ContentResolver res = context.getContentResolver(); Uri uri = ContentUris.withAppendedId(albumArtUri, album_id); if(uri != null) { InputStream in = null; try { in = res.openInputStream(uri); BitmapFactory.Options options = new BitmapFactory.Options(); //先制定原始大小 options.inSampleSize = 1; //只进行大小判断 options.inJustDecodeBounds = true; //调用此方法得到options得到图片的大小 BitmapFactory.decodeStream(in, null, options); /** 我们的目标是在你N pixel的画面上显示。 所以需要调用computeSampleSize得到图片缩放的比例 **/ /** 这里的target为800是根据默认专辑图片大小决定的,800只是测试数字但是试验后发现完美的结合 **/ if(small){ options.inSampleSize = computeSampleSize(options, 40); } else{ options.inSampleSize = computeSampleSize(options, 600); } // 我们得到了缩放比例,现在开始正式读入Bitmap数据 options.inJustDecodeBounds = false; options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; in = res.openInputStream(uri); return BitmapFactory.decodeStream(in, null, options); } catch (FileNotFoundException e) { Bitmap bm = getArtworkFromFile(context, song_id, album_id); if(bm != null) { if(bm.getConfig() == null) { bm = bm.copy(Bitmap.Config.RGB_565, false); if(bm == null && allowdefalut) { return getDefaultArtwork(context, small); } } } else if(allowdefalut) { bm = getDefaultArtwork(context, small); } return bm; } finally { try { if(in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * 对图片进行合适的缩放 * @param options * @param target * @return */ public static int computeSampleSize(BitmapFactory.Options options, int target) { int w = options.outWidth; int h = options.outHeight; int candidateW = w / target; int candidateH = h / target; int candidate = Math.max(candidateW, candidateH); if(candidate == 0) { return 1; } if(candidate > 1) { if((w > target) && (w / candidate) < target) { candidate -= 1; } } if(candidate > 1) { if((h > target) && (h / candidate) < target) { candidate -= 1; } } return candidate; }}3 活动界面编程,这里直接上代码
主界面:
<?xml version="1.0" encoding="utf-8"?>播放细节界面
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainActivity_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/mainActivity_toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/mainActivity_toolBar"
android:background="#FFF">
<ListView
android:id="@+id/musicName_list"
android:layout_width="match_parent"
android:layout_height="440dp"
android:textColor="#FFF"/>
<RelativeLayout
android:id="@+id/mainActivity_seekBarLayout"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_below="@+id/musicName_list"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/drawable_bg">
<SeekBar
android:id="@+id/mainActivity_seekBar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:thumb="@drawable/media_player_progress_button"
/>
<TextView
android:id="@+id/main_current_position"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_below="@id/mainActivity_seekBar"
android:text="0:00"
android:textColor="#060606"/>
<TextView
android:id="@+id/main_final_position"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="@id/mainActivity_seekBar"
android:text="3:00"
android:textColor="#090808"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="#7e7677"
android:layout_below="@+id/mainActivity_seekBarLayout">
<ImageView
android:id="@+id/album_view"
android:layout_width="80dp"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/mainActivity_musicName"
android:layout_width="80dp"
android:layout_toRightOf="@+id/album_view"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:text="歌曲名称"
android:textColor="#090909"/>
<TextView
android:id="@+id/mainActivity_artist"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="@+id/mainActivity_musicName"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/album_view"
android:text="歌曲作者"
android:textColor="#060606"/>
<ImageButton
android:id="@+id/mainActivity_play_pause"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/mainActivity_next"
app:srcCompat="@drawable/play"/>
<ImageButton
android:id="@+id/mainActivity_next"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
app:srcCompat="@drawable/next"
android:layout_toLeftOf="@+id/mainActivity_menu"/>
<ImageButton
android:id="@+id/mainActivity_menu"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:layout_alignParentRight="true"
app:srcCompat="@drawable/menu"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/mainActivity_nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/nav_header"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#885959" > <RelativeLayout android:id="@+id/play_activity_toolBar1" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" > android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/playActivity_return" android:layout_width="45dp" android:layout_height="30dp" android:background="@drawable/back_return_normal"/> <TextView android:id="@+id/playActivity_musicName" android:layout_width="match_parent" android:layout_height="20dp" android:layout_toRightOf="@+id/playActivity_return" android:text="歌曲名称" android:layout_marginLeft="5dp" android:textColor="#fcfcfc"/> <TextView android:id="@+id/playActivity_artist" android:layout_width="match_parent" android:layout_marginLeft="5dp" android:layout_height="20dp" android:layout_toRightOf="@+id/playActivity_return" android:layout_below="@+id/playActivity_musicName" android:text="演唱者" android:textColor="#c3b9b9" /> </RelativeLayout> <ScrollView android:id="@+id/playActivity_musicLyrics" android:layout_width="match_parent" android:layout_height="400dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_above="@+id/playActivity_seekbarLayout"> </ScrollView> <RelativeLayout android:id="@+id/playActivity_seekbarLayout" android:layout_width="match_parent" android:layout_height="35dp" android:background="@drawable/drawable_bg" android:layout_above="@+id/playActivity_toolBar2"> <SeekBar android:id="@+id/playActivity_seekBar" android:layout_width="match_parent" android:layout_height="20dp" android:thumb="@drawable/media_player_progress_button" /> <TextView android:id="@+id/current_position" android:layout_width="wrap_content" android:layout_height="25dp" android:layout_below="@id/playActivity_seekBar" android:text="0:00" android:textColor="#FFF"/> <TextView android:id="@+id/final_position" android:layout_width="wrap_content" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_below="@id/playActivity_seekBar" android:text="3:00" android:textColor="#FFF"/> </RelativeLayout> <RelativeLayout android:id="@+id/playActivity_toolBar2" android:layout_width="match_parent" android:layout_height="70dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <ImageButton android:id="@+id/stop" android:layout_width="50dp" android:layout_margin="10dp" android:layout_height="match_parent" app:srcCompat="@drawable/stop"/> <ImageButton android:id="@+id/previous" android:layout_width="50dp" android:layout_margin="10dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/stop" app:srcCompat="@drawable/previous"/> <ImageButton android:id="@+id/play_pause" android:layout_margin="10dp" android:layout_width="50dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/previous" app:srcCompat="@drawable/play" /> <ImageButton android:id="@+id/next_music" android:layout_width="50dp" android:layout_margin="10dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/play_pause" app:srcCompat="@drawable/next" /> <ImageButton android:id="@+id/music_menu" android:layout_width="50dp" android:layout_margin="10dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/next_music" app:srcCompat="@drawable/playqueue" /> </RelativeLayout></RelativeLayout>4 MainActivity编程
public class MainActivity extends AppCompatActivity { static ListView listView; List<Mp3Info>mp3InfoList; Mp3Info mp3Info; static int location=0; static Boolean isPlaying=false; List musicNameList=new ArrayList<>(); static TextView musicNameView; static TextView artistView; static ImageView albumView; static ImageButton playOrPauseButton; static ImageButton nextButton; static SeekBar seekBar; static TextView currentTiemView; static TextView durationView; DrawerLayout drawerLayout; String Tag="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ToolBar及DrawerLayout Toolbar toolbar=(Toolbar)findViewById(R.id.mainActivity_toolBar); setSupportActionBar(toolbar); ActionBar actionBar=getSupportActionBar(); if(actionBar!=null){ actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeAsUpIndicator(R.drawable.menu); } drawerLayout=(DrawerLayout)findViewById(R.id.mainActivity_drawer_layout); //获得各个控件并设置点击事件 musicNameView=(TextView)findViewById(R.id.mainActivity_musicName); artistView=(TextView)findViewById(R.id.mainActivity_artist); playOrPauseButton=(ImageButton)findViewById(R.id.mainActivity_play_pause); nextButton=(ImageButton)findViewById(R.id.mainActivity_next); seekBar=(SeekBar)findViewById(R.id.mainActivity_seekBar); currentTiemView=(TextView)findViewById(R.id.main_current_position) ; durationView=(TextView)findViewById(R.id.main_final_position); albumView=(ImageView)findViewById(R.id.album_view); //设置各控件初始视图为上次离开时所播放的歌曲,如果进程已被Kill掉,则为第一首 mp3InfoList=MediaUtil.getMp3InfoList(MainActivity.this); mp3Info=mp3InfoList.get(location); musicNameView.setText(mp3Info.getTitle()); artistView.setText(mp3Info.getArtist()); long id=mp3Info.getId(); long albumId=mp3Info.getAlbumId(); albumView.setImageBitmap(MediaUtil.getArtwork(this, id, albumId, true, false)); if(isPlaying) playOrPauseButton.setImageResource(R.drawable.pause); else playOrPauseButton.setImageResource(R.drawable.play); //专辑图片点击事件,进入PlayActivity中,并通过Intent传递location和isPlaying状态 albumView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,PlayActivity.class); intent.putExtra("state",isPlaying); intent.putExtra("location",location); startActivity(intent); } }); //暂停、播放按钮点击事件 playOrPauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { playOrpause(); } }); //下一首点击事件 nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { nextMusic(); } }); //从媒体库中获得音乐列表并显示到ListView中,同时设置点击事件 listView=(ListView)findViewById(R.id.musicName_list) ; for(int i=0;i<mp3InfoList.size();i++) musicNameList.add(mp3InfoList.get(i).getTitle()); ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,musicNameList); listView.setAdapter(aa); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { location=i; setPlay(location); } }); //注册监听SeekBar变化的监听器 IntentFilter filter=new IntentFilter(); filter.addAction("com.example.musicplayer.update_seekbar"); UpdateSeekbarBroadcast receiver=new UpdateSeekbarBroadcast(); registerReceiver(receiver,filter); //监听PlayActivity的点击事件 IntentFilter filter1=new IntentFilter(); filter1.addAction("com.example.musicplayer.react_to_playactiity"); PlayActivityBroadcast receiver1=new PlayActivityBroadcast(); registerReceiver(receiver1,filter1); //注册监听Notification的监听器 IntentFilter filter2=new IntentFilter(); filter2.addAction("com.example.musicplayer.notification.previous"); filter2.addAction("com.example.musicplayer.notification.play_pause"); filter2.addAction("com.example.musicplayer.notification.next"); NotificationBroadcast receiver2=new NotificationBroadcast(); registerReceiver(receiver2,filter2); //注册监听后台顺序播放的监听器 IntentFilter filter3=new IntentFilter(); filter3.addAction("com.example.musicplayer.order_play"); OrderPlayBroadcast receiver3=new OrderPlayBroadcast(); registerReceiver(receiver3,filter3); //为SeekBar设置点击拖动事件 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress=seekBar.getProgress(); Intent intent=new Intent(MainActivity.this, MusicService.class); intent.putExtra("tag",2); intent.putExtra("progress",progress); startService(intent); } }); } public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.toolbar,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem menuItem){ switch (menuItem.getItemId()){ case android.R.id.home:drawerLayout.openDrawer(GravityCompat.START); break; case R.id.search: Toast.makeText(MainActivity.this,"You clicked the search button",Toast.LENGTH_SHORT).show(); break; } return true; } //设置播放某一首歌曲 public void setPlay(int location){ //切换播放状态 isPlaying=true; //根据要播放的音乐曲目更新MainActivity的UI mp3Info=mp3InfoList.get(location); long id=mp3Info.getId(); long albumId=mp3Info.getAlbumId(); musicNameView.setText(mp3Info.getTitle()); artistView.setText(mp3Info.getArtist()); Bitmap bitmap = MediaUtil.getArtwork(this, id, albumId, true, false);// 获取专辑位图对象,为大图 albumView.setImageBitmap(bitmap); //切换播放状态图标 playOrPauseButton.setImageResource(R.drawable.pause); seekBar.setMax((int)mp3Info.getDuration()); currentTiemView.setText("0:00"); durationView.setText(MediaUtil.formatTime(mp3Info.getDuration())); //传递歌曲下标location并启动服务 Intent intent=new Intent(MainActivity.this,MusicService.class); intent.putExtra("tag",0); intent.putExtra("location",location); startService(intent); } //播放下一首歌曲 public void nextMusic(){ if((++location)<mp3InfoList.size()){ setPlay(location); } } //播放上一首歌曲 public void previousMusic(){ if ((--location)>=0){ setPlay(location); } } //暂停或继续播放 public void playOrpause(){ Intent intent=new Intent(MainActivity.this,MusicService.class); //切换播放状态并改变状态图标 isPlaying=!isPlaying; if(isPlaying) playOrPauseButton.setImageResource(R.drawable.pause); else playOrPauseButton.setImageResource(R.drawable.play); intent.putExtra("tag",1); intent.putExtra("state",isPlaying); startService(intent); } //停止播放此首音乐,并将进度条滑至开始处 public void stopMusic(){ Intent intent=new Intent(MainActivity.this,MusicService.class); intent.putExtra("tag",3); if(isPlaying) playOrPauseButton.setImageResource(R.drawable.play); else playOrPauseButton.setImageResource(R.drawable.pause); startService(intent); } //监听来自Service的SeekBar变化 class UpdateSeekbarBroadcast extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ int position=intent.getIntExtra("position",0); currentTiemView.setText(MediaUtil.formatTime(position)); seekBar.setProgress(position); } } //接收来自PlayActivity的广播 class PlayActivityBroadcast extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ int tag1=intent.getIntExtra("tag1",-1); switch (tag1){ case 0: { playOrpause(); } break; case 1:previousMusic(); break; case 2:nextMusic(); break; case 3:stopMusic(); default: break; case 4:{ location=intent.getIntExtra("location",5); setPlay(location); } } } } //接收来自Notification的广播 class NotificationBroadcast extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ int tag2=intent.getIntExtra("tag2",-1); LogUtil.d(Tag,"received the broadcast from notification"); switch (tag2){ case 0:previousMusic(); break; case 1:{ playOrpause(); isPlaying=intent.getBooleanExtra("state",false); } break; case 2:nextMusic(); break; default: break; } } } //接收来自后台自动播放的广播以更新UI class OrderPlayBroadcast extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ LogUtil.d(Tag,"update ui according to the orderly play"); location=intent.getIntExtra("location",-1); mp3Info=mp3InfoList.get(location); long id=mp3Info.getId(); long albumId=mp3Info.getAlbumId(); musicNameView.setText(mp3Info.getTitle()); artistView.setText(mp3Info.getArtist()); Bitmap bitmap = MediaUtil.getArtwork(MainActivity.this, id, albumId, true, false);// 获取专辑位图对象,为大图 albumView.setImageBitmap(bitmap); seekBar.setMax((int)mp3Info.getDuration()); currentTiemView.setText("0:00"); durationView.setText(MediaUtil.formatTime(mp3Info.getDuration())); } }}
public class PlayActivity extends AppCompatActivity implements View.OnClickListener {
TextView musicNameView,artistView;
Button returnBack;
ImageButton stop,previousMusic,playOrPauseButton,nextMusic,musicList;
SeekBar seekBar;
TextView currentTimeView,durationView;
Intent intent;
static int location=0;
static Boolean isPlaying=false;
Mp3Info mp3Info;
List<Mp3Info>mp3InfoList=new ArrayList<>();
String[]musicNameList;
String Tag="PlayActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
//获得歌曲列表信息
mp3InfoList=MediaUtil.getMp3InfoList(PlayActivity.this);
//获得各控件实例
musicNameView=(TextView)findViewById(R.id.playActivity_musicName) ;
artistView=(TextView)findViewById(R.id.playActivity_artist);
stop=(ImageButton)findViewById(R.id.stop);
previousMusic=(ImageButton)findViewById(R.id.previous);
playOrPauseButton=(ImageButton)findViewById(R.id.play_pause);
nextMusic=(ImageButton)findViewById(R.id.next_music);
musicList=(ImageButton)findViewById(R.id.music_menu);
returnBack=(Button)findViewById(R.id.playActivity_return);
seekBar=(SeekBar)findViewById(R.id.playActivity_seekBar);
currentTimeView=(TextView)findViewById(R.id.current_position);
durationView=(TextView)findViewById(R.id.final_position);
//从MainActivity中传递过来的数据
Intent intent1=getIntent();
location=intent1.getIntExtra("location",0);
isPlaying=intent1.getBooleanExtra("state",false);
//根据传递过来的数据更新UI视图
seekBar.setMax((int)(mp3InfoList.get(location).getDuration()));
currentTimeView.setText("0:00");
durationView.setText(MediaUtil.formatTime(mp3InfoList.get(location).getDuration()));
musicNameView.setText(mp3InfoList.get(location).getTitle());
artistView.setText(mp3InfoList.get(location).getArtist());
durationView.setText(MediaUtil.formatTime(mp3InfoList.get(location).getDuration()));
if(isPlaying)
playOrPauseButton.setImageResource(R.drawable.pause);
else playOrPauseButton.setImageResource(R.drawable.play);
//注册seekBar监听器
IntentFilter filter=new IntentFilter();
filter.addAction("com.example.musicplayer.update_seekbar");
UpdateSeekbarBroadcast receiver=new UpdateSeekbarBroadcast();
registerReceiver(receiver,filter);
//注册后台顺序播放的监听器
IntentFilter filter1=new IntentFilter();
filter1.addAction("com.example.musicplayer.order_play");
OrderPlayBroadcast receiver1=new OrderPlayBroadcast();
registerReceiver(receiver1,filter1);
//为SeekBar设置点击拖动事件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress=seekBar.getProgress();
Intent intent=new Intent(PlayActivity.this, MusicService.class);
intent.putExtra("tag",2);
intent.putExtra("progress",progress);
startService(intent);
}
});
//设置各按钮点击事件
stop.setOnClickListener(this);
previousMusic.setOnClickListener(this);
playOrPauseButton.setOnClickListener(this);
nextMusic.setOnClickListener(this);
musicList.setOnClickListener(this);
}
//监听来自Service的SeekBar变化
class UpdateSeekbarBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
int position=intent.getIntExtra("position",0);
currentTimeView.setText(MediaUtil.formatTime(position));
seekBar.setProgress(position);
}
}
//接收来自后台自动播放的广播以更新UI
class OrderPlayBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
LogUtil.d(Tag,"PlayActivity receive broadcast from service orderly play to update UI ");
location=intent.getIntExtra("location",-1);
mp3Info=mp3InfoList.get(location);
musicNameView.setText(mp3Info.getTitle());
artistView.setText(mp3Info.getArtist());
seekBar.setMax((int)mp3Info.getDuration());
currentTimeView.setText("0:00");
durationView.setText(MediaUtil.formatTime(mp3Info.getDuration()));
}
}
@Override
public void onClick(View view){
Intent intent=new Intent("com.example.musicplayer.react_to_playactiity");
switch (view.getId()){
case R.id.stop :{
intent.putExtra("tag1",3);
}
break;
case R.id.previous: {
isPlaying=!isPlaying;
previousMusic();
intent.putExtra("tag1", 1);
}
break;
case R.id.play_pause:{
isPlaying=!isPlaying;
if(isPlaying) {
playOrPauseButton.setImageResource(R.drawable.pause);
}
else {
playOrPauseButton.setImageResource(R.drawable.play);
}
intent.putExtra("tag1",0);
intent.putExtra("state",isPlaying);
}
break;
case R.id.next_music:{
nextMusic();
intent.putExtra("tag1",2);
}
break;
case R.id.music_menu:
Log.d("PlayActivity","press the button music_menu");
showMusicList();
break;
default:
break;
}
sendBroadcast(intent);
}
//根据点击事件更新UI视图
public void previousMusic(){
if((--location)>=0)
{
seekBar.setMax((int)(mp3InfoList.get(location).getDuration()));
currentTimeView.setText("0:00");
durationView.setText(MediaUtil.formatTime(mp3InfoList.get(location).getDuration()));
musicNameView.setText(mp3InfoList.get(location).getTitle());
artistView.setText(mp3InfoList.get(location).getArtist());
playOrPauseButton.setImageResource(R.drawable.pause);
}
}
public void nextMusic(){
if((++location)>=0)
{
seekBar.setMax((int)(mp3InfoList.get(location).getDuration()));
currentTimeView.setText("0:00");
durationView.setText(MediaUtil.formatTime(mp3InfoList.get(location).getDuration()));
musicNameView.setText(mp3InfoList.get(location).getTitle());
artistView.setText(mp3InfoList.get(location).getArtist());
playOrPauseButton.setImageResource(R.drawable.pause);
}
}
//通过AlertDialog显示歌曲列表
public void showMusicList(){
Log.d("PlayActivity","showMusicList start");
LinearLayout linearLayoutMain = new LinearLayout(this);//自定义一个布局文件
linearLayoutMain.setLayoutParams(new LinearLayoutCompat.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Log.d("PlayActivity","LinearLayout created");
ListView listView = new ListView(this);//this为获取当前的上下文
listView.setFadingEdgeLength(0);
List<Map<String,String>>musicList=new ArrayList<Map<String, String>>();
for(int i=0;i<mp3InfoList.size();i++){
Map<String,String>item=new HashMap<String,String>();
item.put("musicName",mp3InfoList.get(i).getTitle().toString());
item.put("artist",mp3InfoList.get(i).getArtist().toString());
musicList.add(item);
}
Log.d("PlayActivity","LinearLayout created and itialized");
SimpleAdapter adapter=new SimpleAdapter(PlayActivity.this,musicList,R.layout.music_item
,new String[]{"musicName","artist"}
, new int[]{R.id.music_item_musicName,R.id.music_item_artist});
Log.d("PlayActivity","SimpleAdapter created");
listView.setAdapter(adapter);
linearLayoutMain.addView(listView);
Log.d("PlayActivity","listView was added to the linearLayout");
final AlertDialog dialog=new AlertDialog.Builder(this).setTitle("歌曲列表").setView(linearLayoutMain)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).create();
Log.d("PlayActivity","Dialog created");
dialog.show();
Log.d("PlayActivity","Dialog showed");
//响应子项点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//更新自身UI
musicNameView.setText(mp3InfoList.get(i).getTitle());
artistView.setText(mp3InfoList.get(i).getArtist());
durationView.setText(MediaUtil.formatTime(mp3InfoList.get(i).getDuration()));
//发送广播附带歌曲下标给MainActivity来控制服务播放歌曲
Intent intent=new Intent("com.example.musicplayer.react_to_playactiity");
intent.putExtra("tag1",4);
intent.putExtra("location",i);
sendBroadcast(intent);
Log.d("PlayActivity","broadcast sended");
dialog.cancel();
Log.d("PlayActivity","Dialog canceled");
}
});
}
}
public class MusicService extends Service {上面就是这个Demo的全部内容,还有很多不足和程序运行期间会出现一些闪退的现象,限于本人目前水平有限,希望日后经过更深入学习能回来解决现在留下的问题。
List<Mp3Info> mp3InfoList = new ArrayList<>();
MediaPlayer mediaPlayer;
Mp3Info mp3Info;
Notification notification;
static int location;
int position;
static Boolean isPlaying=false;
RemoteViews contecntViews;
public MusicService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer=new MediaPlayer();
//获得歌曲Mp3Info类列表
mp3InfoList = MediaUtil.getMp3InfoList(MusicService.this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MusicService", "service onStartommand");
int tag= intent.getIntExtra("tag", -1);
switch (tag) {
case 0: {
isPlaying=true;
location=intent.getIntExtra("location",-1);
mp3Info=mp3InfoList.get(location);
String url = mp3Info.getUrl();
String title=mp3Info.getTitle();
String artist=mp3Info.getArtist();
long id=mp3Info.getId();
long albumId=mp3Info.getAlbumId();
try {
if (mediaPlayer != null) {
mediaPlayer.reset();
}
mediaPlayer.setDataSource(this, Uri.parse(url));
mediaPlayer.prepare();
mediaPlayer.start();
showNotification(title,artist,id,albumId);
} catch (IOException e) {
e.printStackTrace();
}
}break;
case 1:{
if(mediaPlayer!=null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else mediaPlayer.start();
}
isPlaying=intent.getBooleanExtra("state",false);
if(isPlaying==true)
isPlaying=false;
else
isPlaying=true;
}break;
case 2:{
isPlaying=true;
int progress=intent.getIntExtra("progress",0);
if(mediaPlayer!=null){
mediaPlayer.seekTo(progress);
}
}break;
case 3:{
mediaPlayer.stop();
try{
mediaPlayer.prepare();
mediaPlayer.seekTo(0);}
catch (Exception e){
e.printStackTrace();
}
} break;
}
//
new PlayProgress().execute();
//设置自动播放下一首
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if(mediaPlayer!=null){
try {
if (++location < mp3InfoList.size()) {
mediaPlayer.reset();
mediaPlayer.setDataSource(MusicService.this, Uri.parse(mp3InfoList.get(location).getUrl()));
mediaPlayer.prepare();
mediaPlayer.start();
}
}catch (Exception e){
e.printStackTrace();
}
}
//通过发送广播让MainActivity,PlayActivity更新UI
Intent intent=new Intent("com.example.musicplayer.order_play");
intent.putExtra("location",location);
sendBroadcast(intent);
//后台更新NotificationUI
isPlaying=true;
mp3Info=mp3InfoList.get(location);
String title=mp3Info.getTitle();
String artist=mp3Info.getArtist();
long id=mp3Info.getId();
long albumId=mp3Info.getAlbumId();
showNotification(title,artist,id,albumId);
}
});
return super.onStartCommand(intent, flags, startId);
}
public class PlayProgress extends AsyncTask<Void,Integer,Integer>{
@Override
public void onPreExecute(){
super.onPreExecute();
}
@Override
public Integer doInBackground(Void...parms){
while (true){
try{
Thread.sleep(500);
}
catch (Exception e){
e.printStackTrace();
}
position=mediaPlayer.getCurrentPosition();
if(position<mediaPlayer.getDuration()){
Intent mIntent=new Intent("com.example.musicplayer.update_seekbar");
mIntent.putExtra("position",position);
sendBroadcast(mIntent);
}else{
break;
}
}
return 0;
}
@Override
protected void onPostExecute(Integer integer) {
Log.d("===","异步类执行完毕");
super.onPostExecute(integer);
}
}
//通知栏
public void showNotification(String title, String artist, long id, long albumId){
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
contecntViews =new RemoteViews(getPackageName(),R.layout.notification_music);
contecntViews.setTextViewText(R.id.notification_musicName,title);
contecntViews.setTextViewText(R.id.notification_artist,artist);
Bitmap bitmap = MediaUtil.getArtwork(this, id,
albumId, true, false);// 获取专辑位图对象,为大图
contecntViews.setImageViewBitmap(R.id.notification_imageView,bitmap);
contecntViews.setOnClickPendingIntent(R.id.notification_previousMusic,PendingIntent.getBroadcast(this,0,
new Intent("com.example.musicplayer.notification.previous").putExtra("tag2",0), PendingIntent.FLAG_UPDATE_CURRENT));
contecntViews.setOnClickPendingIntent(R.id.notification_play_pause,PendingIntent.getBroadcast(this,0,
new Intent("com.example.musicplayer.notification.play_pause").putExtra("tag2",1).putExtra("state",!isPlaying), PendingIntent.FLAG_UPDATE_CURRENT));
contecntViews.setOnClickPendingIntent(R.id.notification_next,PendingIntent.getBroadcast(this,0,
new Intent("com.example.musicplayer.notification.next").putExtra("tag2",2), PendingIntent.FLAG_UPDATE_CURRENT));
notification=builder.setContent(contecntViews).setSmallIcon(R.mipmap.ic_launcher).build();
manager.notify(0,notification);
}
@Override
public void onDestroy(){
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。