Android开发之本地音乐播放器(二)

article/2025/10/24 1:48:10

此次音乐播放器是针对上一个:https://blog.csdn.net/qq_43433255/article/details/88084420
开发出来的一个功能增强型,基本实现功能为:

  1. 通过列表管理SD中的音乐文件。
  2. 通过seekbar可以显示一首音乐的播放进度。
  3. 通过seekbar实现音乐播放的进度控制
  4. 根据不同的音乐切换背景图片
    当然最近有小伙伴找我要全部代码,因为后面有些代码是截图,我已经上传了:https://download.csdn.net/download/qq_43433255/11203930
    (我发现,这个积分好像是自动默认的,以前能设置积分)
    界面设计部分:
    在这里插入图片描述
    在这里插入图片描述
    activity_main的代码为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.shaoyangyang.shaoshuaih.MainActivity"><LinearLayoutandroid:id="@+id/main_top_linlayout"android:layout_width="match_parent"android:layout_height="50dp"android:background="@color/reddemo"><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/main_menu_imgv"android:layout_width="30dp"android:layout_height="30dp"android:layout_centerVertical="true"android:layout_marginLeft="15dp"android:src="@mipmap/ic_menu" /><TextViewandroid:id="@+id/main_logic_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"android:text="我的音乐"android:textColor="@color/white"android:textSize="25dp" /></RelativeLayout></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/main_online_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:text="在线音乐"android:textColor="@color/white_60P"android:textSize="25dp" /><ImageViewandroid:id="@+id/main_search_imgv"android:layout_width="40dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"android:src="@mipmap/ic_menu_search" /></RelativeLayout></LinearLayout></LinearLayout><android.support.v4.view.ViewPagerandroid:id="@+id/main_vp"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.v4.view.ViewPager></LinearLayout>

acticity_music的代码:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.shaoyangyang.shaoshuaih.MusicActivity"><ImageViewandroid:id="@+id/music_bg_imgv"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="centerCrop"android:src="@mipmap/touxiang1" /><RelativeLayoutandroid:id="@+id/music_relativelayout"android:layout_width="match_parent"android:layout_height="50dp"><ImageViewandroid:id="@+id/music_down_imgv"android:layout_width="50dp"android:layout_height="50dp"android:layout_alignParentLeft="true"android:layout_margin="10dp"android:src="@mipmap/ic_arrow_down" /><TextViewandroid:id="@+id/music_title_tv"android:layout_width="match_parent"android:layout_height="28dp"android:layout_toRightOf="@+id/music_down_imgv"android:text="周杰伦"android:textColor="#ffffff"android:textSize="20sp" /><TextViewandroid:id="@+id/music_artist_tv"android:layout_width="match_parent"android:layout_height="22dp"android:layout_below="@+id/music_title_tv"android:layout_marginLeft="8dp"android:layout_toRightOf="@+id/music_down_imgv"android:text="告白气球"android:textColor="#ffffff"android:textSize="15sp" /></RelativeLayout><RelativeLayoutandroid:layout_width="350dp"android:layout_height="500dp"android:layout_above="@+id/music_linlayout"android:layout_below="@+id/music_relativelayout"android:layout_centerHorizontal="true"android:layout_centerVertical="true"><ImageViewandroid:id="@+id/music_disc_imagv"android:layout_width="match_parent"android:layout_height="300dp"android:layout_marginTop="100dp"android:src="@mipmap/play_page_disc" /><ImageViewandroid:id="@+id/music_needle_imag"android:layout_width="150dp"android:layout_height="159dp"android:layout_marginLeft="132dp"android:src="@mipmap/play_page_needle" /></RelativeLayout><LinearLayoutandroid:id="@+id/music_linlayout"android:layout_width="match_parent"android:layout_height="90dp"android:layout_alignParentBottom="true"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="5"android:orientation="horizontal"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/music_current_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:text="00:00"android:textColor="#ffffff" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_toLeftOf="@+id/music_total_tv"android:layout_toRightOf="@+id/music_current_tv"><SeekBarandroid:id="@+id/music_seekbar"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout><TextViewandroid:id="@+id/music_total_tv"android:layout_width="50dp"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:layout_alignParentRight="true"android:text="10:20"android:textColor="#ffffff" /></RelativeLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="10"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/music_play_btn_loop_img"android:layout_width="60dp"android:layout_height="60dp"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:src="@mipmap/ic_play_btn_loop" /><ImageViewandroid:id="@+id/music_prev_imgv"android:layout_width="60dp"android:layout_height="60dp"android:layout_centerVertical="true"android:layout_toRightOf="@+id/music_play_btn_loop_img"android:src="@drawable/seach_btn_pre" /><ImageViewandroid:id="@+id/music_pause_imgv"android:layout_width="60dp"android:layout_height="60dp"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:src="@drawable/seach_btn_pause" /><ImageViewandroid:id="@+id/music_next_imgv"android:layout_width="60dp"android:layout_height="60dp"android:layout_centerVertical="true"android:layout_marginLeft="25dp"android:layout_toRightOf="@id/music_pause_imgv"android:src="@drawable/seach_btn_next" /></RelativeLayout></LinearLayout></LinearLayout></RelativeLayout>

mainactivity.java的代码:

package com.example.shaoyangyang.shaoshuaih;import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.media.Image;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;import com.example.shaoyangyang.shaoshuaih.adapter.MusicPagerAdapter;
import com.example.shaoyangyang.shaoshuaih.fragment.LogicFragment;
import com.example.shaoyangyang.shaoshuaih.fragment.OnlineFragment;import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;//实现OnClickListener的接口
public class MainActivity extends AppCompatActivity implements View.OnClickListener {//定义activity_main.xml的控件对象private TextView logicTv;private TextView onlineTv;private ViewPager viewPager;private ImageView menuImagv;private ImageView seachImagv;private Calendar calendar;//将Fragment放入List集合中,存放fragment对象private List<Fragment> fragmentList = new ArrayList<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//绑定idbangdingID();//设置监听jianting();//创建fragment对象LogicFragment logicFragment = new LogicFragment();OnlineFragment onlineFragment = new OnlineFragment();//将fragment对象添加到fragmentList中fragmentList.add(logicFragment);fragmentList.add(onlineFragment);//通过MusicPagerAdapter类创建musicPagerAdapter的适配器,下面我将添加MusicPagerAdapter类的创建方法MusicPagerAdapter musicPagerAdapter = new MusicPagerAdapter(getSupportFragmentManager(), fragmentList);//viewPager绑定适配器viewPager.setAdapter(musicPagerAdapter);viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {switch (position) {case 0:logicTv.setTextColor(getResources().getColor(R.color.white));onlineTv.setTextColor(getResources().getColor(R.color.white_60P));break;case 1:onlineTv.setTextColor(getResources().getColor(R.color.white));logicTv.setTextColor(getResources().getColor(R.color.white_60P));break;}}@Overridepublic void onPageScrollStateChanged(int state) {}});}private void jianting() {logicTv.setOnClickListener(this);onlineTv.setOnClickListener(this);menuImagv.setOnClickListener(this);seachImagv.setOnClickListener(this);}private void bangdingID() {logicTv = findViewById(R.id.main_logic_tv);onlineTv = findViewById(R.id.main_online_tv);viewPager = findViewById(R.id.main_vp);menuImagv = findViewById(R.id.main_menu_imgv);seachImagv = findViewById(R.id.main_search_imgv);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.main_logic_tv://实现点击TextView切换fragmentviewPager.setCurrentItem(0);break;case R.id.main_online_tv:viewPager.setCurrentItem(1);break;case R.id.main_menu_imgv:calendar = Calendar.getInstance();int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);int day = calendar.get(Calendar.DAY_OF_MONTH);DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {@Overridepublic void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {Toast.makeText(MainActivity.this, "所选择的日期是:" + year + "-" + (month + 1) + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();}}, year, month, day);datePickerDialog.show();break;case R.id.main_search_imgv:calendar = Calendar.getInstance();int hour = calendar.get(Calendar.HOUR);int minute = calendar.get(Calendar.MINUTE);TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {@Overridepublic void onTimeSet(TimePicker view, int hourOfDay, int minute) {Toast.makeText(MainActivity.this, "所选择的时间是:" + hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();}}, hour, minute, true);timePickerDialog.show();break;default:break;}}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {final View view = LayoutInflater.from(this).inflate(R.layout.dialog1_layout, null);AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("退出提示");builder.setView(view);builder.setPositiveButton("能", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {finish();}});builder.setNegativeButton("不能", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "且看曼城分分 钟打你脸", Toast.LENGTH_SHORT).show();}});builder.show();}return super.onKeyDown(keyCode, event);}
}

musicactivity。java的代码:

package com.example.shaoyangyang.shaoshuaih;import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;import com.example.shaoyangyang.shaoshuaih.entity.Music;
import com.example.shaoyangyang.shaoshuaih.utils.BlurUtil;
import com.example.shaoyangyang.shaoshuaih.utils.Common;
import com.example.shaoyangyang.shaoshuaih.utils.MergeImage;import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;public class MusicActivity extends AppCompatActivity implements View.OnClickListener {//定义activity_music.xml的控件对象//设置音乐播放模式private int i = 0;private int playMode = 0;private int buttonWitch = 0;private ImageView bgImgv;private TextView titleTv;private TextView artistTv;private TextView currrentTv;private TextView totalTv;private ImageView prevImgv;private ImageView nextImgv;private int position;private ImageView discImagv;private ImageView needleImagv;private MediaPlayer mediaPlayer;private ImageView pauseImgv;private ImageView downImg;private ImageView styleImg;private SeekBar seekBar;private int totaltime;private boolean isStop;private ObjectAnimator objectAnimator = null;private RotateAnimation rotateAnimation = null;private RotateAnimation rotateAnimation2 = null;private String TAG = "MusicActivity";//Handler实现向主线程进行传值private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);seekBar.setProgress((int) (msg.what));currrentTv.setText(formatTime(msg.what));}};//MusicActivity onCreate()方法@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_music);bingID();                                                                           //调用bingID();方法实现对控件的绑定Intent intent = getIntent();                                                    //通过getIntent()方法实现intent信息的获取position = intent.getIntExtra("position", 0);            //获取positionmediaPlayer = new MediaPlayer();prevAndnextplaying(Common.musicList.get(position).path);seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {               //seekbar设置监听,实现指哪放到哪@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {if (fromUser) {mediaPlayer.seekTo(progress);}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}});}//prevAndnext() 实现页面的展现private void prevAndnextplaying(String path) {isStop = false;mediaPlayer.reset();titleTv.setText(Common.musicList.get(position).title);artistTv.setText(Common.musicList.get(position).artist + "--" + Common.musicList.get(position).album);pauseImgv.setImageResource(R.mipmap.ic_play_btn_pause);if (Common.musicList.get(position).albumBip != null) {Bitmap bgbm = BlurUtil.doBlur(Common.musicList.get(position).albumBip, 10, 5);//将专辑虚化bgImgv.setImageBitmap(bgbm);                                    //设置虚化后的专辑图片为背景Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.play_page_disc);//BitmapFactory.decodeResource用于根据给定的资源ID从指定的资源文件中解析、创建Bitmap对象。Bitmap bm = MergeImage.mergeThumbnailBitmap(bitmap1, Common.musicList.get(position).albumBip);//将专辑图片放到圆盘中discImagv.setImageBitmap(bm);} else {Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.touxiang1);bgImgv.setImageBitmap(bitmap);Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.play_page_disc);Bitmap bm = MergeImage.mergeThumbnailBitmap(bitmap1, bitmap);discImagv.setImageBitmap(bm);}try {mediaPlayer.setDataSource(path);mediaPlayer.prepare();                   // 准备mediaPlayer.start();                        // 启动mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {@RequiresApi(api = Build.VERSION_CODES.KITKAT)@Overridepublic void onCompletion(MediaPlayer mp) {if(!mediaPlayer.isPlaying()){setPlayMode();}}});} catch (IllegalArgumentException | SecurityException | IllegalStateException| IOException e) {e.printStackTrace();}totalTv.setText(formatTime(Common.musicList.get(position).length));seekBar.setMax(Common.musicList.get(position).length);MusicThread musicThread = new MusicThread();                                         //启动线程new Thread(musicThread).start();//实例化,设置旋转对象objectAnimator = ObjectAnimator.ofFloat(discImagv, "rotation", 0f, 360f);//设置转一圈要多长时间objectAnimator.setDuration(8000);//设置旋转速率objectAnimator.setInterpolator(new LinearInterpolator());//设置循环次数 -1为一直循环objectAnimator.setRepeatCount(-1);//设置转一圈后怎么转objectAnimator.setRepeatMode(ValueAnimator.RESTART);objectAnimator.start();rotateAnimation = new RotateAnimation(-25f, 0f, Animation.RELATIVE_TO_SELF, 0.3f, Animation.RELATIVE_TO_SELF, 0.1f);rotateAnimation.setDuration(500);rotateAnimation.setInterpolator(new LinearInterpolator());rotateAnimation.setRepeatCount(0);rotateAnimation.setFillAfter(true);rotateAnimation.setStartOffset(500);needleImagv.setAnimation(rotateAnimation);rotateAnimation.cancel();rotateAnimation2 = new RotateAnimation(0f, -25f, Animation.RELATIVE_TO_SELF, 0.3f, Animation.RELATIVE_TO_SELF, 0.1f);rotateAnimation2.setDuration(500);rotateAnimation2.setInterpolator(new LinearInterpolator());rotateAnimation2.setRepeatCount(0);rotateAnimation2.setFillAfter(true);needleImagv.setAnimation(rotateAnimation2);rotateAnimation2.cancel();}@RequiresApi(api = Build.VERSION_CODES.KITKAT)private void setPlayMode() {if (playMode == 0)//全部循环{if (position == Common.musicList.size() - 1)//默认循环播放{position = 0;// 第一首mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);} else {position++;mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);}} else if (playMode == 1)//单曲循环{//position不需要更改mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);} else if (playMode == 2)//随机{position = (int) (Math.random() * Common.musicList.size());//随机播放mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);}}@TargetApi(Build.VERSION_CODES.KITKAT)private void setBtnMode() {if (playMode == 0)//全部循环{if (position == Common.musicList.size() - 1)//默认循环播放{if (buttonWitch == 1) {position--;mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);} else if (buttonWitch == 2) {position = 0;// 第一首mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);}} else if (position == 0) {if (buttonWitch == 1) {position = Common.musicList.size() - 1;mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);} else if (buttonWitch == 2) {position++;mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);}}else {if(buttonWitch ==1){position--;mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);}else if(buttonWitch ==2){position++;mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);}}} else if (playMode == 1)//单曲循环{//position不需要更改mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);} else if (playMode == 2)//随机{position = (int) (Math.random() * Common.musicList.size());//随机播放mediaPlayer.reset();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);prevAndnextplaying(Common.musicList.get(position).path);}}//格式化数字private String formatTime(int length) {Date date = new Date(length);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");    //规定固定的格式String totaltime = simpleDateFormat.format(date);return totaltime;}//绑定id,设置监听private void bingID() {titleTv = findViewById(R.id.music_title_tv);artistTv = findViewById(R.id.music_artist_tv);bgImgv = findViewById(R.id.music_bg_imgv);currrentTv = findViewById(R.id.music_current_tv);totalTv = findViewById(R.id.music_total_tv);prevImgv = findViewById(R.id.music_prev_imgv);nextImgv = findViewById(R.id.music_next_imgv);discImagv = findViewById(R.id.music_disc_imagv);needleImagv = findViewById(R.id.music_needle_imag);pauseImgv = findViewById(R.id.music_pause_imgv);downImg = findViewById(R.id.music_down_imgv);seekBar = findViewById(R.id.music_seekbar);styleImg = findViewById(R.id.music_play_btn_loop_img);pauseImgv.setOnClickListener(this);prevImgv.setOnClickListener(this);nextImgv.setOnClickListener(this);downImg.setOnClickListener(this);styleImg.setOnClickListener(this);}//onClick()点击监听@RequiresApi(api = Build.VERSION_CODES.KITKAT)@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.music_prev_imgv:buttonWitch = 1;setBtnMode();break;case R.id.music_next_imgv:buttonWitch = 2;setBtnMode();break;case R.id.music_pause_imgv:if (mediaPlayer.isPlaying()) {mediaPlayer.pause();objectAnimator.pause();needleImagv.startAnimation(rotateAnimation2);pauseImgv.setImageResource(R.mipmap.ic_play_btn_play);} else {mediaPlayer.start();objectAnimator.resume();needleImagv.startAnimation(rotateAnimation);pauseImgv.setImageResource(R.mipmap.ic_play_btn_pause);}break;case R.id.music_play_btn_loop_img:i++;if (i % 3 == 1) {Toast.makeText(MusicActivity.this, "单曲循环", Toast.LENGTH_SHORT).show();playMode = 1;styleImg.setImageResource(R.mipmap.ic_play_btn_one);}if (i % 3 == 2) {Toast.makeText(MusicActivity.this, "随机播放", Toast.LENGTH_SHORT).show();playMode = 2;styleImg.setImageResource(R.mipmap.ic_play_btn_shuffle);}if (i % 3 == 0) {Toast.makeText(MusicActivity.this, "顺序播放", Toast.LENGTH_SHORT).show();playMode = 0;styleImg.setImageResource(R.mipmap.ic_play_btn_loop);}break;case R.id.music_down_imgv:this.finish();break;default:break;}}@Overrideprotected void onPause() {super.onPause();for (Music music : Common.musicList) {music.isPlaying = false;}Common.musicList.get(position).isPlaying = true;}@Overrideprotected void onStop() {super.onStop();}@Overrideprotected void onDestroy() {super.onDestroy();i = 0;isStop = false;if (mediaPlayer.isPlaying()) {mediaPlayer.stop();}if (mediaPlayer != null) {mediaPlayer.stop();}}//创建一个类MusicThread实现Runnable接口,实现多线程class MusicThread implements Runnable {@Overridepublic void run() {while (!isStop && Common.musicList.get(position) != null) {try {//让线程睡眠1000毫秒Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}//放送给Handler现在的运行到的时间,进行ui更新handler.sendEmptyMessage(mediaPlayer.getCurrentPosition());}}}}

代码繁多:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

贴上一些关键代码提供参考!

最后,音乐播放器的效果图如下:
在这里插入图片描述在这里插入图片描述


http://chatgpt.dhexx.cn/article/lFlDMK1F.shtml

相关文章

Android音乐播放器源码

转载自&#xff1a;https://blog.csdn.net/xch_yang/article/details/103916201 android开发音乐播放器&#xff0c;能够很好的应用Android基础知识&#xff0c;是个不错的开发选择&#xff0c;部分界面如下 这个项目很好的结合了Android的四大组件知识。其中音乐来源于手机本…

本地音乐播放器+android8.1,APlayer v1.5.6.8-15681 安卓本地音乐播放器 | 智享阁

应用简介 APlayer是一款安卓本地音乐播放器&#xff0c;遵循 Material Design 设计&#xff0c;UI清新简洁、操作简单、功能齐全。 应用特点 首页Tab可配置&#xff0c;最多支持五个,包括歌曲、艺术家、专辑、文件夹、播放列表 专辑、艺术家封面自动补全 支持显示本地和在线歌词…

大作业---Android本地音乐播放器开发知识点19145120

步骤 系列文章前言1. 简述程序、进程、线程的区别和联系。&#xff08;10分&#xff09;2. 什么是UI线程&#xff1f;&#xff1f;什么是工作线程&#xff1f;&#xff08;10分&#xff09;3. 如何定义1个结构良好可控的工作线程&#xff08;源代码说明&#xff09;&#xff1f…

Android入门之本地音乐播放器

功能简介 读取模拟器中音乐文件&#xff1b;列表展示&#xff0c;获取歌曲详细信息&#xff1a;歌名、歌手名、专辑名、专辑封面、播放时间等&#xff1b;基本功能&#xff1a;上下切歌&#xff0c;播放暂停、进度条显示与点击跳转。成品效果图&#xff1a; Mediaplayer基本…

大学生安卓期末设计之本地音乐播放器

作为一个热爱设计交互性产品的男大学生&#xff0c;我真的不太爱听课&#xff0c;所以很多时候&#xff0c;需要一个人学会阅读搜索资料并不停尝试&#xff0c;我希望大家也多多热爱这样持之以恒的尝试。 先看产品的一个模拟机测试效果&#xff08;b站南阳洛信也有视频) 安卓A…

Android本地音乐播放器

UI界面模仿QQ音乐 实现一个简单的本地播放器&#xff0c;功能包括&#xff1a;播放&#xff0c;暂停&#xff0c;上一曲&#xff0c;下一曲&#xff0c;进度条。 功能实现 读取本地音乐 1.创建一个Song类 public class Song {public String song;//歌曲名public String sing…

毕业设计- 基于Android的本地音乐播放器

—— 木叶飞舞之处&#xff0c;火亦生生不息。 项目介绍 本系统支持扫瞄本地音乐播放、暂停、上一首、下一首&#xff0c;点击播放栏跳转到播放页面&#xff0c;可查看当前播放列表&#xff0c;设置播放模式如顺序播放、随机播放&#xff0c;标记为我的喜欢&#xff0c;可以创…

Selenium自动化测试设计模式-PO模式

前言&#xff1a; 在python自动化过程中&#xff0c;Selenium自动化测试中有一个名字常常被提及PageObject&#xff08;思想与面向对象的特性相同&#xff09;&#xff0c;通过PO模式可以大大提高测试用例的维护效率。 不了解po设计模式的可自行百度 面向对象的特性&#xf…

电商项目测试实战(十一)后台业务场景测试设计

后台订单处理业务场景测试设计 流程步骤&#xff1a; 设计测试用例&#xff1a; 第一步&#xff1a;绘制流程图 1、确认业务中的操作 2、分析执行的顺序 3、按照业务方向进行连线 收到前台订单&#xff08;商城->订单->订单列表&#xff09;订单确认发货&#xff0…

电商项目测试实战(十)前台下单业务场景测试设计

前台下单业务场景测试设计 流程步骤&#xff1a; 设计测试用例&#xff1a; 第一步&#xff1a;需求分析 第二步&#xff1a;绘制流程图 1、确定业务中的操作 2、分析执行的顺序 3、按照业务方向进行连线 登录->选购商品->加入购物车->支付->确认订单->等待…

空调测试用例设计

** 空调测试用例设计 ** 界面测试 功能测试 可靠性测试 易用性测试 可维护性测试 兼容性测试 竞品测试 负载压力测试 稳定性测试 文档测试 界面测试&#xff1a; 空调外观的美观性空调外观尺寸是否和设计尺寸一致遥控按钮是否清晰和易懂显示温度及模式的显示屏是否显示设备…

测试场景设计-登录设计

来一波广告&#xff1a;欢迎关注测者说&#xff0c;测试理论知识尽在此处 面试中经常被问到有一个登录页面&#xff0c;你怎么设计测试场景&#xff0c;原来可以做到这么细致。 具体需求&#xff1a; 有一个登陆页面&#xff0c;&#xff08;假如上面有2个textbox,一个提交按钮…

基于微信小程序的大学生心理健康测试设计与实现 .docx

目录 1 绪论 3 1.1 项目开发背景 3 1.2 项目开发意义 3 1.3 项目主要的内容 4 2 相关技术介绍及系统环境开发条件 5 2.1相关技术介绍 5 2.2系统环境开发条件 6 3 系统的需求分析与设计 6 3.1可行性分析 7 3.2需求分析 7 3.2.1系统总体概述 8 3.2.2功能性需求 8 3.2.3非功能性需…

测试设计技术

本文是根据测试架构师修炼之道&#xff08;第二部分 突破&#xff1a;向软件测试架构师的目标迈进&#xff09;整理的&#xff0c;主要分为5个小部分&#xff1a;测试设计四步走、测试设计软技能、设计技术之控制用例粒度、设计技术之自动化测试、设计技术之探索式测试。学习的…

数字系统的测试与可测试设计(DFT)

数字系统的测试与可测试设计&#xff08;DFT&#xff09; 背景介绍1 Defects1.1 名词解释1.2 缺陷种类1.2.1 Physical Defects物理缺陷1.2.2 Shorting Defects1.2.2.1 Gate-Oxide-Shorts1.2.2.2 Bridge1.2.2.1 Open1.2.2.1 Post-fabrication failures 2 Fault Modelling&#x…

2. 测试分析与测试设计

1. 为什么要做好测试分析和测试设计 以业务驱动测试&#xff1a;当下的测试圈子内&#xff0c;大家一直在强调自动化技术、DevOps等&#xff0c;这些是提高效率和质量的利器&#xff0c;但是所有有效的测试行为&#xff0c;都是建立在对业务需求有正确的理解和分析的基础上的。…

软件测试之---测试设计方法

二、测试设计方法&#xff08;黑盒测试设计方法&#xff1b;白盒测试设计方法&#xff09; 1、等价类划分法 1.1等价类划分法概念 将输入&#xff08;输出&#xff09;域划分成若干个子集合&#xff0c;从划分的子集合中选取代表数据&#xff0c;如果选取的数据测试没有问题&…

测试用例:四步测试设计法

读者提问&#xff1a; 阿常&#xff0c;上节我们讲到测试点不等同于测试用例&#xff0c;想请教你一下&#xff0c;如何把测试点加工成测试用例呢&#xff1f; 目前团队中测试人员针对同样的测试点&#xff0c;编写出来的测试用例各不相同&#xff0c;一千个人就有一千个哈姆雷…

常用测试设计方法

目录 1、按照开发阶段划分&#xff08;1&#xff09;单元测试(模块测试)&#xff08;2&#xff09;集成测试(组装测试)&#xff08;3&#xff09;确认测试(有效性测试)&#xff08;4&#xff09;系统测试&#xff08;5&#xff09;验收测试 2、按照代码运行划分&#xff08;1&a…

大数据基础概述

大数据基础概述 1.前言2.大数据技术发展的三个阶段3.大数据技术面临的问题4.大数据技术数据的特点5.大数据对各方面的影响6.大数据技术的应用领域7.大数据技术的相关概念8.大数据技术的相关产业9.云计算和物联网 1.前言 三次信息浪潮 信息化浪潮发生时间标志解决问题代表企业第…