我这个seekbar比较简单,是访问自己放进raw文件里的音乐文件;通过Mediapaly实现后台播放。
用if语句判断图片实现切换歌曲,seekbar可以拖动歌曲进度,用
Duration=mp4.getDuration(); //获取音乐长度
sb.setMax(Duration); //设置SeekBar最大值为音乐文件持续时间
请实现建立raw文件夹放入歌曲Mp3,并放入相关图片。
XML界面: (drawable的都是自己的图片,你可以相应修改)
<?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"
android:background="@mipmap/musicbg"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/zjpic_2"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:src="@drawable/zjad1"/>
<TextView
android:id="@+id/song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌曲名"
android:textSize="20sp"/>
<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/start_2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/bt_4"
android:text="播放音乐"/>
<Button
android:id="@+id/stop_2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/bt_4"
android:text="暂停播放"/>
<Button
android:id="@+id/last_2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/bt_4"
android:text="上一首"/>
<Button
android:id="@+id/next_2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:background="@drawable/bt_4"
android:text="下一首"/>
</LinearLayout>
<Button
android:id="@+id/tuichu_1"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_margin="25dp"
android:layout_height="40dp"
android:gravity="center"
android:background="@drawable/bt_4"
android:text="退出"/>
</LinearLayout>
效果图:
java逻辑页面:
package com.example.myapplication3;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MusicActivity extends AppCompatActivity {
/** Called when the activity is first created. */
Button play,pause,last,next,exit;
MediaPlayer mp1,mp2,mp3,mp4;
SeekBar sb;
Handler handler=new Handler();
int Duration;
private ImageView mbpicture1;
private int id=R.drawable.zjad1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
mbpicture1=(ImageView) findViewById(R.id.zjpic_2);
play=(Button)findViewById(R.id.start_2);
pause=(Button)findViewById(R.id.stop_2);
last=(Button)findViewById(R.id.last_2);
next=(Button)findViewById(R.id.next_2);
exit=(Button)findViewById(R.id.tuichu_1);
sb=(SeekBar)findViewById(R.id.sb);
//找到相应View
mp1 =MediaPlayer.create(this,Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.zaijiananda));
mp2 =MediaPlayer.create(this,Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.rangwoliuzainishenbian));
mp3 =MediaPlayer.create(this,Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.aidehuiguixian));
mp4 =MediaPlayer.create(this,Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.zhoushengdayu));
//后面的参数必须是URI形式的,所以要把相应路径转换成URI
play.setOnClickListener(playlis);
pause.setOnClickListener(pauselis);
last.setOnClickListener(lastlis);
next.setOnClickListener(nextlis);
exit.setOnClickListener(exitlis);
sb.setOnSeekBarChangeListener(sbLis);
if (id==R.drawable.zjad1){
//监听器
Duration=mp1.getDuration();
//音乐文件持续时间
}else if (id==R.drawable.cyx){
Duration=mp2.getDuration();
}else if (id==R.drawable.aiqinggongyu){
Duration=mp3.getDuration();
}else if (id==R.drawable.dayuhaitang){
Duration=mp4.getDuration();
}
sb.setMax(Duration);
//设置SeekBar最大值为音乐文件的持续时间
}
//start的按钮监听
private OnClickListener playlis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(start);
//调用handler播放
}
};
//利用Runable线程;开始播放
Runnable start=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if (id==R.drawable.zjad1){
mp1.start();
}else if (id==R.drawable.cyx){
mp2.start();
}else if (id==R.drawable.aiqinggongyu){
mp3.start();
}else if (id==R.drawable.dayuhaitang){
mp4.start();
}
handler.post(updatesb);
//用一个handler更新SeekBar
}
};
//下一首
Runnable next_3=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if (id==R.drawable.zjad1) {
id = R.drawable.cyx;
handler.post(start);mp1.pause();
}else if (id==R.drawable.cyx) {
id = R.drawable.aiqinggongyu;
handler.post(start);mp2.pause();
}else if (id==R.drawable.aiqinggongyu) {
id = R.drawable.dayuhaitang;
handler.post(start);mp3.pause();
}else if (id==R.drawable.dayuhaitang) {
id = R.drawable.zjad1;
handler.post(start);mp4.pause();
}
mbpicture1.setImageResource(id);
handler.post(updatesb);
//用一个handler更新SeekBar
}
};
//上一首
Runnable last_3=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if (id==R.drawable.zjad1) {
id = R.drawable.dayuhaitang;
handler.post(start);mp1.pause();
}else if (id==R.drawable.dayuhaitang) {
id = R.drawable.aiqinggongyu;
handler.post(start);mp4.pause();
}else if (id==R.drawable.aiqinggongyu) {
id = R.drawable.cyx;
handler.post(start);mp3.pause();
}else if (id==R.drawable.cyx) {
id = R.drawable.zjad1;
handler.post(start);mp2.pause();
}
mbpicture1.setImageResource(id);
handler.post(updatesb);
//用一个handler更新SeekBar
}
};
//更新进度条
Runnable updatesb =new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if (id==R.drawable.zjad1){
sb.setProgress(mp1.getCurrentPosition());
}else if (id==R.drawable.cyx){
sb.setProgress(mp2.getCurrentPosition());
}else if (id==R.drawable.aiqinggongyu){
sb.setProgress(mp3.getCurrentPosition());
}else if (id==R.drawable.dayuhaitang){
sb.setProgress(mp4.getCurrentPosition());
}
handler.postDelayed(updatesb, 1000);
//每秒钟更新一次
}
};
//暂停事件的监听
private OnClickListener pauselis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp1.pause();mp2.pause();mp3.pause();mp4.pause();
//暂停
}
};
//下一首事件的监听
private OnClickListener nextlis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(next_3);
}
};
//上一首事件的监听
private OnClickListener lastlis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(last_3);
}
};
//seekbar事件的监听
private OnSeekBarChangeListener sbLis=new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (id==R.drawable.zjad1){
mp1.seekTo(sb.getProgress());
//SeekBar确定位置后,跳到指定位置
}else if (id==R.drawable.cyx){
mp2.seekTo(sb.getProgress());
}else if (id==R.drawable.aiqinggongyu) {
mp3.seekTo(sb.getProgress());
}else if (id==R.drawable.dayuhaitang) {
mp4.seekTo(sb.getProgress());
}
}
};
//退出事件的监听
private OnClickListener exitlis=new OnClickListener() {
@Override
public void onClick(View view) {
mp1.reset();mp2.reset();mp3.reset();mp4.reset();
Intent intent=new Intent(MusicActivity.this,MainActivity.class);
startActivity(intent);
}
};
}
我没有写Mediaplay.releas()释放资源,总是app闪退 ,有大佬知道因为啥吗?
感谢感谢