簡易音樂播放器,帶seekBar,可滑動調節播放進度。
阿新 • • 發佈:2019-01-25
閒言少敘,進入正題,這個也不能稱之為播放器,只是播放 /res/raw 下的音樂檔案。
因為是初學者,所以遇到的問題比較多,很多都是不應該出現的問題,但是好在都解決了
我在程式碼中的註釋我覺得夠詳細的了,希望跟我一樣的初學者能少走一些彎路
遇到的問題:
1. 暫停之後恢復播放不能在暫停位置播放
2. 當滑動SeekBar時會因為和播放時不斷更新的SeekBar進度發生衝突
XML
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.yipai.musicplay.MainActivity">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height ="wrap_content" />
<TextView
android:id="@+id/now_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="當前時間"
android:textSize="25sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放音訊">
</Button>
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止播放" />
</LinearLayout>
</LinearLayout>
JAVA
package com.example.yipai.musicplay;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
private TextView now_time;
private SeekBar audio_seekBar;
private Button btn_start_audio;
private Button btn_stop_audio;
private MediaPlayer m;
private Context context = MainActivity.this;
private Thread thread;
//記錄播放位置
private int time;
//記錄是否暫停
private boolean flage = false, isChanging = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Media控制元件設定
m = new MediaPlayer();
init();
}
//Activity從後臺重新回到前臺時被呼叫
@Override
protected void onRestart() {
super.onRestart();
if (m != null) {
if (m.isPlaying()) {
m.start();
}
}
}
//Activity被覆蓋到下面或者鎖屏時被呼叫
@Override
protected void onPause() {
super.onPause();
if (m != null) {
if (m.isPlaying()) {
m.pause();
}
}
}
@Override
protected void onResume() {
super.onResume();
if (m != null) {
if (!m.isPlaying()) {
m.start();
}
}
}
//Activity被銷燬
protected void onDestroy() {
if (m.isPlaying()) {
m.stop();//停止音訊的播放
}
m.release();//釋放資源
super.onDestroy();
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
if (m.isPlaying()) {
//m.getCurrentPosition();獲取當前播放位置
time = m.getCurrentPosition();
// 如果正在播放,則暫停,並把按鈕上的文字設定成“暫停”
m.pause();
btn_start_audio.setText("暫停");
flage = true;//flage 標記為 ture
} else if (flage) {
m.start();//先開始播放
m.seekTo(time);//設定從哪裡開始播放
btn_start_audio.setText("播放");
flage = false;
} else {
m.reset();//恢復到未初始化的狀態
m = MediaPlayer.create(context, R.raw.kids);//讀取音訊
audio_seekBar.setMax(m.getDuration());//設定SeekBar的長度
try {
m.prepare(); //準備
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.start(); //播放
// 建立一個執行緒
btn_start_audio.setText("播放");
}
thread = new Thread(new SeekBarThread());
// 啟動執行緒
thread.start();
break;
case R.id.Button02:
m.stop();
audio_seekBar.setProgress(0);
break;
}
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
now_time.setText("當前播放時間" + ShowTime(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//防止在拖動進度條進行進度設定時與Thread更新播放進度條衝突
isChanging = true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
now_time.setText("當前播放時間" + ShowTime(seekBar.getProgress()));
//將media進度設定為當前seekbar的進度
m.seekTo(seekBar.getProgress());
isChanging = false;
thread = new Thread(new SeekBarThread());
// 啟動執行緒
thread.start();
}
// 自定義的執行緒
class SeekBarThread implements Runnable {
@Override
public void run() {
while (!isChanging && m.isPlaying()) {
// 將SeekBar位置設定到當前播放位置
audio_seekBar.setProgress(m.getCurrentPosition());
try {
// 每100毫秒更新一次位置
Thread.sleep(100);
//播放進度
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//時間顯示函式,我們獲得音樂資訊的是以毫秒為單位的,把把轉換成我們熟悉的00:00格式
public String ShowTime(int time) {
time /= 1000;
int minute = time / 60;
int hour = minute / 60;
int second = time % 60;
minute %= 60;
return String.format("%02d:%02d", minute, second);
}
private void init() {
audio_seekBar = (SeekBar) findViewById(R.id.seekBar);
btn_start_audio = (Button) findViewById(R.id.Button01);
btn_stop_audio = (Button) findViewById(R.id.Button02);
now_time = (TextView) findViewById(R.id.now_time);
btn_start_audio.setOnClickListener(new ClickEvent());
btn_stop_audio.setOnClickListener(new ClickEvent());
audio_seekBar.setOnSeekBarChangeListener(this);
}
}