标签:
1 import java.text.SimpleDateFormat; 2 import java.util.Date; 3 import android.app.Activity; 4 import android.app.LoaderManager.LoaderCallbacks; 5 import android.content.BroadcastReceiver; 6 import android.content.Context; 7 import android.content.CursorLoader; 8 import android.content.Intent; 9 import android.content.IntentFilter; 10 import android.content.Loader; 11 import android.database.Cursor; 12 import android.net.Uri; 13 import android.os.Bundle; 14 import android.provider.MediaStore; 15 import android.support.v4.content.LocalBroadcastManager; 16 import android.util.Log; 17 import android.view.View; 18 import android.widget.AdapterView; 19 import android.widget.AdapterView.OnItemClickListener; 20 import android.widget.Button; 21 import android.widget.ListView; 22 import android.widget.SeekBar; 23 import android.widget.SimpleCursorAdapter; 24 import android.widget.TextView; 25 26 public class MainActivity extends Activity { 27 28 private Button stopBtn, playBtn; 29 private boolean isPlaying=false; 30 31 private Intent playerIntent; //播放服务组件的意图对象 32 33 private SeekBar seekBar; //可拖动的进度控件,用于显示播放的进度 34 35 private PrgReceiver prgReceiver; 36 37 private LocalBroadcastManager lbMgr; 38 39 private TextView msgTv; //格式化显示当前播放的时间与总时间 40 41 private Uri mp3Uri=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //获取扩展卡下的所有的音频文件接口 42 private String[] columns={MediaStore.Audio.Media._ID, 43 MediaStore.Audio.Media.DISPLAY_NAME, //文件名 44 MediaStore.Audio.Media.DATA,//文件路径 45 MediaStore.Audio.Media.DURATION}; //总时长 46 private ListView listView; 47 private SimpleCursorAdapter adapter; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.activity_main); 53 54 playBtn = (Button) findViewById(R.id.btn1Id); 55 stopBtn = (Button) findViewById(R.id.btn2Id); 56 57 seekBar=(SeekBar) findViewById(R.id.seekBarId); 58 msgTv=(TextView) findViewById(R.id.msgId); 59 60 playerIntent=new Intent(getApplicationContext(),PlayerService.class); 61 62 lbMgr=LocalBroadcastManager.getInstance(getApplicationContext()); 63 64 prgReceiver=new PrgReceiver(); 65 lbMgr.registerReceiver(prgReceiver, new IntentFilter(Config.ACTION_PROGRESS)); 66 67 event(); 68 69 initMp3List(); //初始显示mp3列表相关的控件 70 } 71 72 73 private SimpleDateFormat sdf=new SimpleDateFormat("mm:ss"); 74 private void initMp3List() { 75 // TODO Auto-generated method stub 76 listView=(ListView) findViewById(R.id.listViewId); 77 adapter=new SimpleCursorAdapter(getApplicationContext() 78 ,R.layout.item_audio,null, 79 new String[]{columns[1],columns[3],columns[2]}, 80 new int[]{R.id.nameId,R.id.timeId,R.id.pathId}, 81 SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER){ 82 83 @Override 84 public void setViewText(TextView v, String text) {//设置文本控件内容的方法 85 super.setViewText(v, text); 86 87 Log.i("info", "text:"+text); 88 if(v.getId()==R.id.timeId){ 89 long time=Long.parseLong(text); 90 v.setText(sdf.format(new Date(time))); 91 } 92 } 93 }; 94 95 96 97 listView.setAdapter(adapter); 98 99 //初始化启动Loader,加载扩展卡下所有音频文件 100 getLoaderManager().initLoader(1, null, new LoaderCallbacks<Cursor>() { 101 102 @Override 103 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 104 105 return new CursorLoader(getApplicationContext(),mp3Uri,columns,null,null,null); 106 } 107 108 @Override 109 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 110 adapter.swapCursor(data);//适配器切换数据源 111 } 112 113 @Override 114 public void onLoaderReset(Loader<Cursor> loader) { 115 // TODO Auto-generated method stub 116 117 } 118 }); 119 120 //设置ListView中item的点击事件 121 listView.setOnItemClickListener(new OnItemClickListener() { 122 @Override 123 public void onItemClick(AdapterView<?> parent, View view, 124 int position, long id) { 125 // TODO 点击某一文件的item事件,获取文件名和文件的路径 126 Cursor cursor=adapter.getCursor(); 127 cursor.moveToPosition(position); 128 129 String name=cursor.getString(1);//获取第2列的信息,即文件名 130 String path=cursor.getString(2);//获取第3列的信息,即文件路径 131 132 Log.i("info", name+","+path); 133 134 playerIntent.putExtra(Config.EXTRA_CHANGE, true); 135 playerIntent.putExtra(Config.EXTRA_PATH, path); 136 137 startService(playerIntent); 138 139 playBtn.setText("暂停"); 140 141 } 142 }); 143 144 } 145 146 private void event() { 147 // TODO Auto-generated method stub 148 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 149 150 @Override 151 public void onStopTrackingTouch(SeekBar seekBar) { 152 // TODO 拖动结束时的方法 153 //获取当前拖动的位置 154 int seekPosition=seekBar.getProgress(); 155 156 //向播放组件组件发送广播 157 Intent intent=new Intent(Config.ACTION_SEEKTO); 158 intent.putExtra(Config.EXTRA_PROGREES_CUR, seekPosition); 159 160 lbMgr.sendBroadcast(intent);//发送广播(在Service组件内接收) 161 } 162 163 @Override 164 public void onStartTrackingTouch(SeekBar seekBar) { 165 // TODO Auto-generated method stub 166 167 } 168 169 @Override 170 public void onProgressChanged(SeekBar seekBar, int progress, 171 boolean fromUser) { 172 // TODO Auto-generated method stub 173 174 } 175 }); 176 } 177 178 public void play(View v) { //播放或暂停 179 180 playerIntent.putExtra(Config.EXTRA_CHANGE, false); 181 startService(playerIntent); 182 183 isPlaying=!isPlaying; 184 185 if(isPlaying){ 186 playBtn.setText("暂停"); 187 }else{ 188 playBtn.setText("播放"); 189 } 190 } 191 192 public void stop(View v) { //停止 193 stopService(playerIntent); 194 seekBar.setProgress(0); 195 msgTv.setText("00:00/00:00");//=------------------------ 196 197 playBtn.setText("播放"); 198 199 } 200 201 @Override 202 protected void onDestroy() { 203 super.onDestroy(); 204 205 lbMgr.unregisterReceiver(prgReceiver);//取消注册本地广播接收器 206 } 207 208 private String formatTime(int s){ 209 StringBuilder builder=new StringBuilder(); 210 211 int m=s/60; //获取分钟 212 s=s%60; //获取秒数 213 214 builder.append(m/10).append(m%10) 215 .append(":") 216 .append(s/10).append(s%10); 217 218 return builder.toString(); 219 } 220 221 class PrgReceiver extends BroadcastReceiver{ 222 @Override 223 public void onReceive(Context context, Intent intent) { 224 // TODO 接收播放服务组件中发送的进度广播 225 int max=intent.getIntExtra(Config.EXTRA_PROGREES_MAX,0); 226 int cur=intent.getIntExtra(Config.EXTRA_PROGREES_CUR,0); 227 228 seekBar.setMax(max); 229 seekBar.setProgress(cur); 230 231 msgTv.setText(formatTime(cur/1000)+"/"+formatTime(max/1000)); 232 } 233 } 234 235 }
1 import java.text.SimpleDateFormat; 2 import java.util.Date; 3 import android.app.Activity; 4 import android.app.LoaderManager.LoaderCallbacks; 5 import android.content.BroadcastReceiver; 6 import android.content.Context; 7 import android.content.CursorLoader; 8 import android.content.Intent; 9 import android.content.IntentFilter; 10 import android.content.Loader; 11 import android.database.Cursor; 12 import android.net.Uri; 13 import android.os.Bundle; 14 import android.provider.MediaStore; 15 import android.support.v4.content.LocalBroadcastManager; 16 import android.util.Log; 17 import android.view.View; 18 import android.widget.AdapterView; 19 import android.widget.AdapterView.OnItemClickListener; 20 import android.widget.Button; 21 import android.widget.ListView; 22 import android.widget.SeekBar; 23 import android.widget.SimpleCursorAdapter; 24 import android.widget.TextView; 25 26 public class MainActivity extends Activity { 27 28 private Button stopBtn, playBtn; 29 private boolean isPlaying=false; 30 31 private Intent playerIntent; //播放服务组件的意图对象 32 33 private SeekBar seekBar; //可拖动的进度控件,用于显示播放的进度 34 35 private PrgReceiver prgReceiver; 36 37 private LocalBroadcastManager lbMgr; 38 39 private TextView msgTv; //格式化显示当前播放的时间与总时间 40 41 private Uri mp3Uri=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //获取扩展卡下的所有的音频文件接口 42 private String[] columns={MediaStore.Audio.Media._ID, 43 MediaStore.Audio.Media.DISPLAY_NAME, //文件名 44 MediaStore.Audio.Media.DATA,//文件路径 45 MediaStore.Audio.Media.DURATION}; //总时长 46 private ListView listView; 47 private SimpleCursorAdapter adapter; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.activity_main); 53 54 playBtn = (Button) findViewById(R.id.btn1Id); 55 stopBtn = (Button) findViewById(R.id.btn2Id); 56 57 seekBar=(SeekBar) findViewById(R.id.seekBarId); 58 msgTv=(TextView) findViewById(R.id.msgId); 59 60 playerIntent=new Intent(getApplicationContext(),PlayerService.class); 61 62 lbMgr=LocalBroadcastManager.getInstance(getApplicationContext()); 63 64 prgReceiver=new PrgReceiver(); 65 lbMgr.registerReceiver(prgReceiver, new IntentFilter(Config.ACTION_PROGRESS)); 66 67 event(); 68 69 initMp3List(); //初始显示mp3列表相关的控件 70 } 71 72 73 private SimpleDateFormat sdf=new SimpleDateFormat("mm:ss"); 74 private void initMp3List() { 75 // TODO Auto-generated method stub 76 listView=(ListView) findViewById(R.id.listViewId); 77 adapter=new SimpleCursorAdapter(getApplicationContext() 78 ,R.layout.item_audio,null, 79 new String[]{columns[1],columns[3],columns[2]}, 80 new int[]{R.id.nameId,R.id.timeId,R.id.pathId}, 81 SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER){ 82 83 @Override 84 public void setViewText(TextView v, String text) {//设置文本控件内容的方法 85 super.setViewText(v, text); 86 87 Log.i("info", "text:"+text); 88 if(v.getId()==R.id.timeId){ 89 long time=Long.parseLong(text); 90 v.setText(sdf.format(new Date(time))); 91 } 92 } 93 }; 94 95 96 97 listView.setAdapter(adapter); 98 99 //初始化启动Loader,加载扩展卡下所有音频文件 100 getLoaderManager().initLoader(1, null, new LoaderCallbacks<Cursor>() { 101 102 @Override 103 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 104 105 return new CursorLoader(getApplicationContext(),mp3Uri,columns,null,null,null); 106 } 107 108 @Override 109 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 110 adapter.swapCursor(data);//适配器切换数据源 111 } 112 113 @Override 114 public void onLoaderReset(Loader<Cursor> loader) { 115 // TODO Auto-generated method stub 116 117 } 118 }); 119 120 //设置ListView中item的点击事件 121 listView.setOnItemClickListener(new OnItemClickListener() { 122 @Override 123 public void onItemClick(AdapterView<?> parent, View view, 124 int position, long id) { 125 // TODO 点击某一文件的item事件,获取文件名和文件的路径 126 Cursor cursor=adapter.getCursor(); 127 cursor.moveToPosition(position); 128 129 String name=cursor.getString(1);//获取第2列的信息,即文件名 130 String path=cursor.getString(2);//获取第3列的信息,即文件路径 131 132 Log.i("info", name+","+path); 133 134 playerIntent.putExtra(Config.EXTRA_CHANGE, true); 135 playerIntent.putExtra(Config.EXTRA_PATH, path); 136 137 startService(playerIntent); 138 139 playBtn.setText("暂停"); 140 141 } 142 }); 143 144 } 145 146 private void event() { 147 // TODO Auto-generated method stub 148 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 149 150 @Override 151 public void onStopTrackingTouch(SeekBar seekBar) { 152 // TODO 拖动结束时的方法 153 //获取当前拖动的位置 154 int seekPosition=seekBar.getProgress(); 155 156 //向播放组件组件发送广播 157 Intent intent=new Intent(Config.ACTION_SEEKTO); 158 intent.putExtra(Config.EXTRA_PROGREES_CUR, seekPosition); 159 160 lbMgr.sendBroadcast(intent);//发送广播(在Service组件内接收) 161 } 162 163 @Override 164 public void onStartTrackingTouch(SeekBar seekBar) { 165 // TODO Auto-generated method stub 166 167 } 168 169 @Override 170 public void onProgressChanged(SeekBar seekBar, int progress, 171 boolean fromUser) { 172 // TODO Auto-generated method stub 173 174 } 175 }); 176 } 177 178 public void play(View v) { //播放或暂停 179 180 playerIntent.putExtra(Config.EXTRA_CHANGE, false); 181 startService(playerIntent); 182 183 isPlaying=!isPlaying; 184 185 if(isPlaying){ 186 playBtn.setText("暂停"); 187 }else{ 188 playBtn.setText("播放"); 189 } 190 } 191 192 public void stop(View v) { //停止 193 stopService(playerIntent); 194 seekBar.setProgress(0); 195 msgTv.setText("00:00/00:00");//=------------------------ 196 197 playBtn.setText("播放"); 198 199 } 200 201 @Override 202 protected void onDestroy() { 203 super.onDestroy(); 204 205 lbMgr.unregisterReceiver(prgReceiver);//取消注册本地广播接收器 206 } 207 208 private String formatTime(int s){ 209 StringBuilder builder=new StringBuilder(); 210 211 int m=s/60; //获取分钟 212 s=s%60; //获取秒数 213 214 builder.append(m/10).append(m%10) 215 .append(":") 216 .append(s/10).append(s%10); 217 218 return builder.toString(); 219 } 220 221 class PrgReceiver extends BroadcastReceiver{ 222 @Override 223 public void onReceive(Context context, Intent intent) { 224 // TODO 接收播放服务组件中发送的进度广播 225 int max=intent.getIntExtra(Config.EXTRA_PROGREES_MAX,0); 226 int cur=intent.getIntExtra(Config.EXTRA_PROGREES_CUR,0); 227 228 seekBar.setMax(max); 229 seekBar.setProgress(cur); 230 231 msgTv.setText(formatTime(cur/1000)+"/"+formatTime(max/1000)); 232 } 233 } 234 235 }
标签:
原文地址:http://www.cnblogs.com/bimingcong/p/4822186.html