录音机的一些问题

  • 一、不用service实现后台录音功能

    1.在onPause()方法内实现:

     

    01. if (mRecorder.state() == Recorder.RECORDING_STATE) {
    02.  
    03. if (mMode == true) {
    04. mRecorder.stop();
    05. return;
    06. }
    07.  
    08. Intent notificationIntent = new Intent("android.provider.MediaStore.RECORD_SOUND");
    09.  
    10. PendingIntent contentIntent = PendingIntent.getActivity(this0, notificationIntent, 0);
    11.  
    12. long when = System.currentTimeMillis(); // notification time
    13.  
    14. Context context = getApplicationContext();
    15.  
    16. Notification notification = new Notification(R.drawable.ic_launcher_soundrecorder, null, when);
    17.  
    18. Resources res = getResources();
    19.  
    20. String message1 = res.getString(R.string.app_name);
    21.  
    22. String message2 = res.getString(R.string.recording_in_progress);
    23. notification.setLatestEventInfo(context, message1, message2, contentIntent);
    24. String ns = Context.NOTIFICATION_SERVICE;
    25. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    26. mNotificationManager.notify(NOTIFICATION_ID, notification);
    27. mInNotification = true;
    28. }

    2.onResume()方法内去掉通知栏内的通知

     

    01. if (mInNotification == true) {
    02.  
    03. String ns = Context.NOTIFICATION_SERVICE;
    04. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    05.  
    06. mNotificationManager.cancel(NOTIFICATION_ID);
    07.  
    08. mInNotification = false;
    09. }

    3.在androidmanifext.xml中设置activity mode为singleTop。

    二、录音文件使用当前时间

     

    01. public String getCurrentTime(){
    02. SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
    03. String nowTime=sdf.format(new Date());
    04. return nowTime;
    05. }
    06. public  File createTempFile_test(String prefix, String suffix, File directory)
    07. throws IOException {
    08. // Force a prefix null check first
    09. if (prefix.length() < 3) {
    10. throw new IllegalArgumentException("prefix must be at least 3 characters");
    11. }
    12. if (suffix == null) {
    13. suffix = ".tmp";
    14. }
    15. File tmpDirFile = directory;
    16. if (tmpDirFile == null) {
    17. String tmpDir = System.getProperty("java.io.tmpdir"".");
    18. tmpDirFile = new File(tmpDir);
    19. }
    20. File result;
    21. result = new File(tmpDirFile, prefix + getCurrentTime() + suffix);
    22. if(result.exists()){
    23. result.delete();
    24. }
    25. if(!result.exists()){
    26. result.createNewFile();
    27. }
    28. return result;
    29. }

    三。解决先开启一个录音播放器,按home键进去后台播放,此时通过短信附件开启录音机,这时应该关闭后台的那个录音机

    解决办法使用广播进行关闭,代码如下:


    01. //处理广播
    02.  
    03. public class SoundRecorder extends Activity
    04. implements Button.OnClickListener, Recorder.OnStateChangedListener, OnSeekBarChangeListener {
    05. private BroadcastReceiver mReceiver=new BroadcastReceiver() {
    06. @Override
    07. public void onReceive(Context context, Intent intent) {
    08. // TODO Auto-generated method stub
    09. if(mRecorder!=null){
    10. mRecorder.stop();
    11. saveSample();
    12. mRecorder.clear();
    13. }
    14. if (mInNotification == true) {
    15.  
    16.  
    17. String ns = Context.NOTIFICATION_SERVICE;
    18. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    19.  
    20.  
    21. mNotificationManager.cancel(NOTIFICATION_ID);
    22.  
    23.  
    24. mInNotification = false;
    25. }
    26.  
    27.  
    28. }
    29. };
    30.  
    31. //onCreate()中先发送广播再注册广播,第一次就接受不到广播,等第三方应用调用时起作用
    32.  
    33. Intent intent=new Intent();
    34. intent.setAction(ACTION_STOP);
    35. sendBroadcast(intent);
    36.  
    37.  
    38. IntentFilter filter = new IntentFilter();
    39. filter.addAction(ACTION_STOP);
    40. this.registerReceiver(mReceiver, filter);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值