代码改变世界

android录音机的一些问题整理

2012-05-25 18:20  htc开发  阅读(1345)  评论(0编辑  收藏  举报

一、不用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);