package com.jk.activity; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.IBinder; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.ListView; import android.widget.SeekBar; import android.widget.SimpleAdapter; import com.jk.impl.IMusic; import com.jk.impl.onMusicOver; import com.jk.service.musicService; public class MainActivity extends Activity { ListView lv_sing; SeekBar seekbar; ImageView iv_zt, iv_up, iv_play, iv_next, iv_list; private SimpleAdapter adapter; private List<Map<String, String>> musicList = new ArrayList<Map<String, String>>(); private String musicPath; private String musicName; private String musicAlbum; private String musicArtist; private String musicAlbumKey; private String musicAlbumArtPath; private IMusic binder; private int musicid=10000; private String action = "com.jk.service.musicservice"; private boolean f = false; private int size=0; private int musicTime; private long mExitTime = 0; Intent in = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.mainactivity); musicList = loadSongs(); intitView(); if (musicList.size() > 0) { //绑定服务 in = new Intent(MainActivity.this, musicService.class); in.putExtra("path", musicList.get(0).get("path")); bindService(in, conn, BIND_AUTO_CREATE); } initEvent(); } /** * 初始化事件监听 */ private void initEvent() { lv_sing.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String, String> m = new HashMap<String, String>(); m = musicList.get(position); if(musicid==Integer.parseInt(m.get("id"))){//点击同一首歌 start(null);//改变播放状态 }else{ musicid = Integer.parseInt(m.get("id")); if (binder != null) { binder.dorevmo();//handle.removeCallbacks(runnable); binder.doRset();//mp.release(); unbindService(conn); in.putExtra("path", m.get("path")); bindService(in, conn, Context.BIND_AUTO_CREATE); new Thread(new Runnable() {//初始化seekbar @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(500); if (binder != null) { binder.init(seekbar, new onMusicOver() { @Override public void onMusicOver() { // TODO Auto-generated method stub next(null); } }); } } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); f = true; iv_play.setBackgroundResource(R.drawable.start); } } } }); } public void start(View v){ Log.i("tag", "start"+f); if(f){//正在播放 binder.dopause(); iv_play.setBackgroundResource(R.drawable.start); f = false; }else{ binder.dostart(); iv_play.setBackgroundResource(R.drawable.pause); f = true; } } public void next(View v){ Map<String, String> m = new HashMap<String, String>(); musicid = (musicid+1)%musicList.size(); m = musicList.get(musicid); if (binder != null) { binder.dorevmo(); binder.doRset();//mp.release(); unbindService(conn); in.putExtra("path", m.get("path")); bindService(in, conn, Context.BIND_AUTO_CREATE); new Thread(new Runnable() {//初始化seekbar @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(500); if (binder != null) { binder.init(seekbar, new onMusicOver() { @Override public void onMusicOver() { next(null); } }); } } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); f = true; iv_play.setBackgroundResource(R.drawable.start); } } /** * 视图初始化 */ private void intitView() { lv_sing = (ListView) findViewById(R.id.lv_sing); seekbar = (SeekBar) findViewById(R.id.seekBar1); iv_zt = (ImageView) findViewById(R.id.img_zj); iv_up = (ImageView) findViewById(R.id.img_up); iv_play = (ImageView) findViewById(R.id.img_pause); iv_next = (ImageView) findViewById(R.id.img_next); iv_list = (ImageView) findViewById(R.id.img_menu); adapter = new SimpleAdapter(getApplicationContext(), musicList, R.layout.song_item, new String[] { "musicName", "musicArtist", "musicTime", "musicPath", "musicRating" }, new int[] { R.id.tv_name, R.id.tv_songer, R.id.tv_time, R.id.tv_musicPath, R.id.tv_musicRating }); lv_sing.setAdapter(adapter); } ServiceConnection conn = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub System.out.println("onServiceDisconnected"); } public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub System.out.println("onServiceConnected"); binder = (IMusic) service; } }; /** * 获取歌曲信息 * @return */ public List<Map<String, String>> loadSongs() { // 查找sdcard卡上的所有歌曲信息 // 加入封装音乐信息的代码 // 查询所有歌曲 ContentResolver musicResolver = this.getContentResolver(); Cursor musicCursor = musicResolver.query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.Media.SIZE + ">80000", null, null); int musicColumnIndex; if (null != musicCursor && musicCursor.getCount() > 0) { for (musicCursor.moveToFirst(); !musicCursor.isAfterLast(); musicCursor .moveToNext()) { Map musicDataMap = new HashMap(); musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns._ID); int musicRating = musicCursor.getInt(musicColumnIndex); musicDataMap.put("musicRating", musicRating + ""); musicDataMap.put("id",size+""); size=size+1; // 取得音乐播放路径 musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.DATA); musicPath = musicCursor.getString(musicColumnIndex); musicDataMap.put("musicPath", musicPath); // 取得音乐的名字 musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.TITLE); musicName = musicCursor.getString(musicColumnIndex); musicDataMap.put("musicName", musicName); // 取得音乐的专辑名称 musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM); musicAlbum = musicCursor.getString(musicColumnIndex); musicDataMap.put("musicAlbum", musicAlbum); // 取得音乐的演唱者 musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST); musicArtist = musicCursor.getString(musicColumnIndex); musicDataMap.put("musicArtist", musicArtist); // 取得歌曲对应的专辑对应的Key musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_KEY); musicAlbumKey = musicCursor.getString(musicColumnIndex); // 取得歌曲的大小 musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.DURATION); musicTime = musicCursor.getInt(musicColumnIndex); // // Time musicTime = new Time(); // musicTime.set(musicTime); String readableTime = ":" ; int m=musicTime% 60000/1000; int o=musicTime / 60000; if(o==0){ readableTime="00"+readableTime; }else if(0<o&&o<10){ readableTime="0"+o+readableTime; }else{ readableTime=o+readableTime; } if(m<10){ readableTime=readableTime+"0"+m; }else{ readableTime= readableTime+m; } musicDataMap.put("musicTime", readableTime); // musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.DATA); String path = musicCursor.getString(musicColumnIndex); musicDataMap.put("path", path); // musicColumnIndex = musicCursor .getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST); String display = musicCursor.getString(musicColumnIndex); musicDataMap.put("displayName", display); String[] argArr = { musicAlbumKey }; ContentResolver albumResolver = this.getContentResolver(); Cursor albumCursor = albumResolver.query( MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.AudioColumns.ALBUM_KEY + " = ?", argArr, null); if (null != albumCursor && albumCursor.getCount() > 0) { albumCursor.moveToFirst(); int albumArtIndex = albumCursor .getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART); musicAlbumArtPath = albumCursor.getString(albumArtIndex); if (null != musicAlbumArtPath && !"".equals(musicAlbumArtPath)) { musicDataMap.put("musicAlbumImage", musicAlbumArtPath); } else { musicDataMap.put("musicAlbumImage", ""); } } else { // 没有专辑定义,给默认图片 musicDataMap.put("musicAlbumImage", ""); } musicList.add(musicDataMap); } } return musicList; } public static byte[] read(InputStream in) { // TODO Auto-generated method stub ByteArrayOutputStream outs = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; try { while ((len = in.read(buffer)) != -1) { outs.write(buffer, 0, len); } in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return outs.toByteArray(); } }
播放歌曲改变后,先将前一个播放的进度线程移除handler,
释放资源,解除绑定,重新绑定
binder.dorevmo();//handle.removeCallbacks(runnable);
binder.doRset();//mp.release();
unbindService(conn);
in.putExtra("path", m.get("path"));
bindService(in, conn, Context.BIND_AUTO_CREATE);
使用一个线程来执行seekbar的初始化,和播放完成回调方法的设置
new Thread(new Runnable() {//初始化seekbar @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(500); if (binder != null) { binder.init(seekbar, new onMusicOver() { @Override public void onMusicOver() { // TODO Auto-generated method stub next(null); } }); } } catch (InterruptedException e) { e.printStackTrace(); } } }).start();
时间: 2024-10-17 02:38:37