您的位置:首页 > 移动开发 > Android开发

Android开发之本地音乐播放器(增强版)

2016-05-16 22:37 609 查看

界面部分

主界面:



歌曲详情界面:



通知栏界面:



功能介绍

使用了广播和服务来完成部分功能

可以自动查找本地的mp3文件并显示在列表中

主界面和详情界面的seekbar的进度条都可以跟随歌曲播放更新或者拖动

主界面和详情界面以及通知栏都可以切歌或暂停并实现同步

部分代码

主界面:MusicPlayerActivity.java

public class MusicPlayerActivity extends AppCompatActivity {

private ListView lv;
private List lvList;
private List<Mp3Info> mp3InfoList;
private ImageButton bt0,bt1,bt2;
private ImageView iv1;
private TextView tv1,tv2,tv3,tv4;
private Boolean flag=false;
private SeekBar sb;
private int location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_player);

TestBroadcast testBroadcast=new TestBroadcast();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.my.test.broadcast");
registerReceiver(testBroadcast,intentFilter);

MyTestBroadcast myTestBroadcast=new MyTestBroadcast();
IntentFilter intentFilter1=new IntentFilter();
intentFilter1.addAction("com.my.mytest.broadcast");
registerReceiver(myTestBroadcast,intentFilter1);

IntentFilter mNotificationIntentFilter = new IntentFilter();
mNotificationIntentFilter.addAction("com.my.button.start");
mNotificationIntentFilter.addAction("com.my.button.pre");
mNotificationIntentFilter.addAction("com.my.button.next");
MyButtonBroadcast myButtonBroadcast = new MyButtonBroadcast();
registerReceiver(myButtonBroadcast, mNotificationIntentFilter);

lv= (ListView) findViewById(R.id.lv);
bt1= (ImageButton) findViewById(R.id.bt1);
bt2= (ImageButton) findViewById(R.id.bt2);
bt0= (ImageButton) findViewById(R.id.bt0);
tv1= (TextView) findViewById(R.id.tv1);
tv2= (TextView) findViewById(R.id.tv2);
tv3= (TextView) findViewById(R.id.tv3);
tv4= (TextView) findViewById(R.id.tv4);
iv1= (ImageView) findViewById(R.id.iv1);
sb= (SeekBar) findViewById(R.id.sb);

location=0;
LoadLvList();
ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,lvList);
lv.setAdapter(aa);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
location=position;
setPlay(position);
}
});
bt1.setOnClickListener(new View.
OnClickListener() {
@Override
public void onClick(View v) {
PauseAndStart();

}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextPlay();
}
});

bt0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prePlay();
}
});

sb.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(MusicPlayerActivity.this, MyService.class);
intent.putExtra("tag",2);
intent.putExtra("progress",progress);
startService(intent);
}
});

}

private void prePlay() {
if(location-1>=0){
location=location-1;
}
setPlay(location);
}

private void nextPlay() {
if(location+1<mp3InfoList.size()){
location=location+1;
}
setPlay(location);
}

public void LoadLvList() {
mp3InfoList=new ArrayList<Mp3Info>();
mp3InfoList= MediaUtil.getMusicInfo(this);
lvList=new ArrayList();
for(int i=0;i<mp3InfoList.size();i++){
lvList.add(mp3InfoList.get(i).getTitle());
}
}

public void setPlay(int location){
Intent intent=new Intent(MusicPlayerActivity.this, MyService.class);
Mp3Info mp3Info=mp3InfoList.get(location);
sb.setMax((int) mp3Info.getDuration());
sb.setProgress(0);
intent.putExtra("tag",0);
intent.putExtra("url",mp3Info.getUrl());
intent.putExtra("id",mp3Info.getId());
intent.putExtra("title",mp3Info.getTitle());
intent.putExtra("artist",mp3Info.getArtist());
intent.putExtra("albumId",mp3Info.getAlbumId());
startService(intent);
tv1.setText(mp3Info.getTitle());
tv2.setText(mp3Info.getArtist());
tv4.setText(MediaUtil.formatTime(mp3Info.getDuration()));
Bitmap bitmap = MediaUtil.getArtwork(getBaseContext(), mp3Info.getId(),
mp3Info.getAlbumId(), true, false);
iv1.setImageBitmap(bitmap);
if(flag==false){
bt1.setImageResource(R.mipmap.btn_play);
flag=true;
}

}

public class TestBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int position=intent.getIntExtra("position",0);
tv3.setText(MediaUtil.formatTime(position));
sb.setProgress(position);
}
}

public class MyTestBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int loca=intent.getIntExtra("location",0);
Log.d("===loca",""+loca);
location=loca;
setPlay(location);

}
}

public class MyButtonBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getIntExtra("tag",0)==0){
PauseAndStart();
}else if(intent.getIntExtra("tag",0)==1){
prePlay();
}else{
nextPlay();
}
}
}

public void getDetail(View view){
Intent intent=new Intent(this,MusicDetailActivity.class);
Bundle bundle=new Bundle();
Mp3Info mp3Info=mp3InfoList.get(location);
bundle.putBoolean("flag",flag);
bundle.putInt("location",location);
intent.putExtras(bundle);
startActivity(intent);
}

public void PauseAndStart(){
Intent intent=new Intent(MusicPlayerActivity.this, MyService.class);
intent.putExtra("tag",1);
startService(intent);
if(flag==true){
bt1.setImageResource(R.mipmap.btn_pause);
flag=false;
}else{
bt1.setImageResource(R.mipmap.btn_play);
flag=true;
}
}
}


详情界面:MusicDetailActivity.java

public class MusicDetailActivity extends AppCompatActivity {
private TextView song,singer,timeleft,timeright;
private ImageButton start,pre,next;
private SeekBar sb;
private Boolean flag;
private int location;
private List<Mp3Info> mp3InfoList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_detail);

TestBroadcast testBroadcast=new TestBroadcast();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.my.test.broadcast");
registerReceiver(testBroadcast,intentFilter);

song= (TextView) findViewById(R.id.tv_song);
singer= (TextView) findViewById(R.id.tv_singer);
timeleft= (TextView) findViewById(R.id.tv_timeleft);
timeright= (TextView) findViewById(R.id.tv_timeright);
start= (ImageButton) findViewById(R.id.bt_start);
pre= (ImageButton) findViewById(R.id.bt_pre);
next= (ImageButton) findViewById(R.id.bt_next);
sb= (SeekBar) findViewById(R.id.sb);

mp3InfoList=new ArrayList<Mp3Info>();
mp3InfoList= MediaUtil.getMusicInfo(this);

Intent intent=getIntent();
Bundle bundle=intent.getExtras();
flag=bundle.getBoolean("flag");
location=bundle.getInt("location");
Log.d("===location",""+location);
init(location);

start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MusicDetailActivity.this, MyService.class);
intent.putExtra("tag",1);
startService(intent);
if(flag==true){
start.setImageResource(R.drawable.bt_pause);
flag=false;
}else{
start.setImageResource(R.drawable.bt_start);
flag=true;
}

}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent=new Intent("com.my.mytest.broadcast");
location++;
mIntent.putExtra("location",location);
sendBroadcast(mIntent);
init(location);
}
});

pre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent=new Intent("com.my.mytest.broadcast");
location--;
mIntent.putExtra("location",location);
sendBroadcast(mIntent);
init(location);
}
});

sb.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(MusicDetailActivity.this, MyService.class);
intent.putExtra("tag",2);
intent.putExtra("progress",progress);
startService(intent);
}
});

}

private void init(int locat) {
Mp3Info mp3Info=mp3InfoList.get(locat);
song.setText(mp3Info.getTitle());
singer.setText(mp3Info.getArtist());
timeright.setText(MediaUtil.formatTime(mp3Info.getDuration()));
sb.setMax((int) mp3Info.getDuration());
sb.setProgress(0);
}

public class TestBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int position=intent.getIntExtra("position",0);
timeleft.setText(MediaUtil.formatTime(position));
sb.setProgress(position);
}
}
}


服务:MyService.java

public class MyService extends Service {

private MediaPlayer mediaPlayer;

private NotificationManager nm;
private NotificationCompat.Builder builder;
private Notification nf;

public MyService() {
}

public class MyBind extends Binder {
public MyService getService() {
return MyService.this;
}
}

public void test() {
Log.d("====", "我是Service里的方法");
stopSelf();
}

@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
Log.d("====", "服务创建");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("====", "服务启动--非绑定方式");
if(intent!=null){
int tag=intent.getIntExtra("tag",3);
switch (tag){
case 0:
String url=intent.getStringExtra("url");
String title=intent.getStringExtra("title");
String artist=intent.getStringExtra("artist");
long id=intent.getLongExtra("id",0);
long albumId=intent.getLongExtra("albumId",0);
try {
if(mediaPlayer!=null){
mediaPlayer.reset();
}
mediaPlayer.setDataSource(this, Uri.parse(url));
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
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();
}
}
break;
case 2:
int progress=intent.getIntExtra("progress",0);
if(mediaPlayer!=null){
mediaPlayer.seekTo(progress);
}
default:
break;
}
}
new PlayProgress().execute();
return super.onStartCommand(intent, flags, startId);
}

public class PlayProgress extends AsyncTask<Void,Integer,Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();

}

@Override
protected Integer doInBackground(Void... params) {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int position=mediaPlayer.getCurrentPosition();
if(position<mediaPlayer.getDuration()){
Intent mIntent=new Intent("com.my.test.broadcast");
mIntent.putExtra("position",position);
sendBroadcast(mIntent);
}else{
break;
}
}
return 0;
}

@Override
protected void onProgressUpdate(Integer... values) {
Log.d("===","更新进度"+values[0]);
}

@Override
protected void onPostExecute(Integer integer) {
Log.d("===","异步类执行完毕");
super.onPostExecute(integer);
}
}

private void showNotification(String title, String artist,long id,long albumId) {
nm= (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
builder= new NotificationCompat.Builder(getBaseContext());
RemoteViews contentView=new RemoteViews(getPackageName(),R.layout.notification_music_layout);
contentView.setTextViewText(R.id.tv1,title);
contentView.setTextViewText(R.id.tv2,artist);

contentView.setOnClickPendingIntent(R.id.bt1, PendingIntent.getBroadcast(this, 0, new Intent("com.my.button.start").putExtra("tag",0), PendingIntent.FLAG_UPDATE_CURRENT));
contentView.setOnClickPendingIntent(R.id.bt0, PendingIntent.getBroadcast(this, 0, new Intent("com.my.button.pre").putExtra("tag",1), PendingIntent.FLAG_UPDATE_CURRENT));
contentView.setOnClickPendingIntent(R.id.bt2, PendingIntent.getBroadcast(this, 0, new Intent("com.my.button.next").putExtra("tag",2), PendingIntent.FLAG_UPDATE_CURRENT));

Bitmap bitmap = MediaUtil.getArtwork(this, id,
albumId, true, false);// 获取专辑位图对象,为大图
contentView.setImageViewBitmap(R.id.iv1,bitmap);
builder.setContent(contentView).setSmallIcon(R.mipmap.ic_launcher);
nf=builder.build();
nm.notify(0,nf);
}
@Override
public IBinder onBind(Intent intent) {
Log.d("====", "服务启动--绑定方式");
return new MyBind();
}

@Override
public boolean onUnbind(Intent intent) {
Log.d("====", "服务启动--绑定方式--解除绑定");
return super.onUnbind(intent);
}

@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
Log.d("====", "服务销毁");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: