Android开发简易音乐播放器
作者:无落 发布时间:2023-12-26 01:07:03
标签:Android,音乐播放器
这里介绍一个简易的音乐播放器,供大家参考,具体内容如下
效果图如下:
但是,由于这是一个简易版的音乐播放器,所播放的音乐只有一首,且被写死,但,操作却十分简单,方便理解!
这是代码的主要设计:
音乐主要存放在这一个文件中:
下面就来介绍各部分代码:
activity—main。xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#696969">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:id="@+id/title"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="3dp"
android:text="生僻字"
android:textSize="25dp"
android:gravity="center"
android:textColor="#ffffff"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#afafaf"
android:layout_below="@+id/title"/>
<ImageView
android:id="@+id/disc"
android:layout_width="280dp"
android:layout_height="280dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/title"
android:layout_marginTop="50dp"
android:src="@drawable/xcvb" />
<ImageView
android:id="@+id/needle"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_below="@+id/title"
android:layout_marginLeft="150dp"/>
<RelativeLayout
android:id="@+id/music1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/rl"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:gravity="center">
<SeekBar
android:id="@+id/music_seek_bar"
android:layout_width="240dp"
android:layout_height="wrap_content"/>
<TextSwitcher
android:id="@+id/text_switcher"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_toRightOf="@+id/music_seek_bar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="00:00/2:00"
android:textColor="@color/colorAccent"/>
</TextSwitcher>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center"
android:id="@+id/rl"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/playing_pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/music_previous" />
<ImageView
android:id="@+id/playing_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/music_play" />
<ImageView
android:id="@+id/playing_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/music_next" />
</LinearLayout>
</RelativeLayout>
main。activity部分:
package com.example.cungu.musicdemo;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextSwitcher;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
Runnable, ServiceConnection, SeekBar.OnSeekBarChangeListener {
private ImageView disc,needle,playingPre,playingPlay,playingNext;
private ObjectAnimator discAnimation,needleAnimation;//自定义指针和唱盘
private boolean isPlaying = true;//0,1 判断是否处于播放状态
//声明服务
private static final String TAG = MainActivity.class.getSimpleName();
private MediaService.MusicController mMusicController;
//使用方法:mMusicController.play();播放 mMusicController.pause();暂停
private boolean running;
private TextSwitcher mSwitcher;
private SeekBar mSeekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置透明栏
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
//滑动条部分
mSeekBar = (SeekBar) findViewById(R.id.music_seek_bar);
mSeekBar.setOnSeekBarChangeListener(this);
mSwitcher = (TextSwitcher) findViewById(R.id.text_switcher);
mSwitcher.setInAnimation(this, android.R.anim.fade_in);
mSwitcher.setOutAnimation(this, android.R.anim.fade_out);
Intent intent = new Intent(this, MediaService.class);
//增加StartService,来增加后台播放功能
startService(intent);
// 绑定服务,使用context来绑定
// 那个界面需要绑定 就用哪个 Activity
// 参数1:Intent 代表需要绑定哪一个Service
// 参数2:ServiceConnection 回调接口,可以接收到Service连接成功和断开的回调,成功就可以取到对象。
// 绑定服务 参数2就是服务和指定的对象绑定在一起
bindService(intent, this, BIND_AUTO_CREATE);
//指针和唱片部分
initViews();//定义背景图
setAnimations();
}
private void initViews() {
playingPre = (ImageView) findViewById(R.id.playing_pre);
playingPlay = (ImageView) findViewById(R.id.playing_play);
playingNext = (ImageView) findViewById(R.id.playing_next);
disc = (ImageView) findViewById(R.id.disc);
needle = (ImageView) findViewById(R.id.needle);
playingPre.setOnClickListener(this);
playingPlay.setOnClickListener(this);
playingNext.setOnClickListener(this);
}
//动画设置
private void setAnimations() {
discAnimation = ObjectAnimator.ofFloat(disc, "rotation", 0, 360);
discAnimation.setDuration(20000);
discAnimation.setInterpolator(new LinearInterpolator());
discAnimation.setRepeatCount(ValueAnimator.INFINITE);
needleAnimation = ObjectAnimator.ofFloat(needle, "rotation", 0, 25);
needle.setPivotX(0);
needle.setPivotY(0);
needleAnimation.setDuration(800);
needleAnimation.setInterpolator(new LinearInterpolator());
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.playing_pre://前一曲
if (discAnimation != null) {
discAnimation.end();
playing();
}
break;
case R.id.playing_play://播放中
if (isPlaying){
playing();
}else {
if (needleAnimation != null) {
needleAnimation.reverse();
needleAnimation.end();
mMusicController.pause();
}
if (discAnimation != null && discAnimation.isRunning()) {
discAnimation.cancel();
mMusicController.pause();
float valueAvatar = (float) discAnimation.getAnimatedValue();
discAnimation.setFloatValues(valueAvatar, 360f + valueAvatar);
}
playingPlay.setImageResource(R.drawable.music_play);
isPlaying = true;
}
break;
case R.id.playing_next://下一曲
if (discAnimation != null) {
discAnimation.end();
playing();
}
break;
default:
break;
}
}
//播放:1、播放音乐 2、动画旋转 3、暂停图片切换为播放按钮图片
private void playing(){
needleAnimation.start();
discAnimation.start();
playingPlay.setImageResource(R.drawable.music_pause);
mMusicController.play();//播放
isPlaying = false;
}
//===================================播放歌曲服务开启、停止、结束===============================
@Override
protected void onStart() {
super.onStart();
Thread thread = new Thread(this);
thread.start();
}
@Override
protected void onStop() {
running = false;
super.onStop();
}
@Override
protected void onDestroy() {
// 解除绑定
unbindService(this);
super.onDestroy();
}
//---------------------播放到当前音乐的滑动条及时间设置--------------------------
@Override
public void run() {
running = true;
try {
while (running) {
if (mMusicController != null) {
long musicDuration = mMusicController.getMusicDuration();
final long position = mMusicController.getPosition();
final Date dateTotal = new Date(musicDuration);
final SimpleDateFormat sb = new SimpleDateFormat("mm:ss");
mSeekBar.setMax((int) musicDuration);
mSeekBar.setProgress((int) position);
mSwitcher.post(
new Runnable() {
@Override
public void run() {
Date date = new Date(position);
String time = sb.format(date) + "/" + sb.format(dateTotal);
mSwitcher.setCurrentText(time);
}
}
);
}
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//-----------------------------
//服务绑定与解除绑定的回调
/**
* 当服务与当前绑定对象,绑定成功,服务onBind方法调用并且返回之后
* 回调给这个方法
*
* @param name
* @param service IBinder 就是服务 onBind 返回的对象
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMusicController = ((MediaService.MusicController) service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mMusicController = null;
}
public void btnStopService(View view) {
Intent intent = new Intent(this, MediaService.class);
stopService(intent);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mMusicController.setPosition(seekBar.getProgress());
}
}
mediaserver部分的代码:
package com.example.cungu.musicdemo;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
public class MediaService extends Service {
private MediaPlayer mPlayer;
/* 绑定服务的实现流程:
* 1.服务 onCreate, onBind, onDestroy 方法
* 2.onBind 方法需要返回一个 IBinder 对象
* 3.如果 Activity 绑定,Activity 就可以取到 IBinder 对象,可以直接调用对象的方法
*/
// 相同应用内部不同组件绑定,可以使用内部类以及Binder对象来返回。
public class MusicController extends Binder {
public void play() {
mPlayer.start();//开启音乐
}
public void pause() {
mPlayer.pause();//暂停音乐
}
public long getMusicDuration() {
return mPlayer.getDuration();//获取文件的总长度
}
public long getPosition() {
return mPlayer.getCurrentPosition();//获取当前播放进度
}
public void setPosition (int position) {
mPlayer.seekTo(position);//重新设定播放进度
}
}
/**
* 当绑定服务的时候,自动回调这个方法
* 返回的对象可以直接操作Service内部的内容
* @param intent
* @return
*/
@Override
public IBinder onBind(Intent intent) {
return new MusicController();
}
@Override
public void onCreate() {
super.onCreate();
mPlayer = MediaPlayer.create(this, R.raw.yinyue1);
}
/**
* 任意一次unbindService()方法,都会触发这个方法
* 用于释放一些绑定时使用的资源
* @param intent
* @return
*/
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
mPlayer.release();
mPlayer = null;
super.onDestroy();
}
}
到此,这一个简易的音乐播放器,就完成了。
但是一个音乐播放器,至少播放的音乐不会只有一首,所以,可以考虑试做一个本地音乐播放器,读取本地的音乐!
来源:https://blog.csdn.net/qq_43433255/article/details/88084420
0
投稿
猜你喜欢
- 目录登陆界面的实现登陆界面代码Login类login的监听类 LoginListener聊天界面运行图Client类代码Server代码登陆
- C#移除字符串中的不可见Unicode字符 背景最近发现某个数据采集的系统拿下来的数据,有些字段的JSON被莫名截断了,导致后续数
- 0. Iochttps://docs.spring.io/spring-framework/docs/current/spring-fram
- Spring Boot中的那些Conditionalspring boot中为我们提供了丰富的Conditional来让我们得以非常方便的在
- 在使用Java web开发的后端工程师们大多会使用Maven作为项目构建以及编译的工具,微服务和大中台当道的今天,更加关注maven的细节是
- 这篇文章主要介绍了JDK线程池和Spring线程池的使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- Java的List在删除元素时,一般会用list.remove(o)/remove(i)方法。在使用时,容易触碰陷阱,得到意想不到的结果。总
- 对于生成的sql语句 自动加上单引号的情况mybatis是这样的,如果表的字段跟系统字段冲突,写sql语句的时候必须得加上单引号,这样才会区
- 1、注意事项众所周知,对一个文件进行读写操作时,我们需要创建对应的输入流和输出流但需要注意的是,读写操作不能同时进行(边读边写),即不能同时
- 查看和修改线程优先级1.题目JAVA中每个线程都有优化级属性,默认情况下,新建的线程和创建该线程的线程优先级是一样的。当线程调度器选择要运行
- 在spring的注解 @RequestMapping 之下可以直接获取 HttpServletRequest 来获得诸如request he
- 第一部分: 使用idea 打包工程jar 1.准备好一份 开发好的 可执行的 含有main方法的&nbs
- 配置详情pom.xmldependency> <groupId>com.baomidou<
- 前言多数据源的事务处理是个老生常谈的话题,跨两个数据源的事务管理也算是分布式事务的范畴,在同一个JVM里处理多数据源的事务,比较经典的处理方
- 我们都知道mybatis在进行参数判断的时候,直接可以用<if test=""></if> 就可
- Java7引入了Fork Join的概念,来更好的支持并行运算。顾名思义,Fork Join类似与流程语言的分支,合并的概念。也就是说Jav
- 其实这个表示有点不太对,应该是 Druid 动态切换数据源的方法,只是应用在了 springboot 框架中,准备代码准备了半天,之前在一次
- 我们在平常项目开发中,经常会用到周期性定时任务,这个时候使用定时任务就能很方便的实现。在SpringBoot中用得最多的就是Schedule
- 将一个字符串转化成String[]数组,提供两种方法前言将字符串转化成数组提供两种方法:1.split("");2.to
- 目录Spring自动注入失败如何解决?回答注入你的bean使用@Configurable手动查找bean:不推荐Spring自动注入失败如何