2024年Android最全Android应用开发--MP3音乐播放器代码实现(二)(2),android组件化面试

结语

  • 现在随着短视频,抖音,快手的流行NDK模块开发也显得越发重要,需要这块人才的企业也越来越多,随之学习这块的人也变多了,音视频的开发,往往是比较难的,而这个比较难的技术就是NDK里面的技术。
  • 音视频/高清大图片/人工智能/直播/抖音等等这年与用户最紧密,与我们生活最相关的技术一直都在寻找最终的技术落地平台,以前是windows系统,而现在则是移动系统了,移动系统中又是以Android占比绝大部分为前提,所以AndroidNDK技术已经是我们必备技能了。
  • 要学习好NDK,其中的关于C/C++,jni,Linux基础都是需要学习的,除此之外,音视频的编解码技术,流媒体协议,ffmpeg这些都是音视频开发必备技能,而且
  • OpenCV/OpenGl/这些又是图像处理必备知识,下面这些我都是当年自己搜集的资料和做的一些图,因为当年我就感觉视频这块会是一个大的趋势。所以提前做了一些准备。现在拿出来分享给大家。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

Toast.LENGTH_SHORT).show();

break;

case isNoneRepeat: // 无重复

repeatBtn

.setBackgroundResource(R.drawable.repeat_none_selector);

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

Toast.LENGTH_SHORT).show();

break;

}

break;

case R.id.play_music: // 播放音乐

if(isFirstTime) {

play();

isFirstTime = false;

isPlaying = true;

isPause = false;

} else {

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.shuffle_music: // 随机播放

if (isNoneShuffle) {

shuffleBtn

.setBackgroundResource(R.drawable.shuffle_selector);

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

Toast.LENGTH_SHORT).show();

isNoneShuffle = false;

isShuffle = true;

shuffleMusic();

repeatBtn.setClickable(false);

} else if (isShuffle) {

shuffleBtn

.setBackgroundResource(R.drawable.shuffle_none_selector);

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

Toast.LENGTH_SHORT).show();

isShuffle = false;

isNoneShuffle = true;

repeatBtn.setClickable(true);

}

break;

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

playBtn.setBackgroundResource(R.drawable.play_selector);

isFirstTime = false;

isPlaying = true;

isPause = false;

next();

break;

case R.id.playing: //正在播放

Mp3Info mp3Info = mp3Infos.get(listPosition);

Intent intent = new Intent(HomeActivity.this, PlayerActivity.class);

intent.putExtra(“title”, mp3Info.getTitle());

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

intent.putExtra(“artist”, mp3Info.getArtist());

intent.putExtra(“listPosition”, listPosition);

intent.putExtra(“currentTime”, currentTime);

intent.putExtra(“duration”, duration);

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

startActivity(intent);

break;

}

}

}

private class MusicListItemClickListener implements OnItemClickListener {

/**

  • 点击列表播放音乐

*/

@Override

public void onItemClick(AdapterView<?> parent, View view, int position,

long id) {

listPosition = position;

playMusic(listPosition);

}

}

public class MusicListItemContextMenuListener implements OnCreateContextMenuListener {

@Override

public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);

vibrator.vibrate(50); //长按振动

musicListItemDialog(); //长按后弹出的对话框

}

}

/**

  • 填充列表

  • @param mp3Infos

*/

public void setListAdpter(List<HashMap<String, String>> mp3list) {

mAdapter = new SimpleAdapter(this, mp3list,

R.layout.music_list_item_layout, new String[] { “title”,

“Artist”, “duration” }, new int[] { R.id.music_title,

R.id.music_Artist, R.id.music_duration });

mMusiclist.setAdapter(mAdapter);

}

/**

  • 下一首歌曲

*/

public void next() {

listPosition = listPosition + 1;

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

Mp3Info mp3Info = mp3Infos.get(listPosition);

musicTitle.setText(mp3Info.getTitle());

Intent intent = new Intent();

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

intent.putExtra(“listPosition”, listPosition);

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

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

startService(intent);

} else {

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

}

}

/**

  • 上一首歌曲

*/

public void previous() {

listPosition = listPosition - 1;

if(listPosition >= 0) {

Mp3Info mp3Info = mp3Infos.get(listPosition);

musicTitle.setText(mp3Info.getTitle());

Intent intent = new Intent();

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

intent.putExtra(“listPosition”, listPosition);

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

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

startService(intent);

}else {

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

}

}

public void play() {

playBtn.setBackgroundResource(R.drawable.play_selector);

Mp3Info mp3Info = mp3Infos.get(listPosition);

musicTitle.setText(mp3Info.getTitle());

Intent intent = new Intent();

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

intent.putExtra(“listPosition”, 0);

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

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

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 shuffleMusic() {

Intent intent = new Intent(CTL_ACTION);

intent.putExtra(“control”, 4);

sendBroadcast(intent);

}

public void musicListItemDialog() {

String[] menuItems = new String[]{“播放音乐”,“设为铃声”,“查看详情”};

ListView menuList = new ListView(HomeActivity.this);

menuList.setCacheColorHint(Color.TRANSPARENT);

menuList.setDividerHeight(1);

menuList.setAdapter(new ArrayAdapter(HomeActivity.this, R.layout.context_dialog_layout, R.id.dialogText, menuItems));

menuList.setLayoutParams(new LayoutParams(ConstantUtil.getScreen(HomeActivity.this)[0] / 2, LayoutParams.WRAP_CONTENT));

final CustomDialog customDialog = new CustomDialog.Builder(HomeActivity.this).setTitle(R.string.operation).setView(menuList).create();

customDialog.show();

menuList.setOnItemClickListener( new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

customDialog.cancel();

customDialog.dismiss();

}

});

}

public void playMusic(int listPosition) {

if (mp3Infos != null) {

Mp3Info mp3Info = mp3Infos.get(listPosition);

musicTitle.setText(mp3Info.getTitle());

Intent intent = new Intent(HomeActivity.this, PlayerActivity.class);

intent.putExtra(“title”, mp3Info.getTitle());

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

intent.putExtra(“artist”, mp3Info.getArtist());

intent.putExtra(“listPosition”, listPosition);

intent.putExtra(“currentTime”, currentTime);

intent.putExtra(“repeatState”, repeatState);

intent.putExtra(“shuffleState”, isShuffle);

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

startActivity(intent);

}

}

@Override

protected void onStop() {

// TODO Auto-generated method stub

super.onStop();

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

}

/**

  • 按返回键弹出对话框确定退出

*/

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK

&& event.getAction() == KeyEvent.ACTION_DOWN) {

new AlertDialog.Builder(this)

.setIcon(R.drawable.ic_launcher)

.setTitle(“退出”)

.setMessage(“您确定要退出?”)

.setNegativeButton(“取消”, null)

.setPositiveButton(“确定”,

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

finish();

Intent intent = new Intent(

HomeActivity.this,

PlayerService.class);

unregisterReceiver(homeReceiver);

stopService(intent); // 停止后台服务

}

}).show();

}

return super.onKeyDown(keyCode, event);

}

//自定义的BroadcastReceiver,负责监听从Service传回来的广播

public class HomeReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if(action.equals(MUSIC_CURRENT)){

//currentTime代表当前播放的时间

currentTime = intent.getIntExtra(“currentTime”, -1);

musicDuration.setText(MediaUtil.formatTime(currentTime));

} else if (action.equals(MUSIC_DURATION)) {

duration = intent.getIntExtra(“duration”, -1);

}

else if(action.equals(UPDATE_ACTION)) {

//获取Intent中的current消息,current代表当前正在播放的歌曲

listPosition = intent.getIntExtra(“current”, -1);

if(listPosition >= 0) {

musicTitle.setText(mp3Infos.get(listPosition).getTitle());

}

}else if(action.equals(REPEAT_ACTION)) {

repeatState = intent.getIntExtra(“repeatState”, -1);

switch (repeatState) {

case isCurrentRepeat: // 单曲循环

repeatBtn

.setBackgroundResource(R.drawable.repeat_current_selector);

shuffleBtn.setClickable(false);

break;

case isAllRepeat: // 全部循环

repeatBtn

.setBackgroundResource(R.drawable.repeat_all_selector);

shuffleBtn.setClickable(false);

break;

case isNoneRepeat: // 无重复

repeatBtn

.setBackgroundResource(R.drawable.repeat_none_selector);

shuffleBtn.setClickable(true);

break;

}

}

else if(action.equals(SHUFFLE_ACTION)) {

isShuffle = intent.getBooleanExtra(“shuffleState”, false);

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);

}

}

}

}

}

到这里,要开讲啦。

以下是需要注意的几点:

1. 音乐是通过Service来播放的,Activity通过启动服务来实现在后台播放音乐。

2. Activity中自定义了一个广播接收器,需要进行intent过滤器的定义,动作的添加,注册广播接收器:

homeReceiver = new HomeReceiver();

// 创建IntentFilter

IntentFilter filter = new IntentFilter();

// 指定BroadcastReceiver监听的Action

filter.addAction(UPDATE_ACTION);

filter.addAction(MUSIC_CURRENT);

filter.addAction(MUSIC_DURATION);

filter.addAction(REPEAT_ACTION);

filter.addAction(SHUFFLE_ACTION);

// 注册BroadcastReceiver

registerReceiver(homeReceiver, filter);

3. 在广播接收器类当中对动作进行处理,比如实现时间的更新和标题的更新等。

4. 这里还要注意按钮触发,播放状态的改变,比如音乐循环,有三种状态:单曲、全部循环、顺序,每切换一个状态都要向服务发送一条广播,通知它要改变状态。

5. 点击列表的时候,会跳入到播放界面的Activity中,要注意用intent来传递参数,注意每个参数的用途,比如title、url、MSG,就分别代表标题、路径、播放状态。

6. 长按列表会弹出自定义对话框,也会有短暂的震动效果,自定义对话框需要自行实现。这里我也贴一下实现代码吧。

package com.wwj.sb.utils;

import android.app.Activity;

import android.app.Dialog;

import android.content.Context;

import android.content.DialogInterface;

import android.text.TextUtils;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.Button;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

import com.wwj.sb.activity.R;

/**

  • 自定义对话框类

  • @author wwj

*/

public class CustomDialog extends Dialog {

public CustomDialog(Context context) {

super(context);

}

public CustomDialog(Context context, int theme) {

super(context, theme);

}

public static class Builder {

private Context context;

private int mIcon = -1; // 提示图标

private CharSequence mTitle; // 提示标题

private CharSequence mMessage; // 提示内容

private CharSequence mPositiveButtonText; // 确定按钮文本

private CharSequence mNegativeButtonText; // 取消按钮文本

private CharSequence mNeutralButtonText; // 中间按钮文本

private boolean mCancelable = true; // 是否启用取消键

private int mViewSpacingLeft;

private int mViewSpacingTop;

private int mViewSpacingRight;

private int mViewSpacingBottom;

private boolean mViewSpacingSpecified = false;

// 提示内容View

private View mView;

// 各种触发事件

private OnClickListener mPositiveButtonClickListener,

mNegativeButtonClickListener, mNeutralButtonClickListener;

private OnCancelListener mCancelListener; // 取消键事件

private OnKeyListener mKeyListener; // 按键处理

public Builder(Context context) {

this.context = context;

}

public Builder setMessage(CharSequence message) {

this.mMessage = message;

return this;

}

public Builder setMessage(int message) {

this.mMessage = context.getText(message);

return this;

}

public Builder setTitle(int title) {

this.mTitle = context.getText(title);

return this;

}

public Builder setTitle(CharSequence title) {

this.mTitle = title;

return this;

}

public Builder setIcon(int icon) {

this.mIcon = icon;

return this;

}

public Builder setView(View view) {

this.mView = view;

mViewSpacingSpecified = false;

return this;

}

public Builder setView(View view, int left, int top, int right,

int bottom) {

this.mView = view;

this.mViewSpacingLeft = left;

this.mViewSpacingTop = top;

this.mViewSpacingRight = right;

this.mViewSpacingBottom = bottom;

mViewSpacingSpecified = true;

return this;

}

public Builder setPositonButton(int textId,

final OnClickListener listener) {

this.mPositiveButtonText = context.getText(textId);

this.mPositiveButtonClickListener = listener;

return this;

}

public Builder setPostionButton(String text,

final OnClickListener listener) {

this.mPositiveButtonText = text;

this.mPositiveButtonClickListener = listener;

return this;

}

public Builder setNeutralButton(int textId,

final OnClickListener listener) {

this.mNeutralButtonText = context.getText(textId);

this.mNeutralButtonClickListener = listener;

return this;

}

public Builder setNeutralButton(String text, final OnClickListener listener) {

this.mNeutralButtonText = text;

this.mNeutralButtonClickListener = listener;

return this;

}

public Builder setNegativeButton(int textId,

final OnClickListener listener) {

this.mNegativeButtonText = context.getText(textId);

this.mNegativeButtonClickListener = listener;

return this;

}

public Builder setNegativeButton(String text,

final OnClickListener listener) {

this.mNegativeButtonText = text;

this.mNegativeButtonClickListener = listener;

return this;

}

public Builder setCancelable(boolean cancelable) {

this.mCancelable = cancelable;

return this;

}

public Builder setOnCancelListener(OnCancelListener listener) {

this.mCancelListener = listener;

return this;

}

public Builder setOnKeyListener(OnKeyListener listener) {

this.mKeyListener = listener;

return this;

}

public CustomDialog create() {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final CustomDialog dialog = new CustomDialog(context, R.style.CustomDialog);

dialog.setCancelable(mCancelable);

//设置取消键事件

if(mCancelListener != null) {

dialog.setOnCancelListener(mCancelListener);

}

//设置键盘监听事件

if(mKeyListener != null) {

dialog.setOnKeyListener(mKeyListener);

}

//获取对话框布局

View layout = inflater.inflate(R.layout.alert_dialog, (ViewGroup)(((Activity)context).findViewById(R.id.parentPanel)));

layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

//设置标题

((TextView) layout.findViewById(R.id.alertTitle)).setText(mTitle);

//设置图标

if(mIcon != -1) {

((ImageView) layout.findViewById(R.id.icon))

.setBackgroundResource(mIcon);

}

int count = 0;

//设置确定按钮

if(setButton(layout, mPositiveButtonText, R.id.button1, dialog, mPositiveButtonClickListener)) count++;

// 设置拒绝按钮

if(setButton(layout, mNegativeButtonText, R.id.button2, dialog, mNegativeButtonClickListener)) count++;

// 设置中间按钮

if(setButton(layout, mNeutralButtonText, R.id.button3, dialog, mNeutralButtonClickListener)) count++;

if(count == 0) {

layout.findViewById(R.id.buttonPanel).setVisibility(View.GONE);

}

//一个按钮时,显示两边空间

if(count == 1) {

layout.findViewById(R.id.leftSpacer)

.setVisibility(View.INVISIBLE);

最后

今天关于面试的分享就到这里,还是那句话,有些东西你不仅要懂,而且要能够很好地表达出来,能够让面试官认可你的理解,例如Handler机制,这个是面试必问之题。有些晦涩的点,或许它只活在面试当中,实际工作当中你压根不会用到它,但是你要知道它是什么东西。

最后在这里小编分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

还有 高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【算法合集】

【延伸Android必备知识点】

【Android部分高级架构视频学习资源】

Android精讲视频领取学习后更加是如虎添翼!进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

d(R.id.buttonPanel).setVisibility(View.GONE);

}

//一个按钮时,显示两边空间

if(count == 1) {

layout.findViewById(R.id.leftSpacer)

.setVisibility(View.INVISIBLE);

最后

今天关于面试的分享就到这里,还是那句话,有些东西你不仅要懂,而且要能够很好地表达出来,能够让面试官认可你的理解,例如Handler机制,这个是面试必问之题。有些晦涩的点,或许它只活在面试当中,实际工作当中你压根不会用到它,但是你要知道它是什么东西。

最后在这里小编分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

还有 高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

[外链图片转存中…(img-AcrAIg93-1715708436603)]

【算法合集】

[外链图片转存中…(img-usqJCubZ-1715708436604)]

【延伸Android必备知识点】

[外链图片转存中…(img-J4tAxL99-1715708436604)]

【Android部分高级架构视频学习资源】

Android精讲视频领取学习后更加是如虎添翼!进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值