布局主要代码之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. Python3 小工具-ARP扫描

    from scapy.all import * import optparse import threading import os def scan(ipt): pkt=Ether(dst='ff: ...

  2. C语言文件进阶操作

    Description文件a.dic.b.dic.c.dic中分别存有张三的三科成绩,每个文件都是16字节:前8个字节存储其英文名字zhangsan,后面是一个空格,其后的2个字节存储其年龄(文本方式 ...

  3. Tic-Tac-Toe

    Description Kim likes to play Tic-Tac-Toe. Given a current state, and now Kim is going to take his n ...

  4. 20172330 2017-2018-1 《Java程序设计》第五周学习总结

    20172330 2017-2018-1 <Java程序设计>第五周学习总结 教材学习内容总结 第五章 首先是对各种各种运算符的了解:刚开始以为相等就是=,还有其他一些符号都挺简单的,然后 ...

  5. Js键盘事件全面控制,回车按键事件,键盘对应按键码,按键事件兼容各个浏览器。

    在网上查询的按键码如下: 一.键盘按键和键盘对应代码表:   字母按键码A <--------> 65 B <--------> 66 C <--------> 6 ...

  6. 在Centos中,大容量,且读写频繁的目录

    1./根目录 2./usr目录 3./home目录 4./var目录 5./Swap目录     比较特殊,只要物理内存没使用完,就不会被启用 以上为鸟哥的linuxPDF中的学习心得

  7. QT分析之调试跟踪系统

    原文地址:http://blog.163.com/net_worm/blog/static/127702419201002004518944/ 在我们前面的分析中,经常看到qWarning()和qDe ...

  8. GC是什么?为什么要有GC

    GC:Garbage Collection 垃圾收集器. GC就是对“不可达“的对象进行回收,释放内存. Java内存的管理实际上就是对对象的管理,其中包括对对象的分配和回收. 对于程序员来说,分配对 ...

  9. nginx 反向代理 ,入门

    入门:http://www.cnblogs.com/jjzd/p/6691500.html 启动,重新加载:http://blog.csdn.net/zhongguozhichuang/article ...

  10. 【bzoj1143】[CTSC2008]祭祀river Floyd+网络流最小割

    题目描述 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成的网络.每条河 ...