布局主要代码之ListView:

<span style="font-size:14px;"> <ListView
android:id="@+id/music_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/handle_btnlayout"
android:cacheColorHint="#00000000" /*设置为透明,防止滑动时背景变成白色*/
android:dividerHeight="1dip"
android:divider="#88888888" /*设置分割线的颜色和透明度*/
android:listSelector="#00000000"
android:layout_marginBottom="50dp" >
</ListView></span>

Android之UI界面代码:

<span style="font-size:14px;">public class Music extends BaseActivity{       //BaseActivity继承Activity作为所有活动的父类
public static final String ACTION_UPDATEUI = "action.updateUI";
private ListView ls = null;
private Button play_music = null;
private Button previous_music =null;
private Button next_music =null;
private Button repeat_music = null;
private Button shuffle_music =null;
private TextView music_title = null;
private TextView music_duration =null;
public static List<Map<String, String>> list ;
private Boolean start = false;
private Boolean isrepeat = MusicService.repeat;
UpdateUIBroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.home_activity_layout);
play_music = (Button) findViewById(R.id.play_music);
previous_music = (Button) findViewById(R.id.previous_music);
next_music = (Button) findViewById(R.id.next_music);
repeat_music = (Button) findViewById(R.id.repeat_music);
shuffle_music = (Button) findViewById(R.id.shuffle_music);
music_title = (TextView) findViewById(R.id.music_title);
music_duration = (TextView) findViewById(R.id.music_duration);
music_title.setText(MusicService.music_title);
music_duration.setText(MusicService.music_time); play_music.setOnClickListener(new ButtonsetOnClickListener());
previous_music.setOnClickListener(new ButtonsetOnClickListener());
next_music.setOnClickListener(new ButtonsetOnClickListener());
repeat_music.setOnClickListener(new ButtonsetOnClickListener());
shuffle_music.setOnClickListener(new ButtonsetOnClickListener());
if(MusicService.isplay){
play_music.setBackgroundResource(R.drawable.pause_selector);
}else{
play_music.setBackgroundResource(R.drawable.play_selector);
}
if(isrepeat){
repeat_music.setBackgroundResource(R.drawable.repeat_all_selector);
}else{
shuffle_music.setBackgroundResource(R.drawable.shuffle_selector);
}
//动态注册广播接收器
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_UPDATEUI);
broadcastReceiver = new UpdateUIBroadcastReceiver();
registerReceiver(broadcastReceiver, filter); ls = (ListView) findViewById(R.id.music_list);
//接下来要将这些数据封装成ArrayAdapter对象
SimpleAdapter adapter = this.getAdapter();
ls.setAdapter(adapter);
ls.setOnItemClickListener(new ItemClick()); //点击
ls.setOnItemLongClickListener(null); //长按
ls.setOnItemSelectedListener(null); //勾选
}
private class ButtonsetOnClickListener implements OnClickListener{
Intent intent =new Intent(Music.this, MusicService.class); //记得注册Service
Bundle bundle = new Bundle();
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.play_music:
if(start){
play_music.setBackgroundResource(R.drawable.play_selector);
bundle.putBoolean("start", false);
start = false;
}else{
play_music.setBackgroundResource(R.drawable.pause_selector);
bundle.putBoolean("start", true);
start = true;
}
break;
case R.id.previous_music:
bundle.putBoolean("previous", true);
break;
case R.id.next_music:
bundle.putBoolean("next", true);
break;
case R.id.repeat_music:
bundle.putBoolean("repeat", true);
repeat_music.setBackgroundResource(R.drawable.repeat_all_selector);
shuffle_music.setBackgroundResource(R.drawable.shuffle_none_selector);
break;
case R.id.shuffle_music:
bundle.putBoolean("shuffle", true);
shuffle_music.setBackgroundResource(R.drawable.shuffle_selector);
repeat_music.setBackgroundResource(R.drawable.repeat_none_selector);
break;
default:
break;
}
intent.putExtras(bundle); //Intent传递参数
Music.this.startService(intent); //启动服务
}
}
//定义广播接收器(内部类)
private class UpdateUIBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
music_title.setText(String.valueOf(intent.getExtras().getString("music_title")));
music_duration.setText(String.valueOf(intent.getExtras().getString("music_time")));
}
}
@SuppressLint("SimpleDateFormat")
private SimpleAdapter getAdapter(){
// 使用SimpleAdapter来封装数据时必需要把每个显示的数据封装成Map
list=new ArrayList<Map<String, String>>();
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
//取得所有在外部存储卡上的音乐文件的信息 ps : MediaStore.Audio.Media.INTERNAL_CONTENT_URI 获得内置存储
Cursor cursor = Music.this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
cursor.moveToFirst();
for(int i=0;i<cursor.getCount();i++){
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); //歌曲的名称
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); //歌曲文件的路径
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); //歌曲的总播放时长
String hmsString = format.format(duration);
Map<String, String>map =new HashMap<String, String>();
map.put("title", title);
map.put("time", hmsString);
map.put("url", url);
list.add(map);
cursor.moveToNext();
}
SimpleAdapter adapter =new SimpleAdapter(this, //上下文,显示对象
list, // 要显示的数据,一定要上面的格式进行封装
R.layout.music_list, // 列表中要显示的单项信息的布局
new String[]{"title","time"}, // map中的key
new int[]{R.id.music_title_my,R.id.music_time_my}); // 显示到组件 return adapter;
}
//点击事件
private class ItemClick implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent =new Intent(Music.this, MusicService.class); //记得注册Activity
Bundle bundle = new Bundle();
bundle.putBoolean("playing", true);
bundle.putInt("position", position);
intent.putExtras(bundle); //Intent传递参数
Music.this.startService(intent); //启动服务
play_music.setBackgroundResource(R.drawable.pause_selector);
}
}
}
</span>
<span style="font-size:14px;">public class MusicService extends Service{
public static String music_title ="";
public static String music_time ="";
private MediaPlayer mp ;
private List<Map<String, String>> list = Music.list;
private int position ;
private boolean playing = false;
private boolean start = true;
private boolean previous = false;
private boolean next =false;
public static boolean repeat = true;
private boolean shuffle = false;
public static boolean isplay = false;
Random random = new Random();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
super.onCreate();
/*
final Intent intent = new Intent();
intent.setAction(Music.ACTION_UPDATEUI);
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
intent.putExtra("count", ++count);
sendBroadcast(intent);
}
};
timer.schedule(task, 1000,1000);
*/
}
@SuppressLint("NewApi")
@Override
public int onStartCommand(Intent intent, int flags, int startId){
start = intent.getBooleanExtra("start", true); //取不到值,默认false
playing = intent.getBooleanExtra("playing", false);
previous = intent.getBooleanExtra("previous", false);
next = intent.getBooleanExtra("next", false);
if(repeat){
shuffle = intent.getBooleanExtra("shuffle", false);
if(shuffle){
repeat = false;
}
}else if(shuffle){
repeat = intent.getBooleanExtra("repeat", false);
if(repeat){
shuffle = false;
}
}
if(playing){
position = intent.getIntExtra("position",1);
Uri uri = Uri.parse(list.get(position).get("url"));
playsong(uri);
}else if(previous){
previoussong();
}else if(next){
nextsong();
}
if(mp!=null){
if(start){
mp.start();
isplay = true;
}else{
mp.pause(); //暂停播放
isplay = false;
}
}
return super.onStartCommand(intent, flags, startId);
}
private void playsong(Uri uri){
if(mp!=null&&mp.isPlaying()){
mp.stop(); //停止播放
}
mp = MediaPlayer.create(this, uri);
mp.start();
mp.setOnCompletionListener(new CompletionListener());
music_title = list.get(position).get("title");
music_time = list.get(position).get("time");
//发送广播,改变界面
Intent intent = new Intent();
intent.setAction(Music.ACTION_UPDATEUI);
Bundle bundle = new Bundle();
bundle.putString("music_title",music_title) ;
bundle.putString("music_time", music_time);
intent.putExtras(bundle);
sendBroadcast(intent);
}
//获取随机数
private int randomInt(){
int num = random.nextInt(list.size());
if(num>=1&&num<=list.size()-1){
return num;
}else{
return randomInt();
}
}
private void previoussong(){
if(repeat){
position--;
if(position<=1){
position = list.size()-1;
}
}else{
position = randomInt();
}
String urlstr = list.get(position).get("url");
Uri uri = Uri.parse(urlstr);
playsong(uri);
}
private void nextsong(){
if(repeat){
position++;
if(position>=list.size()){
position = 1;
}
}else{
position = randomInt();
}
String urlstr = list.get(position).get("url");
Uri uri = Uri.parse(urlstr);
playsong(uri);
}
private class CompletionListener implements OnCompletionListener{
@Override
public void onCompletion(MediaPlayer mp) {
nextsong();
}
}
@Override
public void onDestroy(){
super.onDestroy();
if(mp!=null) mp.release(); //释放资源
stopSelf();
// timer.cancel();
}
}</span>

Android之简易音乐播发器的更多相关文章

  1. Android 实现简单音乐播放器(二)

    在Android 实现简单音乐播放器(一)中,我介绍了MusicPlayer的页面设计. 现在,我简单总结一些功能实现过程中的要点和有趣的细节,结合MainActivity.java代码进行说明(写出 ...

  2. Android 实现简单音乐播放器(一)

    今天掐指一算,学习Android长达近两个月了,今天开始,对过去一段时间的学习收获以及遇到的疑难杂症做一些总结. 简单音乐播放器是我自己完成的第一个功能较为完整的APP,可以说是我的Android学习 ...

  3. Android实现简单音乐播放器(MediaPlayer)

    Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能 ...

  4. c++开发ocx入门实践三--基于opencv的简易视频播发器ocx

    原文:http://blog.csdn.net/yhhyhhyhhyhh/article/details/51404649  利用opencv做了个简易的视频播放器的ocx,可以在c++/c#/web ...

  5. Android实现简单音乐播放器(startService和bindService后台运行程序)

    Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能有 ...

  6. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 简易音乐播放器主界面设计 - .NET CORE(C#) WPF开发 阅读导航 本文背景 代码 ...

  7. Android实现网络音乐播放器

    本文是一个简单的音乐播放器 布局代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayo ...

  8. Android开发之音乐播放器的实现

    Android音乐播放器 使用到Android的Actiivity和Service组件 播放音频的代码应该运行在服务中,定义一个播放服务MusicService,服务里定义play.stop.paus ...

  9. Android大作业 --音乐播放器

    1.项目成员(本次作业主要对上一次的音乐播放器进行完善) 韦家城 学号:1600802026 班级:161  博客:https://www.cnblogs.com/ln9969cc/ 邓乾尧 学号:1 ...

随机推荐

  1. Hadoop第二课:Hadoop集群环境配置

    一.Yum配置 1.检查Yum是否安装 rpm -qa|grep yum 2.修改yum源,我使用的是163的镜像源(http://mirrors.163.com/),根据自己的系统选择源, #进入目 ...

  2. Python3 条件与循环

    1.条件控制 下面是一个简单的条件控制语句 s=input('Please input a str: ') if s=='python': print('I love python!') elif s ...

  3. mysql 启动报错

    之前用我这个机器做mysql的测试来,今天启动准备搭建一套线上的主从,结果起不来了... 错误日志: ;InnoDB: End of page dump 170807 11:37:02 InnoDB: ...

  4. C语言中的字符串分割函数

    char *strtok(char *s, const char *delim); 分解字符串为一组字符串.s为要分解的字符串,delim为分隔符字符串. 从s开头开始的一个个被分割的串.当没有被分割 ...

  5. 如何遍历一个文件夹(C语言实现)

    #include<io.h> #include<stdio.h> int main() { long Handle; struct _finddata_t FileInfo; ...

  6. “小葵日记”API接口文档

    "小葵日记"项目API接口文档 时间:2017/10/31 (1)用户登录[待完成] POST:127.0.0.1/index/user/login data 数据别称 数据名 数 ...

  7. CWnd类虚函数的调用时机、缺省实现

    MFC(VC6.0)的CWnd及其子类中,有如下三个函数: class CWnd : public CCmdTarget{    public:        virtual BOOL PreCrea ...

  8. python学习第二天-基本数据类型常用方法

    1.直入主题 python中基本的数据类型有 数字(整形,长整形,浮点型,复数) 字符串 字节串:在介绍字符编码时介绍字节bytes类型 列表 元组 字典 集合 下面我们直接将以下面几个点进行学习 # ...

  9. window.open居中显示

    window.open中会发现怎么打开的浏览器窗口怎么不在正中间,而且还比较随性,那不是逼死强迫症患者了,所以查了一下资料始终打开的窗口能显示在正中间,下面是js中的代码分享 // url 转向网页的 ...

  10. CCleaner专业版注册码

    下载软件安装之后: 1.断网 2.用户名:任意,注册码:C2YW-XZT7-A4SE-UD89-YZPC