小米录音机源码

移动开发
MIUI录音机是基于Android原生录音机来开发的,能够运行在Android2.3.3及以上版本。与Android原生录音机相比,MIUI录音机在UI、交互方式和录音效果上都有较大的提升,其磁带的显示效果更是收到了很多用户的好评

源码简介:MIUI录音机是基于Android原生录音机来开发的,能够运行在Android2.3.3及以上版本。与Android原生录音机相比,MIUI录音机在UI、交互方式和录音效果上都有较大的提升,其磁带的显示效果更是收到了很多用户的好评

源码效果:

源码片段:

  1. /* 
  2.  * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *        http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */ 
  16.   
  17. package net.micode.soundrecorder; 
  18.   
  19. import android.app.KeyguardManager; 
  20. import android.app.Notification; 
  21. import android.app.NotificationManager; 
  22. import android.app.PendingIntent; 
  23. import android.app.Service; 
  24. import android.content.Context; 
  25. import android.content.Intent; 
  26. import android.media.AudioManager; 
  27. import android.media.MediaRecorder; 
  28. import android.net.Uri; 
  29. import android.os.Bundle; 
  30. import android.os.Handler; 
  31. import android.os.IBinder; 
  32. import android.os.PowerManager; 
  33. import android.os.PowerManager.WakeLock; 
  34. import android.telephony.PhoneStateListener; 
  35. import android.telephony.TelephonyManager; 
  36.   
  37. import java.io.File; 
  38. import java.io.IOException; 
  39.   
  40. public class RecorderService extends Service implements MediaRecorder.OnErrorListener { 
  41.   
  42.     public final static String ACTION_NAME = "action_type"
  43.   
  44.     public final static int ACTION_INVALID = 0
  45.   
  46.     public final static int ACTION_START_RECORDING = 1
  47.   
  48.     public final static int ACTION_STOP_RECORDING = 2
  49.   
  50.     public final static int ACTION_ENABLE_MONITOR_REMAIN_TIME = 3
  51.   
  52.     public final static int ACTION_DISABLE_MONITOR_REMAIN_TIME = 4
  53.   
  54.     public final static String ACTION_PARAM_FORMAT = "format"
  55.   
  56.     public final static String ACTION_PARAM_PATH = "path"
  57.   
  58.     public final static String ACTION_PARAM_HIGH_QUALITY = "high_quality"
  59.   
  60.     public final static String ACTION_PARAM_MAX_FILE_SIZE = "max_file_size"
  61.   
  62.     public final static String RECORDER_SERVICE_BROADCAST_NAME = "com.android.soundrecorder.broadcast"
  63.   
  64.     public final static String RECORDER_SERVICE_BROADCAST_STATE = "is_recording"
  65.   
  66.     public final static String RECORDER_SERVICE_BROADCAST_ERROR = "error_code"
  67.   
  68.     public final static int NOTIFICATION_ID = 62343234
  69.   
  70.     private static MediaRecorder mRecorder = null
  71.   
  72.     private static String mFilePath = null
  73.   
  74.     private static long mStartTime = 0
  75.   
  76.     private RemainingTimeCalculator mRemainingTimeCalculator; 
  77.   
  78.     private NotificationManager mNotifiManager; 
  79.   
  80.     private Notification mLowStorageNotification; 
  81.   
  82.     private TelephonyManager mTeleManager; 
  83.   
  84.     private WakeLock mWakeLock; 
  85.   
  86.     private KeyguardManager mKeyguardManager; 
  87.   
  88.     private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() { 
  89.         @Override 
  90.         public void onCallStateChanged(int state, String incomingNumber) { 
  91.             if (state != TelephonyManager.CALL_STATE_IDLE) { 
  92.                 localStopRecording(); 
  93.             } 
  94.         } 
  95.     }; 
  96.   
  97.     private final Handler mHandler = new Handler(); 
  98.   
  99.     private Runnable mUpdateRemainingTime = new Runnable() { 
  100.         public void run() { 
  101.             if (mRecorder != null && mNeedUpdateRemainingTime) { 
  102.                 updateRemainingTime(); 
  103.             } 
  104.         } 
  105.     }; 
  106.   
  107.     private boolean mNeedUpdateRemainingTime; 
  108.   
  109.     @Override 
  110.     public void onCreate() { 
  111.         super.onCreate(); 
  112.         mRecorder = null
  113.         mLowStorageNotification = null
  114.         mRemainingTimeCalculator = new RemainingTimeCalculator(); 
  115.         mNeedUpdateRemainingTime = false
  116.         mNotifiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
  117.         mTeleManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
  118.         mTeleManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); 
  119.         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
  120.         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SoundRecorder"); 
  121.         mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 
  122.     } 
  123.   
  124.     @Override 
  125.     public int onStartCommand(Intent intent, int flags, int startId) { 
  126.         Bundle bundle = intent.getExtras(); 
  127.         if (bundle != null && bundle.containsKey(ACTION_NAME)) { 
  128.             switch (bundle.getInt(ACTION_NAME, ACTION_INVALID)) { 
  129.                 case ACTION_START_RECORDING: 
  130.                     localStartRecording(bundle.getInt(ACTION_PARAM_FORMAT), 
  131.                             bundle.getString(ACTION_PARAM_PATH), 
  132.                             bundle.getBoolean(ACTION_PARAM_HIGH_QUALITY), 
  133.                             bundle.getLong(ACTION_PARAM_MAX_FILE_SIZE)); 
  134.                     break
  135.                 case ACTION_STOP_RECORDING: 
  136.                     localStopRecording(); 
  137.                     break
  138.                 case ACTION_ENABLE_MONITOR_REMAIN_TIME: 
  139.                     if (mRecorder != null) { 
  140.                         mNeedUpdateRemainingTime = true
  141.                         mHandler.post(mUpdateRemainingTime); 
  142.                     } 
  143.                     break
  144.                 case ACTION_DISABLE_MONITOR_REMAIN_TIME: 
  145.                     mNeedUpdateRemainingTime = false
  146.                     if (mRecorder != null) { 
  147.                         showRecordingNotification(); 
  148.                     } 
  149.                     break
  150.                 default
  151.                     break
  152.             } 
  153.             return START_STICKY; 
  154.         } 
  155.         return super.onStartCommand(intent, flags, startId); 
  156.     } 
  157.   
  158.     @Override 
  159.     public void onDestroy() { 
  160.         mTeleManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); 
  161.         if (mWakeLock.isHeld()) { 
  162.             mWakeLock.release(); 
  163.         } 
  164.         super.onDestroy(); 
  165.     } 
  166.   
  167.     @Override 
  168.     public IBinder onBind(Intent intent) { 
  169.         return null
  170.     } 
  171.   
  172.     @Override 
  173.     public void onLowMemory() { 
  174.         localStopRecording(); 
  175.         super.onLowMemory(); 
  176.     } 
  177.   
  178.     private void localStartRecording(int outputfileformat, String path, boolean highQuality, 
  179.             long maxFileSize) { 
  180.         if (mRecorder == null) { 
  181.             mRemainingTimeCalculator.reset(); 
  182.             if (maxFileSize != -1) { 
  183.                 mRemainingTimeCalculator.setFileSizeLimit(new File(path), maxFileSize); 
  184.             } 
  185.   
  186.             mRecorder = new MediaRecorder(); 
  187.             mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
  188.             if (outputfileformat == MediaRecorder.OutputFormat.THREE_GPP) { 
  189.                 mRemainingTimeCalculator.setBitRate(SoundRecorder.BITRATE_3GPP); 
  190.                 mRecorder.setAudioSamplingRate(highQuality ? 44100 : 22050); 
  191.                 mRecorder.setOutputFormat(outputfileformat); 
  192.                 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
  193.             } else { 
  194.                 mRemainingTimeCalculator.setBitRate(SoundRecorder.BITRATE_AMR); 
  195.                 mRecorder.setAudioSamplingRate(highQuality ? 16000 : 8000); 
  196.                 mRecorder.setOutputFormat(outputfileformat); 
  197.                 mRecorder.setAudioEncoder(highQuality ? MediaRecorder.AudioEncoder.AMR_WB 
  198.                         : MediaRecorder.AudioEncoder.AMR_NB); 
  199.             } 
  200.             mRecorder.setOutputFile(path); 
  201.             mRecorder.setOnErrorListener(this); 
  202.   
  203.             // Handle IOException 
  204.             try { 
  205.                 mRecorder.prepare(); 
  206.             } catch (IOException exception) { 
  207.                 sendErrorBroadcast(Recorder.INTERNAL_ERROR); 
  208.                 mRecorder.reset(); 
  209.                 mRecorder.release(); 
  210.                 mRecorder = null
  211.                 return
  212.             } 
  213.             // Handle RuntimeException if the recording couldn't start 
  214.             try { 
  215.                 mRecorder.start(); 
  216.             } catch (RuntimeException exception) { 
  217.                 AudioManager audioMngr = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
  218.                 boolean isInCall = (audioMngr.getMode() == AudioManager.MODE_IN_CALL); 
  219.                 if (isInCall) { 
  220.                     sendErrorBroadcast(Recorder.IN_CALL_RECORD_ERROR); 
  221.                 } else { 
  222.                     sendErrorBroadcast(Recorder.INTERNAL_ERROR); 
  223.                 } 
  224.                 mRecorder.reset(); 
  225.                 mRecorder.release(); 
  226.                 mRecorder = null
  227.                 return
  228.             } 
  229.             mFilePath = path; 
  230.             mStartTime = System.currentTimeMillis(); 
  231.             mWakeLock.acquire(); 
  232.             mNeedUpdateRemainingTime = false
  233.             sendStateBroadcast(); 
  234.             showRecordingNotification(); 
  235.         } 
  236.     } 
  237.   
  238.     private void localStopRecording() { 
  239.         if (mRecorder != null) { 
  240.             mNeedUpdateRemainingTime = false
  241.             try { 
  242.                 mRecorder.stop(); 
  243.             } catch (RuntimeException e) { 
  244.             } 
  245.             mRecorder.release(); 
  246.             mRecorder = null
  247.   
  248.             sendStateBroadcast(); 
  249.             showStoppedNotification(); 
  250.         } 
  251.         stopSelf(); 
  252.     } 
  253.   
  254.     private void showRecordingNotification() { 
  255.         Notification notification = new Notification(R.drawable.stat_sys_call_record, 
  256.                 getString(R.string.notification_recording), System.currentTimeMillis()); 
  257.         notification.flags = Notification.FLAG_ONGOING_EVENT; 
  258.         PendingIntent pendingIntent; 
  259.         pendingIntent = PendingIntent 
  260.                 .getActivity(this0new Intent(this, SoundRecorder.class), 0); 
  261.   
  262.         notification.setLatestEventInfo(this, getString(R.string.app_name), 
  263.                 getString(R.string.notification_recording), pendingIntent); 
  264.   
  265.         startForeground(NOTIFICATION_ID, notification); 
  266.     } 
  267.   
  268.     private void showLowStorageNotification(int minutes) { 
  269.         if (mKeyguardManager.inKeyguardRestrictedInputMode()) { 
  270.             // it's not necessary to show this notification in lock-screen 
  271.             return
  272.         } 
  273.   
  274.         if (mLowStorageNotification == null) { 
  275.             mLowStorageNotification = new Notification(R.drawable.stat_sys_call_record_full, 
  276.                     getString(R.string.notification_recording), System.currentTimeMillis()); 
  277.             mLowStorageNotification.flags = Notification.FLAG_ONGOING_EVENT; 
  278.         } 
  279.   
  280.         PendingIntent pendingIntent; 
  281.         pendingIntent = PendingIntent 
  282.                 .getActivity(this0new Intent(this, SoundRecorder.class), 0); 
  283.   
  284.         mLowStorageNotification.setLatestEventInfo(this, getString(R.string.app_name), 
  285.                 getString(R.string.notification_warning, minutes), pendingIntent); 
  286.         startForeground(NOTIFICATION_ID, mLowStorageNotification); 
  287.     } 
  288.   
  289.     private void showStoppedNotification() { 
  290.         stopForeground(true); 
  291.         mLowStorageNotification = null
  292.   
  293.         Notification notification = new Notification(R.drawable.stat_sys_call_record, 
  294.                 getString(R.string.notification_stopped), System.currentTimeMillis()); 
  295.         notification.flags = Notification.FLAG_AUTO_CANCEL; 
  296.         Intent intent = new Intent(Intent.ACTION_VIEW); 
  297.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  298.         intent.setType("audio/*"); 
  299.         intent.setDataAndType(Uri.fromFile(new File(mFilePath)), "audio/*"); 
  300.   
  301.         PendingIntent pendingIntent; 
  302.         pendingIntent = PendingIntent.getActivity(this0, intent, 
  303.                 PendingIntent.FLAG_UPDATE_CURRENT); 
  304.   
  305.         notification.setLatestEventInfo(this, getString(R.string.app_name), 
  306.                 getString(R.string.notification_stopped), pendingIntent); 
  307.         mNotifiManager.notify(NOTIFICATION_ID, notification); 
  308.     } 
  309.   
  310.     private void sendStateBroadcast() { 
  311.         Intent intent = new Intent(RECORDER_SERVICE_BROADCAST_NAME); 
  312.         intent.putExtra(RECORDER_SERVICE_BROADCAST_STATE, mRecorder != null); 
  313.         sendBroadcast(intent); 
  314.     } 
  315.   
  316.     private void sendErrorBroadcast(int error) { 
  317.         Intent intent = new Intent(RECORDER_SERVICE_BROADCAST_NAME); 
  318.         intent.putExtra(RECORDER_SERVICE_BROADCAST_ERROR, error); 
  319.         sendBroadcast(intent); 
  320.     } 
  321.   
  322.     private void updateRemainingTime() { 
  323.         long t = mRemainingTimeCalculator.timeRemaining(); 
  324.         if (t <= 0) { 
  325.             localStopRecording(); 
  326.             return
  327.         } else if (t <= 1800 
  328.                 && mRemainingTimeCalculator.currentLowerLimit() != RemainingTimeCalculator.FILE_SIZE_LIMIT) { 
  329.             // less than half one hour 
  330.             showLowStorageNotification((int) Math.ceil(t / 60.0)); 
  331.         } 
  332.   
  333.         if (mRecorder != null && mNeedUpdateRemainingTime) { 
  334.             mHandler.postDelayed(mUpdateRemainingTime, 500); 
  335.         } 
  336.     } 
  337.   
  338.     public static boolean isRecording() { 
  339.         return mRecorder != null
  340.     } 
  341.   
  342.     public static String getFilePath() { 
  343.         return mFilePath; 
  344.     } 
  345.   
  346.     public static long getStartTime() { 
  347.         return mStartTime; 
  348.     } 
  349.   
  350.     public static void startRecording(Context context, int outputfileformat, String path, 
  351.             boolean highQuality, long maxFileSize) { 
  352.         Intent intent = new Intent(context, RecorderService.class); 
  353.         intent.putExtra(ACTION_NAME, ACTION_START_RECORDING); 
  354.         intent.putExtra(ACTION_PARAM_FORMAT, outputfileformat); 
  355.         intent.putExtra(ACTION_PARAM_PATH, path); 
  356.         intent.putExtra(ACTION_PARAM_HIGH_QUALITY, highQuality); 
  357.         intent.putExtra(ACTION_PARAM_MAX_FILE_SIZE, maxFileSize); 
  358.         context.startService(intent); 
  359.     } 
  360.   
  361.     public static void stopRecording(Context context) { 
  362.         Intent intent = new Intent(context, RecorderService.class); 
  363.         intent.putExtra(ACTION_NAME, ACTION_STOP_RECORDING); 
  364.         context.startService(intent); 
  365.     } 
  366.   
  367.     public static int getMaxAmplitude() { 
  368.         return mRecorder == null ? 0 : mRecorder.getMaxAmplitude(); 
  369.     } 
  370.   
  371.     @Override 
  372.     public void onError(MediaRecorder mr, int what, int extra) { 
  373.         sendErrorBroadcast(Recorder.INTERNAL_ERROR); 
  374.         localStopRecording(); 
  375.     } 

下载地址:http://down.51cto.com/data/2089509

责任编辑:倪明 来源: devstore
相关推荐

2022-05-13 23:03:55

Windows 10微软应用程序

2018-01-31 09:00:54

内核源码系统

2013-12-12 13:29:07

不刷机小米系统雷军MIUI

2021-08-27 12:38:08

Windows 11操作系统微软

2014-05-15 17:09:21

小米平板小米电视2新品发布

2013-10-09 10:12:13

小米智能

2012-05-01 16:44:50

小米

2012-06-08 15:10:24

欧洲杯小米俄罗斯

2015-03-09 14:03:04

2015-04-15 14:07:21

2012-04-13 13:44:54

小米

2015-03-10 17:47:48

小米手机小米4恶意软件

2012-04-12 09:37:28

小米

2013-12-09 11:00:37

小米雷军

2021-03-29 22:12:04

小米小米11 Pro小米11 Ultra

2012-07-25 10:57:40

传真机

2011-11-28 15:15:40

传真机行情

2021-09-27 15:16:11

小米MIUI12.5

2012-06-27 09:57:35

小米手机

2011-01-18 09:40:48

Linux声卡
点赞
收藏

51CTO技术栈公众号