Android应用开发-MP3音乐播放器代码实现(三)(1)

*/

private void findViewById() {

previousBtn = (Button) findViewById(R.id.previous_music);

repeatBtn = (Button) findViewById(R.id.repeat_music);

playBtn = (Button) findViewById(R.id.play_music);

shuffleBtn = (Button) findViewById(R.id.shuffle_music);

nextBtn = (Button) findViewById(R.id.next_music);

searchBtn = (Button) findViewById(R.id.search_music);

queueBtn = (Button) findViewById(R.id.play_queue);

music_progressBar = (SeekBar) findViewById(R.id.audioTrack);

currentProgress = (TextView) findViewById(R.id.current_progress);

finalProgress = (TextView) findViewById(R.id.final_progress);

}

/**

  • 给每一个按钮设置监听器

*/

private void setViewOnclickListener() {

ViewOnclickListener ViewOnClickListener = new ViewOnclickListener();

previousBtn.setOnClickListener(ViewOnClickListener);

repeatBtn.setOnClickListener(ViewOnClickListener);

playBtn.setOnClickListener(ViewOnClickListener);

shuffleBtn.setOnClickListener(ViewOnClickListener);

nextBtn.setOnClickListener(ViewOnClickListener);

searchBtn.setOnClickListener(ViewOnClickListener);

queueBtn.setOnClickListener(ViewOnClickListener);

music_progressBar.setOnSeekBarChangeListener(new SeekBarChangeListener());

}

/**

  • 在OnResume中初始化和接收Activity数据

*/

@Override

protected void onResume() {

super.onResume();

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

title = bundle.getString(“title”);

artist = bundle.getString(“artist”);

url = bundle.getString(“url”);

listPosition = bundle.getInt(“listPosition”);

repeatState = bundle.getInt(“repeatState”);

isShuffle = bundle.getBoolean(“shuffleState”);

flag = bundle.getInt(“MSG”);

currentTime = bundle.getInt(“currentTime”);

duration = bundle.getInt(“duration”);

initView();

}

/**

  • 初始化界面

*/

public void initView() {

musicTitle.setText(title);

musicArtist.setText(artist);

music_progressBar.setProgress(currentTime);

music_progressBar.setMax(duration);

switch (repeatState) {

case isCurrentRepeat: // 单曲循环

shuffleBtn.setClickable(false);

repeatBtn.setBackgroundResource(R.drawable.repeat_current_selector);

break;

case isAllRepeat: // 全部循环

shuffleBtn.setClickable(false);

repeatBtn.setBackgroundResource(R.drawable.repeat_all_selector);

break;

case isNoneRepeat: // 无重复

shuffleBtn.setClickable(true);

repeatBtn.setBackgroundResource(R.drawable.repeat_none_selector);

break;

}

if(isShuffle) {

isNoneShuffle = false;

shuffleBtn.setBackgroundResource(R.drawable.shuffle_selector);

repeatBtn.setClickable(false);

} else {

isNoneShuffle = true;

shuffleBtn.setBackgroundResource(R.drawable.shuffle_none_selector);

repeatBtn.setClickable(true);

}

if(flag == AppConstant.PlayerMsg.PLAYING_MSG) { //如果播放信息是正在播放

Toast.makeText(PlayerActivity.this, “正在播放–” + title, 1).show();

}

else if(flag == AppConstant.PlayerMsg.PLAY_MSG) { //如果是点击列表播放歌曲的话

play();

}

playBtn.setBackgroundResource(R.drawable.play_selector);

isPlaying = true;

isPause = false;

}

/**

  • 反注册广播

*/

@Override

protected void onStop() {

super.onStop();

unregisterReceiver(playerReceiver);

}

@Override

protected void onDestroy() {

super.onDestroy();

}

/**

  • 控件点击事件

  • @author wwj

*/

private class ViewOnclickListener implements OnClickListener {

Intent intent = new Intent();

@Override

public void onClick(View v) {

switch(v.getId()) {

case R.id.play_music:

if (isPlaying) {

playBtn.setBackgroundResource(R.drawable.pause_selector);

intent.setAction(“com.wwj.media.MUSIC_SERVICE”);

intent.putExtra(“MSG”, AppConstant.PlayerMsg.PAUSE_MSG);

startService(intent);

isPlaying = false;

isPause = true;

} else if (isPause) {

playBtn.setBackgroundResource(R.drawable.play_selector);

intent.setAction(“com.wwj.media.MUSIC_SERVICE”);

intent.putExtra(“MSG”, AppConstant.PlayerMsg.CONTINUE_MSG);

startService(intent);

isPause = false;

isPlaying = true;

}

break;

case R.id.previous_music: //上一首歌曲

previous_music();

break;

case R.id.next_music: //下一首歌曲

next_music();

break;

case R.id.repeat_music: //重复播放音乐

if (repeatState == isNoneRepeat) {

repeat_one();

shuffleBtn.setClickable(false); //是随机播放变为不可点击状态

repeatState = isCurrentRepeat;

} else if (repeatState == isCurrentRepeat) {

repeat_all();

shuffleBtn.setClickable(false);

repeatState = isAllRepeat;

} else if (repeatState == isAllRepeat) {

repeat_none();

shuffleBtn.setClickable(true);

repeatState = isNoneRepeat;

}

Intent intent = new Intent(REPEAT_ACTION);

switch (repeatState) {

case isCurrentRepeat: // 单曲循环

repeatBtn

.setBackgroundResource(R.drawable.repeat_current_selector);

Toast.makeText(PlayerActivity.this, R.string.repeat_current,

Toast.LENGTH_SHORT).show();

intent.putExtra(“repeatState”, isCurrentRepeat);

sendBroadcast(intent);

break;

case isAllRepeat: // 全部循环

repeatBtn

.setBackgroundResource(R.drawable.repeat_all_selector);

Toast.makeText(PlayerActivity.this, R.string.repeat_all,

Toast.LENGTH_SHORT).show();

intent.putExtra(“repeatState”, isAllRepeat);

sendBroadcast(intent);

break;

case isNoneRepeat: // 无重复

repeatBtn

.setBackgroundResource(R.drawable.repeat_none_selector);

Toast.makeText(PlayerActivity.this, R.string.repeat_none,

Toast.LENGTH_SHORT).show();

intent.putExtra(“repeatState”, isNoneRepeat);

break;

}

break;

case R.id.shuffle_music: //随机播放状态

Intent shuffleIntent = new Intent(SHUFFLE_ACTION);

if (isNoneShuffle) { //如果当前状态为非随机播放,点击按钮之后改变状态为随机播放

shuffleBtn

.setBackgroundResource(R.drawable.shuffle_selector);

Toast.makeText(PlayerActivity.this, R.string.shuffle,

Toast.LENGTH_SHORT).show();

isNoneShuffle = false;

isShuffle = true;

shuffleMusic();

repeatBtn.setClickable(false);

shuffleIntent.putExtra(“shuffleState”, true);

sendBroadcast(shuffleIntent);

} else if (isShuffle) {

shuffleBtn

.setBackgroundResource(R.drawable.shuffle_none_selector);

Toast.makeText(PlayerActivity.this, R.string.shuffle_none,

Toast.LENGTH_SHORT).show();

isShuffle = false;

isNoneShuffle = true;

repeatBtn.setClickable(true);

shuffleIntent.putExtra(“shuffleState”, false);

sendBroadcast(shuffleIntent);

}

break;

}

}

}

/**

  • 实现监听Seekbar的类

  • @author wwj

*/

private class SeekBarChangeListener implements OnSeekBarChangeListener {

@Override

public void onProgressChanged(SeekBar seekBar, int progress,

boolean fromUser) {

if(fromUser) {

audioTrackChange(progress); //用户控制进度的改变

}

}

@Override

public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override

public void onStopTrackingTouch(SeekBar seekBar) {

}

}

/**

  • 播放音乐

*/

public void play() {

//开始播放的时候为顺序播放

repeat_none();

Intent intent = new Intent();

intent.setAction(“com.wwj.media.MUSIC_SERVICE”);

intent.putExtra(“url”, url);

intent.putExtra(“listPosition”, listPosition);

intent.putExtra(“MSG”, flag);

startService(intent);

}

/**

  • 随机播放

*/

public void shuffleMusic() {

Intent intent = new Intent(CTL_ACTION);

intent.putExtra(“control”, 4);

sendBroadcast(intent);

}

public void audioTrackChange(int progress) {

Intent intent = new Intent();

intent.setAction(“com.wwj.media.MUSIC_SERVICE”);

intent.putExtra(“url”, url);

intent.putExtra(“listPosition”, listPosition);

if(isPause) {

intent.putExtra(“MSG”, AppConstant.PlayerMsg.PAUSE_MSG);

}

else {

intent.putExtra(“MSG”, AppConstant.PlayerMsg.PROGRESS_CHANGE);

}

intent.putExtra(“progress”, progress);

startService(intent);

}

/**

  • 单曲循环

*/

public void repeat_one() {

Intent intent = new Intent(CTL_ACTION);

intent.putExtra(“control”, 1);

sendBroadcast(intent);

}

/**

  • 全部循环

*/

public void repeat_all() {

Intent intent = new Intent(CTL_ACTION);

intent.putExtra(“control”, 2);

sendBroadcast(intent);

}

/**

  • 顺序播放

*/

public void repeat_none() {

Intent intent = new Intent(CTL_ACTION);

intent.putExtra(“control”, 3);

sendBroadcast(intent);

}

/**

  • 上一首

*/

public void previous_music() {

playBtn.setBackgroundResource(R.drawable.play_selector);

listPosition = listPosition - 1;

if(listPosition >= 0) {

Mp3Info mp3Info = mp3Infos.get(listPosition); //上一首MP3

musicTitle.setText(mp3Info.getTitle());

musicArtist.setText(mp3Info.getArtist());

url = mp3Info.getUrl();

Intent intent = new Intent();

intent.setAction(“com.wwj.media.MUSIC_SERVICE”);

intent.putExtra(“url”, mp3Info.getUrl());

intent.putExtra(“listPosition”, listPosition);

intent.putExtra(“MSG”, AppConstant.PlayerMsg.PRIVIOUS_MSG);

startService(intent);

}

else {

Toast.makeText(PlayerActivity.this, “没有上一首了”, Toast.LENGTH_SHORT).show();

}

}

/**

  • 下一首

*/

public void next_music() {

playBtn.setBackgroundResource(R.drawable.play_selector);

listPosition = listPosition + 1;

if(listPosition <= mp3Infos.size() - 1) {

Mp3Info mp3Info = mp3Infos.get(listPosition);

url = mp3Info.getUrl();

musicTitle.setText(mp3Info.getTitle());

musicArtist.setText(mp3Info.getArtist());

Intent intent = new Intent();

intent.setAction(“com.wwj.media.MUSIC_SERVICE”);

intent.putExtra(“url”, mp3Info.getUrl());

intent.putExtra(“listPosition”, listPosition);

intent.putExtra(“MSG”, AppConstant.PlayerMsg.NEXT_MSG);

startService(intent);

} else {

Toast.makeText(PlayerActivity.this, “没有下一首了”, Toast.LENGTH_SHORT).show();

}

}

/**

  • 用来接收从service传回来的广播的内部类

  • @author wwj

*/

public class PlayerReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

尾声

最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。

进阶学习视频

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

" />

尾声

最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。

进阶学习视频

[外链图片转存中…(img-vHwu764R-1713247286530)]

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-5LTXmy6P-1713247286530)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 21
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值