版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中实现一个音乐播放器
这期内容当中在下将会给大家带来有关怎么在Android中实现一个音乐播放器,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。实现方式:第一步:使用AndroidStudio创建一个Android工程,并且修改activity_main.xml文件<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fyt.musicplayer.MainActivity"
android:orientation="vertical">
<!--显示播放进度-->
<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">
<!--显示当前进度-->
<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>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放音乐"
android:onClick="play"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停播放"
android:onClick="pausePlay"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续播放"
android:onClick="continuePlay"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
android:onClick="exit"/>
</LinearLayout>第二步:新建一个MusicService.java文件,用于处理音乐播放的逻辑package
com.fyt.musicplayer;
import
android.app.Service;
import
android.content.Intent;
import
android.media.MediaPlayer;
import
android.os.Binder;
import
android.os.Bundle;
import
android.os.IBinder;
import
android.os.Message;
import
android.support.annotation.Nullable;
import
java.io.IOException;
import
java.util.Timer;
import
java.util.TimerTask;
//创建一个继承自服务的音乐服务类
public
class
MusicService
extends
Service
{
private
MediaPlayer
player;
private
Timer
timer;
//绑定服务时,调用此方法
@Nullable
@Override
public
IBinder
onBind(Intent
intent)
{
return
new
MusicControl();
}
//创建播放音乐的服务
@Override
public
void
onCreate()
{
super.onCreate();
//创建音乐播放器对象
player
=
new
MediaPlayer();
}
//销毁播放音乐服务
@Override
public
void
onDestroy()
{
super.onDestroy();
//停止播放音乐
player.stop();
//释放占用的资源
player.release();
//将player置为空
player
=
null;
}
//播放音乐
public
void
play()
{
try
{
if(player
==
null)
{
player
=
new
MediaPlayer();
}
//重置
player.reset();
//加载多媒体文件
player.setDataSource("sdcard/zxmzf.mp3");
//准备播放音乐
player.prepare();
//播放音乐
player.start();
//添加计时器
addTimer();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
//暂停播放音乐
public
void
pausePlay()
{
player.pause();
}
//继续播放音乐
public
void
continuePlay()
{
player.start();
}
//创建一个实现音乐接口的音乐控制类
class
MusicControl
extends
Binder
implements
MusicInterface
{
@Override
public
void
play()
{
MusicService.this.play();
}
@Override
public
void
pausePlay()
{
MusicService.this.pausePlay();
}
@Override
public
void
continuePlay()
{
MusicService.this.continuePlay();
}
@Override
public
void
seekTo(int
progress)
{
MusicService.this.seekTo(progress);
}
}
//设置音乐的播放位置
public
void
seekTo(int
progress)
{
player.seekTo(progress);
}
//添加计时器用于设置音乐播放器中的播放进度
public
void
addTimer()
{
//如果没有创建计时器对象
if(timer
==
null)
{
//创建计时器对象
timer
=
new
Timer();
timer.schedule(new
TimerTask()
{
//执行计时任务
@Override
public
void
run()
{
//获得歌曲总时长
int
duration
=
player.getDuration();
//获得歌曲的当前播放进度
int
currentPosition
=
player.getCurrentPosition();
//创建消息对象
Message
msg
=
MainActivity.handler.obtainMessage();
//将音乐的播放进度封装至消息对象中
Bundle
bundle
=
new
Bundle();
bundle.putInt("duration",
duration);
bundle.putInt("currentPosition",
currentPosition);
msg.setData(bundle);
//将消息发送到主线程的消息队列
MainActivity.handler.sendMessage(msg);
}
},
//开始计时任务后的5毫秒,第一次执行run方法,以后每500毫秒执行一次
5,
500);
}
}
}第三步:创建一个MusicInterface.java文件创建用于操作音乐播放的接口package
com.fyt.musicplayer;
//创建一个音乐播放接口
public
interface
MusicInterface
{
//播放音乐
void
play();
//暂停播放音乐
void
pausePlay();
//继续播放音乐
void
continuePlay();
//修改音乐的播放位置
void
seekTo(int
progress);
}第四步:修改MainActivity.java文件package
com.fyt.musicplayer;
import
android.app.Activity;
import
android.content.ComponentName;
import
android.content.Intent;
import
android.content.ServiceConnection;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.IBinder;
import
android.os.Message;
import
android.view.View;
import
android.widget.SeekBar;
import
android.widget.TextView;
public
class
MainActivity
extends
Activity
{
MyServiceConn
conn;
Intent
intent;
MusicInterface
mi;
//用于设置音乐播放器的播放进度
private
static
SeekBar
sb;
private
static
TextView
tv_progress;
private
static
TextView
tv_total;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_progress
=
(TextView)
findViewById(R.id.tv_progress);
tv_total
=
(TextView)
findViewById(R.id.tv_total);
//创建意图对象
intent
=
new
Intent(this,
MusicService.class);
//启动服务
startService(intent);
//创建服务连接对象
conn
=
new
MyServiceConn();
//绑定服务
bindService(intent,
conn,
BIND_AUTO_CREATE);
//获得布局文件上的滑动条
sb
=
(SeekBar)
findViewById(R.id.sb);
//为滑动条添加事件监听
sb.setOnSeekBarChangeListener(new
SeekBar.OnSeekBarChangeListener()
{
//当滑动条中的进度改变后,此方法被调用
@Override
public
void
onProgressChanged(SeekBar
seekBar,
int
progress,
boolean
fromUser)
{
}
//滑动条刚开始滑动,此方法被调用
@Override
public
void
onStartTrackingTouch(SeekBar
seekBar)
{
}
//当滑动条停止滑动,此方法被调用
@Override
public
void
onStopTrackingTouch(SeekBar
seekBar)
{
//根据拖动的进度改变音乐播放进度
int
progress
=
seekBar.getProgress();
//改变播放进度
mi.seekTo(progress);
}
});
}
//创建消息处理器对象
public
static
Handler
handler
=
new
Handler(){
//在主线程中处理从子线程发送过来的消息
@Override
public
void
handleMessage(Message
msg)
{
//获取从子线程发送过来的音乐播放的进度
Bundle
bundle
=
msg.getData();
//歌曲的总时长(毫秒)
int
duration
=
bundle.getInt("duration");
//歌曲的当前进度(毫秒)
int
currentPostition
=
bundle.getInt("currentPosition");
//刷新滑块的进度
sb.setMax(duration);
sb.setProgress(currentPostition);
//歌曲的总时长
int
minute
=
duration
/
1000
/
60;
int
second
=
duration
/
1000
%
60;
String
strMinute
=
null;
String
strSecond
=
null;
//如果歌曲的时间中的分钟小于10
if(minute
<
10)
{
//在分钟的前面加一个0
strMinute
=
"0"
+
minute;
}
else
{
strMinute
=
minute
+
"";
}
//如果歌曲的时间中的秒钟小于10
if(second
<
10)
{
//在秒钟前面加一个0
strSecond
=
"0"
+
second;
}
else
{
strSecond
=
second
+
"";
}
tv_total.setText(strMinute
+
":"
+
strSecond);
//歌曲当前播放时长
minute
=
currentPostition
/
1000
/
60;
second
=
currentPostition
/
1000
%
60;
//如果歌曲的时间中的分钟小于10
if(minute
<
10)
{
//在分钟的前面加一个0
strMinute
=
"0"
+
minute;
}
else
{
strMinute
=
minute
+
"";
}
//如果歌曲的时间中的秒钟小于10
if(second
<
10)
{
//在秒钟前面加一个0
strSecond
=
"0"
+
second;
}
else
{
strSecond
=
second
+
"";
}
tv_progress.setText(strMinute
+
":"
+
strSecond);
}
};
//播放音乐按钮响应函数
public
void
play(View
view)
{
//播放音
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年教师将取消编制 2024年教师将取消编制是真的吗3篇
- 国际虚拟运行课程出口成交市公开课特等奖市赛课微课一等奖课件
- 华南旅游区概述中国旅游地理YYY省公开课一等奖全国示范课微课金奖课件
- 2023年广东省中考化学试卷和答案解析
- 2023年物理化学真题
- 2023年度钻井队长述职报告范本
- 内科学:循环系统疾病试卷
- 内科学(医学高级):泌尿系统疾病一
- 2024年04月辽宁东北大学实验技术人员招考聘用笔试历年(难与易错考点)高频考题后附答案详解
- 2024年04月浙江金华武义县政协办公室招考聘用笔试历年(难与易错考点)高频考题后附答案详解
- 2023年公安执法-基本级执法资格考试参考题库附答案
- MRI临床应用简介
- 技术服务和质保期服务计划
- 内科大水力学实验指导01有压管流综合型实验-8孔口与管嘴出流实验
- 体育公园设计方案
- 2023年中央经济工作会精神(全文版)
- 2022-2023学年广东省佛山市南海区大沥镇八年级(下)期中数学试卷(含解析)
- CG产品工艺简介关键工序原理和常见不良改善交流
- OSN1800产品维护培训
- 中国传媒大学-主持人即兴口语训练-课件-1
- 清华大学毕业论文答辩模板P
评论
0/150
提交评论