Android应用开发--MP3音乐播放器代码实现(二),android原生开发框架

import android.content.Context;

import android.database.Cursor;

import android.provider.MediaStore;

import com.wwj.sb.domain.Mp3Info;

public class MediaUtil {

/**

  • 用于从数据库中查询歌曲的信息,保存在List当中

  • @return

*/

public static List getMp3Infos(Context context) {

Cursor cursor = context.getContentResolver().query(

MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,

MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

List mp3Infos = new ArrayList();

for (int i = 0; i < cursor.getCount(); i++) {

cursor.moveToNext();

Mp3Info mp3Info = new Mp3Info();

long id = cursor.getLong(cursor

.getColumnIndex(MediaStore.Audio.Media._ID)); //音乐id

String title = cursor.getString((cursor

.getColumnIndex(MediaStore.Audio.Media.TITLE))); //音乐标题

String artist = cursor.getString(cursor

.getColumnIndex(MediaStore.Audio.Media.ARTIST)); //艺术家

long duration = cursor.getLong(cursor

.getColumnIndex(MediaStore.Audio.Media.DURATION)); //时长

long size = cursor.getLong(cursor

.getColumnIndex(MediaStore.Audio.Media.SIZE)); //文件大小

String url = cursor.getString(cursor

.getColumnIndex(MediaStore.Audio.Media.DATA)); //文件路径

int isMusic = cursor.getInt(cursor

.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); //是否为音乐

if (isMusic != 0) { //只把音乐添加到集合当中

mp3Info.setId(id);

mp3Info.setTitle(title);

mp3Info.setArtist(artist);

mp3Info.setDuration(duration);

mp3Info.setSize(size);

mp3Info.setUrl(url);

mp3Infos.add(mp3Info);

}

}

return mp3Infos;

}

/**

  • 往List集合中添加Map对象数据,每一个Map对象存放一首音乐的所有属性

  • @param mp3Infos

  • @return

*/

public static List> getMusicMaps(

List mp3Infos) {

List> mp3list = new ArrayList>();

for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext() {

Mp3Info mp3Info = (Mp3Info) iterator.next();

HashMap map = new HashMap();

map.put(“title”, mp3Info.getTitle());

map.put(“Artist”, mp3Info.getArtist());

map.put(“duration”, formatTime(mp3Info.getDuration()));

map.put(“size”, String.valueOf(mp3Info.getSize()));

map.put(“url”, mp3Info.getUrl());

mp3list.add(map);

}

return mp3list;

}

/**

  • 格式化时间,将毫秒转换为分:秒格式

  • @param time

  • @return

*/

public static String formatTime(long time) {

String min = time / (1000 * 60) + “”;

String sec = time % (1000 * 60) + “”;

if (min.length() < 2) {

min = “0” + time / (1000 * 60) + “”;

} else {

min = time / (1000 * 60) + “”;

}

if (sec.length() == 4) {

sec = “0” + (time % (1000 * 60)) + “”;

} else if (sec.length() == 3) {

sec = “00” + (time % (1000 * 60)) + “”;

} else if (sec.length() == 2) {

sec = “000” + (time % (1000 * 60)) + “”;

} else if (sec.length() == 1) {

sec = “0000” + (time % (1000 * 60)) + “”;

}

return min + “:” + sec.trim().substring(0, 2);

}

}

好吧,来重头戏了,一大段代码来袭。

HomeActivity.Java

package com.wwj.sb.activity;

import java.util.HashMap;

import java.util.List;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.IntentFilter;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Vibrator;

import android.view.ContextMenu;

import android.view.ContextMenu.ContextMenuInfo;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnCreateContextMenuListener;

import android.view.ViewGroup.LayoutParams;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

import android.widget.Toast;

import com.wwj.sb.domain.AppConstant;

import com.wwj.sb.domain.Mp3Info;

import com.wwj.sb.service.PlayerService;

import com.wwj.sb.utils.ConstantUtil;

import com.wwj.sb.utils.CustomDialog;

import com.wwj.sb.utils.MediaUtil;

/**

  • 2013/5/7

  • 简、美音乐播放器

  • @author wwj

*/

public class HomeActivity extends Activity {

private ListView mMusiclist; // 音乐列表

private List mp3Infos = null;

private SimpleAdapter mAdapter; // 简单适配器

private Button previousBtn; // 上一首

private Button repeatBtn; // 重复(单曲循环、全部循环)

private Button playBtn; // 播放(播放、暂停)

private Button shuffleBtn; // 随机播放

private Button nextBtn; // 下一首

private TextView musicTitle;//歌曲标题

private TextView musicDuration; //歌曲时间

private Button musicPlaying; //歌曲专辑

private int repeatState; //循环标识

private final int isCurrentRepeat = 1; // 单曲循环

private final int isAllRepeat = 2; // 全部循环

private final int isNoneRepeat = 3; // 无重复播放

private boolean isFirstTime = true;

private boolean isPlaying; // 正在播放

private boolean isPause; // 暂停

private boolean isNoneShuffle = true; // 顺序播放

private boolean isShuffle = false; // 随机播放

private int listPosition = 0; //标识列表位置

private HomeReceiver homeReceiver; //自定义的广播接收器

//一系列动作

public static final String UPDATE_ACTION = “com.wwj.action.UPDATE_ACTION”;

public static final String CTL_ACTION = “com.wwj.action.CTL_ACTION”;

public static final String MUSIC_CURRENT = “com.wwj.action.MUSIC_CURRENT”;

public static final String MUSIC_DURATION = “com.wwj.action.MUSIC_DURATION”;

public static final String REPEAT_ACTION = “com.wwj.action.REPEAT_ACTION”;

public static final String SHUFFLE_ACTION = “com.wwj.action.SHUFFLE_ACTION”;

private int currentTime;

private int duration;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.home_activity_layout);

mMusiclist = (ListView) findViewById(R.id.music_list);

mMusiclist.setOnItemClickListener(new MusicListItemClickListener());

mMusiclist.setOnCreateContextMenuListener(new MusicListItemContextMenuListener());

mp3Infos = MediaUtil.getMp3Infos(getApplicationContext()); //获取歌曲对象集合

setListAdpter(MediaUtil.getMusicMaps(mp3Infos)); //显示歌曲列表

findViewById(); //找到界面上的每一个控件

setViewOnclickListener(); //为一些控件设置监听器

repeatState = isNoneRepeat; // 初始状态为无重复播放状态

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

}

/**

  • 从界面上根据id获取按钮

*/

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

musicTitle = (TextView) findViewById(R.id.music_title);

musicDuration = (TextView) findViewById(R.id.music_duration);

musicPlaying = (Button) findViewById(R.id.playing);

}

/**

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

*/

private void setViewOnclickListener() {

ViewOnClickListener viewOnClickListener = new ViewOnClickListener();

previousBtn.setOnClickListener(viewOnClickListener);

repeatBtn.setOnClickListener(viewOnClickListener);

playBtn.setOnClickListener(viewOnClickListener);

shuffleBtn.setOnClickListener(viewOnClickListener);

nextBtn.setOnClickListener(viewOnClickListener);

musicPlaying.setOnClickListener(viewOnClickListener);

}

private class ViewOnClickListener implements OnClickListener {

Intent intent = new Intent();

@Override

public void onClick(View v) {

switch (v.getId()) {

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

playBtn.setBackgroundResource(R.drawable.play_selector);

isFirstTime = false;

isPlaying = true;

isPause = false;

previous();

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;

}

switch (repeatState) {

case isCurrentRepeat: // 单曲循环

repeatBtn

.setBackgroundResource(R.drawable.repeat_current_selector);

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

Toast.LENGTH_SHORT).show();

break;

case isAllRepeat: // 全部循环

repeatBtn

.setBackgroundResource(R.drawable.repeat_all_selector);

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

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> 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代表当前正在播放的歌曲

要如何成为Android架构师?

搭建自己的知识框架,全面提升自己的技术体系,并且往底层源码方向深入钻研。
大多数技术人喜欢用思维脑图来构建自己的知识体系,一目了然。这里给大家分享一份大厂主流的Android架构师技术体系,可以用来搭建自己的知识框架,或者查漏补缺;
Android应用开发--MP3音乐播放器代码实现(二),android原生开发框架_第1张图片

对应这份技术大纲,我也整理了一套Android高级架构师完整系列的视频教程,主要针对3-5年Android开发经验以上,需要往高级架构师层次学习提升的同学,在这里点击GitHub免费分享,希望能帮你突破瓶颈,跳槽进大厂;

最后我必须强调几点:

1.搭建知识框架可不是说你整理好要学习的知识顺序,然后看一遍理解了能复制粘贴就够了,大多都是需要你自己读懂源码和原理,能自己手写出来的。
2.学习的时候你一定要多看多练几遍,把知识才吃透,还要记笔记,这些很重要! 最后你达到什么水平取决你消化了多少知识
3.最终你的知识框架应该是一个完善的,兼顾广度和深度的技术体系。然后经过多次项目实战积累经验,你才能达到高级架构师的层次。

你只需要按照在这个大的框架去填充自己,年薪40W一定不是终点,技术无止境

ext, 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代表当前正在播放的歌曲

要如何成为Android架构师?

搭建自己的知识框架,全面提升自己的技术体系,并且往底层源码方向深入钻研。
大多数技术人喜欢用思维脑图来构建自己的知识体系,一目了然。这里给大家分享一份大厂主流的Android架构师技术体系,可以用来搭建自己的知识框架,或者查漏补缺;
[外链图片转存中…(img-pNxEF9RL-1646138276098)]

对应这份技术大纲,我也整理了一套Android高级架构师完整系列的视频教程,主要针对3-5年Android开发经验以上,需要往高级架构师层次学习提升的同学,在这里点击GitHub免费分享,希望能帮你突破瓶颈,跳槽进大厂;

最后我必须强调几点:

1.搭建知识框架可不是说你整理好要学习的知识顺序,然后看一遍理解了能复制粘贴就够了,大多都是需要你自己读懂源码和原理,能自己手写出来的。
2.学习的时候你一定要多看多练几遍,把知识才吃透,还要记笔记,这些很重要! 最后你达到什么水平取决你消化了多少知识
3.最终你的知识框架应该是一个完善的,兼顾广度和深度的技术体系。然后经过多次项目实战积累经验,你才能达到高级架构师的层次。

你只需要按照在这个大的框架去填充自己,年薪40W一定不是终点,技术无止境

你可能感兴趣的:(程序员,面试,移动开发,android)