




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
音频Media生命周期,参考Media yer文档;<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"<uses-permission*音频器示例,注publicclassMainActivityextendsActivity/**进度条privateSeekBar/**类privateMedia yermediayer;privateImageButtonyBT;privateProgressDialogdialog;protectedvoidonCreate(BundlesavedInstanceState){musicSB=(SeekBar)findViewById(R.id.musicSB);yBT=(ImageButton)findViewById(R.id.yBT);//==服务 ephonyManager=(ephonyManager) ephonyManager.listen(newMyPhoneSta }//==退出按钮调用销毁方法==protectedvoidonDestroy()if yer!=null)mediayer.stop(); //停止音乐mediayer.release(); //释放资源mediayer=null;}}//==方法public y(Viewv)if yer==null)try yer=new // yer= yer.create(context, yUrl=String.format("%s/%s",Environment.getExternalStorageDirectory(),"daqin.mp3"); //音频路径,本地 yUrl=" //设置为音频 //准备,在主 加载文 //推荐使用,异步准备,在新线程加载文 //准备好,准备好//显示加载}catch(Exceptione){}}elseif(media ying()) //暂}else //继}//==设置按钮图标== ying()?android.R.drawable.ic_media_pause:android.R.drawable.ic_media}//==显示加载dialogprivatevoidshowDialog()dialog=newProgressDialog(this);}/* 相关//==准备完毕实现类privateclassMyPreparedListenerimplementsOnPreparedListenerpublicvoidonPrepared(Media yermp){ //准new //启MySeekBarChangeListener());//}
}//==状态privateclass istenerextends istenerprivateboolean //是否暂publicvoidonCallStateChanged(intstate, if(mediayer!=null)switch(state)case //来电了,停if(media ying()){ isPause=true;}caseif(isPause //如果是来电导致的暂停,则继 isPause=false;}} yer.isying()android.R.drawable.ic_media_pause:android.R.drawable.ic_media}}}//== yer完成类private pletionListener public yermp)if yer!=null) //停止音乐 //释放资源 yer=null; //回收 //还原进度条}}}//==SeekBar进度条实现类privateclassMySeekBarChangeListenerimplementsOnSeekBarChangeListenerpublicvoidonStopTrackingTouch(SeekBarseekBar)if yer!=null) //把器调整到进条的位}}publicvoidonStartTrackingTouch(SeekBarseekBar)publicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser)}/* 相关/* 线程相关//==启用新的线程,显示进度privateclassSeekBarThreadextendsThreadpublicvoidrun() //获取音频文件的时长,设置到while yer!=null) if(mediayer.is ying()&&!musicSB.isPressed()){ //设置SeekBar}try}catch(InterruptedExceptione){}}}}/* 线程相关}应用场如果使用Media yer每次加载音频、、再释放,这样很费事;我们可以使用音频池加载多个音频,需要哪个就直接;代码实intidpool.load(thisR.raw.jiaodizhu1);//异步加载,需要一点时pool.y(id,1,1,0,0,1); //音频id,左音量,右音量,优先级,是否重复,SurfaceView使用Mediayer来,SurfaceView来展示;位置,转到前台时,再从指定的位置;添加器,来实现摧毁和重构的;onCreate准备完毕新增(seeko:拍照需限<uses-permissionandroid:name="android.permission.CAMERA"<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"打开头SDK2.3之后支持前置头,open方法可以接收一个int参数,用来指定哪个头注意SurfaceView不端显示的时候会被销毁,恢复之后会重绘,需其,创建与销毁;注意拍照之后头不回自动回到预览状态,需要重写调用startPreview()方法;publicclassMainActivityextendsActivity{privateSurfaceViewpreviewSV;privateCamerac;protectedvoidonCreate(BundlesavedInstanceState){previewSV=(SurfaceView)findViewById(R.id.previewSV); previewSV.getHolder().addCallback(newMyCallback()); }//==SurfaceView的重构和摧毁privateclassMyCallbackimplementsCallbackpublicvoidsurfaceCreated(SurfaceHolderholder) //打开头,准备预c=Camera.open();Parametersparameters //获取默认参 //设 大 //设 质 //闪光灯 到try
//设置预览显示位}catch(IOExceptione){}}publicvoidsurfaceDestroyed(SurfaceHolderholder) //停止预览,释放if(c!=null) //停止预览 //释放资源c=null;}}publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight)}}/*拍照publicvoidtake(Viewv)//==自动对焦==c.autoFocus(newAutoFocusCallbackpublicvoidonAutoFocus(booleansuccess,Cameracamera) 数据生参数 akePicture(null,null,newPictureCallback()publicvoidonPictureTaken(byte[]data,Cameracamera){StringfilePath=getStoreFilePath();tryOutputStreamos=newFileOutputStream(filePath); }catch(Exceptione){}
}
privateStringgetStoreFilePath()if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))returnEnvironment.getExternalStorageDirectory()+"/"+System.currentTimeMillis()+".mp4";}}}2.2需<uses-permissionnewsetAudioSource(int设 setOutputFormat(intsetAudioEncoder(int设 y(SurfacesetOutputFile(StringpublicclassMainActivityextendsActivityprivateImageButtontakerIB;privateSurfaceViewpreviewSV;privateCamerac;privateMediaRecorderrecorder;protectedvoidonCreate(BundlesavedInstanceState)takerIB=(ImageButton)findViewById(R.id.takeIB);previewSV.getHolder().addCallback(newMyCallback()); //}//==SurfaceView的重构和摧毁privateclassMyCallbackimplementsCallbackpublicvoidsurfaceCreated(SurfaceHolderholder) //打开头,准备览try
c= //设置预览显示位 //预}catch(IOExceptione){}}publicvoidsurfaceDestroyed(SurfaceHolderholder 头if(c!=null) //停止预 //释放资c=}}publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight)}}//==按钮点击方法==publicvoidtake(Viewv)throwsExceptionif(recorder==null) //recorder=newMediaRecorder();
));//视 //准 //开始录}else //停止录 //停 //释recorder=null;}}/**获取存取路径,优先SD卡@returnprivateStringgetStoreFilePath()if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))returnEnvironment.getExternalStorageDirectory()+"/"+System.currentTimeMillis()+".mp4";}}}使用系统的头应//==照相使用系统的头应用,不需要配置权限publicvoidtakeImage(Viewv)Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);System.currentTimeMillis()+".jpg"));startActivityForResult(intent,100);}//==publicvoid (Viewv)Intentintent=newIntent(MediaStore.ACTION_CAPTURE);System.currentTimeMillis()+".mp4")); startActivityForResult(intent,200);}activity关闭返回调用方法==protectedvoidonActivityResult(intreqode,intresultCode,Intentdata){Intentintent=newIntent(Intent.ACTION_VIEW);switch(resultCode)case //拍intent.setDataAndType(imageUri,"image/*"); //使用系统默认的工具打开case // Uri," }}/**获取存取路径,优先SD卡@returnprivateStringgetStoreFilePath()if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))returnEnvironment.getExternalStorageDirectory()+}returngetFilesDir()+}获取图库中的Intentintent=newIntent();startActivityForResult(intent,100);protectedvoidonActivityResult(int ode,intresultCode,Intentdata)if(data!=null&&reqode==100)Uriuri=data.getData(); //获取图库中被选中文件的UriStringpath=getAbsolutePath(uri); //根据Uri获取绝对路径Bitmapbm //根据路径获取一个Bitmap(已缩放,避ImageViewimageView=(ImageView)findViewById(R.id.imageView);}}UriprivateStringgetAbsolutePath(Uriuri)Cursorc=getContentResolver().query(uri,new{MediaStore.Images.Media.DATA},null,null,null);return}加载大在大多数的Android中,每个应用程序可以使用的内存被限制在在加载的时候,按屏幕大小对进行缩放,既不影响显示效果,又节省了内存;privateBitmapgetUsableImage(Stringpath)Optionsopts=newOptions(); //选项对象(在加载时使用)opts.inJustDecodeBounds=true; //修改选项,只获取大小(没有全部加载到内存中,的2进制数据中,会有的一些信息BitmapFactory.decodeFile(path, //加载(只得到大小WindowManagermanagerWindowManager)getSystemService(WINDOW_SERVICE);//获取窗体管理器intscaleX=opts.outWidth/ is//X轴缩放比例(宽度/屏幕宽度intscaleY=opts.outHeight/ Y轴intscale=scaleX>scaleY?scaleX://的缩放比例(X和Y哪个大选哪个opts.inJustDecodeBounds=false; //修改选项,不只 opts.inSampleSize=scale>1?scale:1; //修改选项,加载 //加载(得到缩放后的}获取Exif信息(的一些信息ExifInterfaceexif=newExifInterface("/mnt/sdcard/test.jpg"); //拍摄时间 //拍摄设备 //制造商 //方向System.out.println(exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));//
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论