简易音乐播放器,带seekBar,可滑动调节播放进度。

来源:互联网 发布:Linux系统snmp 编辑:程序博客网 时间:2024/04/29 06:52

闲言少叙,进入正题,这个也不能称之为播放器,只是播放 /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);    }}
0 1