自做音乐播放器涉及到的相关知识

来源:互联网 发布:金融大数据应用案例 编辑:程序博客网 时间:2024/06/05 06:03
******************音乐播放器******************


1. 下载音乐:DownService extends IntentService


*IntentService中有一个onHandleIntent方法
 该方法在工作线程执行,当调用startService时,
 IntentService将会把onHandleIntent中的逻辑添加
 到消息队列中等待执行,工作线程将会轮询该消息队列,
 把队列中的消息对象一一获取,并且一一执行.

2. 发送通知


1>创建Notification.Builder对象
  Notification.Builder builder = new Notification.Builder(Context)


2>设置Notification.Builder对象的属性(setSmallIcon属性
   必须设置才能显示)

3>获得通知管理器(属于一个系统服务)
  NotificationManager nManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

4>可以设置延迟意图PendingIntent
   PendingIntent intent =PendingIntent.getActivity(this,
   100, new Intent(this,OtherActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
   builder.setContentIntent(intent);

5>创建通知
  Notification ntf =bulider.Bulder();

6>可以设置通知的特性
   ntf.flags = Notification.FLAG_AUTO_CANCEL;
  
7>发送通知
  nManager.notify(Notification_ID, ntf);

8>清除通知
  manager.cancel(NOTIFICATION_ID);


3.广播

1>注册广播
①广播接收器类继承BrodcastReceiver:
  class MyReceiver extends BrodcastReceiver{};  //广播接收器类,该类中                                       onReceiver方法为接收到广播后执行的方法
②创建广播接收器类对象:
  MyReceiver receiver = new MyReceiver();
③添加广播过滤器:
  IntentFilter  ifilter = new IntentFilter();
  ifilter.addAction("UPDATE");
④注册广播:

  registerReceiver(receiver,ifilter);


2>发送广播:
  Intent intent = new Intent("UPDATE");
  intent可以传递数据, intent.putExtra(K , V);

  sendBroadcast(intent);


3>取消广播
  unregisterReceiver(receiver);

4.图片压缩处理
1>创建Opts对象封装图片配置信息:
  Opts opts = new Opts();
2>设置opts对象inJustDecodeBounds属性为true,表示仅仅加载图片边界属性
  opts.inJustDecodeBounds=true;
  BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
3>获取原始的宽度与高度:
  int width = opts.outWind;
  int height = opts.outHeight;
4>获得屏幕尺寸(或自定义压缩尺寸):
  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay.getMetrics(dm);
  int winWidth = dm.widthPixels;
  int winHeight = dm.heightPixels;
5>计算压缩比例
  double x = width*1.0/winWindth;
  double y = height*1.0/winHeight;
  if(x>y&&x>1){
  opts.inSampleSize=(int)x;
   }else{
     opts.inSampleSize=(int)y;
   }
6>设置图片读取方式不仅仅加载边界信息:
  opts.inJustDecodeBounds=false;
7>压缩图片
  BitmapFactory.decodeByteArray(bytes,0,bytes.length,opts);

5.保存图片,读取图片:

1>保存图片(根据图片,路径):
  FileOutputStream fos = new FileOutputStream(path);
  bitmap.compress(CompressFormat.JPEG,100,fos)//JPEG为保存图片格式,100为压缩比例

2>读取图片(根据路径):
  Bitmap bitmap = BitmapFactory.decodeFile(path);



6.播放音乐
1>创建播放器对象:
  MediaPlayer mp = new MediaPlayer();
2>重置播放器
  mp.reset();
3>设置数据源:
  mp.serDataSource(path);
4>加载音乐:
  mp.prepare();
5>播放音乐
  mp.start();
6>暂停音乐
  mp.pause();
7>停止播放音乐,释放资源:
  mp.release();
  mp=null;


7.从网络下载音乐,边下载变保存:
1>发送网络请求(get):
  HttpClient client = new DefaultHttpClient();
  String uri="网络路径";
  HttpGet get = new HttpGet(uri);
  HttpResponse resp = client.execute(get);
  HttpEntity entity = resp.getEntity();
2>输入、输出流:
  InputStream is = entity.getContent();
  FileOutStream fos = new FileOutStream(path);
3>边读边保存:
  byte[]buffer = new byte[1024*100];
  int length=0;
  long total = entity.getContentLength();
  long current=0;
  while((length=is.read(buffer))!=-1){
       fos.write(buffer,0,length)  
       fos.flush();
       current+=length; //下载字节
       double text = Math.floor(100.0*current/total); //下载完成百分比
  }


*对于像List<Music>这类集合要通过intent传递时,Music实例类要经过实现序列化(Serializable);
1 0
原创粉丝点击