自做音乐播放器涉及到的相关知识
来源:互联网 发布:金融大数据应用案例 编辑:程序博客网 时间:2024/06/05 06:03
******************音乐播放器******************
*IntentService中有一个onHandleIntent方法
该方法在工作线程执行,当调用startService时,
IntentService将会把onHandleIntent中的逻辑添加
到消息队列中等待执行,工作线程将会轮询该消息队列,
把队列中的消息对象一一获取,并且一一执行.
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");
④注册广播:
Intent intent = new Intent("UPDATE");
intent可以传递数据, intent.putExtra(K , V);
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);
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. 下载音乐:DownService extends IntentService
*IntentService中有一个onHandleIntent方法
该方法在工作线程执行,当调用startService时,
IntentService将会把onHandleIntent中的逻辑添加
到消息队列中等待执行,工作线程将会轮询该消息队列,
把队列中的消息对象一一获取,并且一一执行.
2. 发送通知
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);
Intent intent = new Intent("UPDATE");
intent可以传递数据, intent.putExtra(K , V);
sendBroadcast(intent);
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);
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
- 自做音乐播放器涉及到的相关知识
- 做一个简单的音乐播放器
- 音乐播放器 Songbird 相关
- 使用android模拟器自带的音乐播放器播放音乐
- 今天做音乐播放器的时候遇上的问题
- GDI VC++重绘控件做的音乐播放器
- 自己做的一个动漫风音乐播放器
- WPF做的小型音乐播放器-升级版
- 利用java做一个简单的音乐播放器
- Android开发做一个简单的音乐播放器
- 实用知识:音乐播放的方法使用
- 控制iphone音乐播放器的相关函数
- audacious音乐播放器的安装相关问题
- 做一个虚拟现实的音乐播放软件
- 用service做的音乐播放
- android自带音乐播放器删除音乐失败的问题
- android 自编音乐播放器源代码
- 手把手教你做音乐播放器(五)音乐列表的存储(上)
- DOxygen for C++使用说明——添加数学公式
- 关于The import android.support.v7.app cannot be resolved
- myeclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题 分类: Maven Myeclipse 2014-07-11 1
- Fedora23安装以后要做的优化配置
- js内如何导入jason文件及从做好的文件内获取输入框信息,以及离子特效文件
- 自做音乐播放器涉及到的相关知识
- JAVA快速入门
- PE文件格式分析
- 合并dataset、datatable
- Palindrome Number
- spring MVC 如何获取客户端post过来的数据。
- hibernate中uuid主键注解生成策略
- android studio 新建工程时提示资源文件出错
- 序列化--反序列化:Schema evolution in Avro, Protocol Buffers and Thrift