android录音机的一些问题整理

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

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

if (mRecorder.state() == Recorder.RECORDING_STATE) {

            if (mMode == true) {
                mRecorder.stop();
                return;
            }

            Intent notificationIntent = new Intent("android.provider.MediaStore.RECORD_SOUND");

            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            long when = System.currentTimeMillis(); // notification time

            Context context = getApplicationContext();

            Notification notification = new Notification(R.drawable.ic_launcher_soundrecorder, null, when);

            Resources res = getResources();

            String message1 = res.getString(R.string.app_name);

            String message2 = res.getString(R.string.recording_in_progress);
            notification.setLatestEventInfo(context, message1, message2, contentIntent);
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
            mNotificationManager.notify(NOTIFICATION_ID, notification);
            mInNotification = true;
        }

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

  if (mInNotification == true) {

            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

            mNotificationManager.cancel(NOTIFICATION_ID);

            mInNotification = false;
        }

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



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

 public String getCurrentTime(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
        String nowTime=sdf.format(new Date());
        return nowTime;
    }
    public  File createTempFile_test(String prefix, String suffix, File directory)
            throws IOException {
        // Force a prefix null check first
        if (prefix.length() < 3) {
            throw new IllegalArgumentException("prefix must be at least 3 characters");
        }
        if (suffix == null) {
            suffix = ".tmp";
        }
        File tmpDirFile = directory;
        if (tmpDirFile == null) {
            String tmpDir = System.getProperty("java.io.tmpdir", ".");
            tmpDirFile = new File(tmpDir);
        }
        File result;
            result = new File(tmpDirFile, prefix + getCurrentTime() + suffix);
            if(result.exists()){
                result.delete();
            }
            if(!result.exists()){
               result.createNewFile();
            }
        return result;
    }


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

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


//处理广播

public class SoundRecorder extends Activity
        implements Button.OnClickListener, Recorder.OnStateChangedListener, OnSeekBarChangeListener {
    private BroadcastReceiver mReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if(mRecorder!=null){
                mRecorder.stop();
                saveSample();
                mRecorder.clear();
            }
            if (mInNotification == true) {


                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);


                mNotificationManager.cancel(NOTIFICATION_ID);


                mInNotification = false;
            }


            }
        };

//onCreate()中先发送广播再注册广播,第一次就接受不到广播,等第三方应用调用时起作用

 Intent intent=new Intent();
        intent.setAction(ACTION_STOP);
        sendBroadcast(intent);


        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_STOP);
        this.registerReceiver(mReceiver, filter);

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值