持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第16天,点击查看活动详情
前言
本教程是 Android 实现音乐播放器的 Demo 用来练习的案例。
音乐播放器是一种用于播放各种音乐文件的多媒体播放软件。它涵盖了各种音乐格式的播放工具,比如:MP3播放器,WMA播放器,MP4播放器等。它们不仅界面美观,而且操作简单,给用户带来完美的音乐享受。音乐播放器的人性化界面和扩展性才是各种音乐播放器的特色所在。大部分商业版播放软件往往将界面打造得非常华丽,操作也十分简便,但却缺乏扩展性,支持格式较少。开源播放软件一般能够较好地进行扩展,支持较多的音乐格式,但往往界面朴素,难以吸引用户。
1、需求分析
音乐播放器的设计与实现
Android音乐播放器,保证能运行,具有暂停播放,进度条可以正常使用等功能。
2、实现思路
- 在布局文件中使用线性布局
- 添加照片控件、进度条控件、TextView、Button控件等等,并其设置ID
- 在java代码中给Button设置点击事件等等
- 编写MusicService服务代码
- 添加音乐文件,图标 等等
3、目录结构
有MusicService、布局文件、音乐文件、图标等。
4、具体实现
1、具体实现代码
详细代码如下 :
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static SeekBar sb;
private static TextView tv_progress, tv_total;
private ObjectAnimator animator;
private MusicService.MusicControl musicControl;
MyServiceConn conn;
Intent intent;
private boolean isUnbind = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
/**
* 初始化方法
*/
private void init() {
tv_progress = (TextView) findViewById(R.id.tv_progress);
tv_total = (TextView) findViewById(R.id.tv_total);
sb = (SeekBar) findViewById(R.id.sb);
findViewById(R.id.btn_play).setOnClickListener(this);
findViewById(R.id.btn_pause).setOnClickListener(this);
findViewById(R.id.btn_continue_play).setOnClickListener(this);
findViewById(R.id.btn_exit).setOnClickListener(this);
intent = new Intent(this, MusicService.class);
conn = new MyServiceConn();
bindService(intent, conn, BIND_AUTO_CREATE);
/**
* 事件
*/
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress == seekBar.getMax()) {
animator.pause();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
musicControl.seekTo(progress);
}
});
ImageView iv_music = (ImageView) findViewById(R.id.iv_music);
animator = ObjectAnimator.ofFloat(iv_music, "rotation", 0f, 360.0f);
animator.setDuration(10000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(-1);
}
public static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
int duration = bundle.getInt("duration");
int currentPostition = bundle.getInt("currentPosition");
sb.setMax(duration);
sb.setProgress(currentPostition);
int minute = duration / 1000 / 60;
int second = duration / 1000 % 60;
String strMinute = null;
String strSecond = null;
if (minute < 10) {
strMinute = "0" + minute;
} else {
strMinute = minute + "";
}
if (second < 10) {
strSecond = "0" + second;
} else {
strSecond = second + "";
}
tv_total.setText(strMinute + ":" + strSecond);
minute = currentPostition / 1000 / 60;
second = currentPostition / 1000 % 60;
if (minute < 10) {
strMinute = "0" + minute;
} else {
strMinute = minute + "";
}
if (second < 10) {
strSecond = "0" + second;
} else {
strSecond = second + "";
}
tv_progress.setText(strMinute + ":" + strSecond);
}
};
class MyServiceConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
musicControl = (MusicService.MusicControl) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
private void unbind(boolean isUnbind) {
if(!isUnbind) {
musicControl.pausePlay();
unbindService(conn);
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:
musicControl.play();
animator.start();
break;
case R.id.btn_pause:
musicControl.pausePlay();
animator.pause();
break;
case R.id.btn_continue_play:
musicControl.continuePlay();
animator.start();
break;
case R.id.btn_exit:
unbind(isUnbind);
isUnbind = true;
finish();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbind(isUnbind);
}
}
2、服务Musicervice
public class MusicService extends Service {
private MediaPlayer player;
private Timer timer;
public MusicService() {
}
@Override
public IBinder onBind(Intent intent) {
return new MusicControl();
}
@Override
public void onCreate() {
super.onCreate();
player = new MediaPlayer();
}
public void addTimer() {
if (timer == null) {
timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
if (player == null) return;
int duration = player.getDuration();
int currentPosition = player.getCurrentPosition();
Message msg = MainActivity.handler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putInt("duration", duration);
bundle.putInt("currentPosition", currentPosition);
msg.setData(bundle);
com.example.music.MainActivity.handler.sendMessage(msg);
}
};
timer.schedule(task, 5, 500);
}
}
class MusicControl extends Binder {
public void play() {
try {
player.reset();
player = MediaPlayer.create(getApplicationContext(), R.raw.music);
player.start();
addTimer();
} catch (Exception e) {
e.printStackTrace();
}
}
public void pausePlay() {
player.pause();
}
public void continuePlay() {
player.start();
}
public void seekTo(int progress) {
player.seekTo(progress);
}
}
}
Music-Service销毁方法
@Override
public void onDestroy() {
super.onDestroy();
if (player == null) return;
if (player.isPlaying()) player.stop();
player.release();
player = null;
}
3、布局代码
布局代码主要是照片控件、进度条控件、TextView、Button控件等等。
详细代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<!-- 照片控件 -->
<ImageView
android:id="@+id/iv_music"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:src="@drawable/music"
/>
<!-- 进度条控件 -->
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
/>
<TextView
android:id="@+id/tv_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="00:00"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_play"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/btn_bg_selector"
android:text="播放音乐"
/>
<Button
android:id="@+id/btn_pause"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/btn_bg_selector"
android:text="暂停播放"
/>
<Button
android:id="@+id/btn_continue_play"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/btn_bg_selector"
android:text="继续播放"
/>
<Button
android:id="@+id/btn_exit"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/btn_bg_selector"
android:text="退出"
/>
</LinearLayout>
</LinearLayout>
5、效果演示
1、运行截图
2、播放时,进度条也动
总结
以上就是 Android 音乐播放器的设计与实现的全部内容,Android四大组件分别为activity、service、content provider、broadcast receiver,本文主要是Service服务这一控件,希望大家在Android的学习之路更上一层楼。