0 快速索引表

  • 权限汇总

https://developer.android.google.cn/reference/android/Manifest.permission

/frameworks/base/core/res/AndroidManifest.xml

1

  • 接口 汇总

https://developer.android.google.cn/reference/android/media/MediaRecorder

/frameworks/base/media/java/android/media/MediaRecorder.java

android.media.MediaRecorder.prepare

android.media.MediaRecorder.start

android.media.MediaRecorder.stop

https://developer.android.google.cn/reference/android/media/AudioRecord

/frameworks/base/media/java/android/media/AudioRecord.java

android.media.AudioRecord.getState

android.media.AudioRecord.startRecording

android.media.AudioRecord.stop

android.media.AudioRecord.read

https://developer.android.google.cn/reference/android/media/ImageReader

/frameworks/base/media/java/android/media/ImageReader.java

android.media.ImageReader.acquireLatestImage

https://developer.android.google.cn/reference/android/media/projection/MediaProjection

/frameworks/base/media/java/android/media/projection/MediaProjection.java

android.media.projection.MediaProjection.createVirtualDisplay

android.media.projection.MediaProjection.registerCallback

  • adb shell media命令汇总

adb shell media

  • developer

5


1 需求


2 权限


3 接口

android.media.MediaRecorder.prepare

  • Added in API level 1
1289      /**
1290       * Prepares the recorder to begin capturing and encoding data. This method
1291       * must be called after setting up the desired audio and video sources,
1292       * encoders, file format, etc., but before start().
1293       *
1294       * @throws IllegalStateException if it is called after
1295       * start() or before setOutputFormat().
1296       * @throws IOException if prepare fails otherwise.
1297       */
1298      public void prepare() throws IllegalStateException, IOException
1299      {
1300          if (mPath != null) {
1301              RandomAccessFile file = new RandomAccessFile(mPath, "rw");
1302              try {
1303                  _setOutputFile(file.getFD());
1304              } finally {
1305                  file.close();
1306              }
1307          } else if (mFd != null) {
1308              _setOutputFile(mFd);
1309          } else if (mFile != null) {
1310              RandomAccessFile file = new RandomAccessFile(mFile, "rw");
1311              try {
1312                  _setOutputFile(file.getFD());
1313              } finally {
1314                  file.close();
1315              }
1316          } else {
1317              throw new IOException("No valid output file");
1318          }
1319
1320          _prepare();
1321      }

android.media.MediaRecorder.start

  • Added in API level 1
1323      /**
1324       * Begins capturing and encoding data to the file specified with
1325       * setOutputFile(). Call this after prepare().
1326       *
1327       * <p>Since API level 13, if applications set a camera via
1328       * {@link #setCamera(Camera)}, the apps can use the camera after this method
1329       * call. The apps do not need to lock the camera again. However, if this
1330       * method fails, the apps should still lock the camera back. The apps should
1331       * not start another recording session during recording.
1332       *
1333       * @throws IllegalStateException if it is called before
1334       * prepare() or when the camera is already in use by another app.
1335       */
1336      public native void start() throws IllegalStateException;

android.media.MediaRecorder.stop

  • Added in API level 1
1338      /**
1339       * Stops recording. Call this after start(). Once recording is stopped,
1340       * you will have to configure it again as if it has just been constructed.
1341       * Note that a RuntimeException is intentionally thrown to the
1342       * application, if no valid audio/video data has been received when stop()
1343       * is called. This happens if stop() is called immediately after
1344       * start(). The failure lets the application take action accordingly to
1345       * clean up the output file (delete the output file, for instance), since
1346       * the output file is not properly constructed when this happens.
1347       *
1348       * @throws IllegalStateException if it is called before start()
1349       */
1350      public native void stop() throws IllegalStateException;

android.media.AudioRecord.getState 

  • Added in API level 3
1186      /**
1187       * Returns the state of the AudioRecord instance. This is useful after the
1188       * AudioRecord instance has been created to check if it was initialized
1189       * properly. This ensures that the appropriate hardware resources have been
1190       * acquired.
1191       * @see AudioRecord#STATE_INITIALIZED
1192       * @see AudioRecord#STATE_UNINITIALIZED
1193       */
1194      public int getState() {
1195          return mState;
1196      }

android.media.AudioRecord.startRecording

  • Added in API level 3/16
1339      //---------------------------------------------------------
1340      // Transport control methods
1341      //--------------------
1342      /**
1343       * Starts recording from the AudioRecord instance.
1344       * @throws IllegalStateException
1345       */
1346      public void startRecording()
1347      throws IllegalStateException {
1348          if (mState != STATE_INITIALIZED) {
1349              throw new IllegalStateException("startRecording() called on an "
1350                      + "uninitialized AudioRecord.");
1351          }
1352
1353          // start recording
1354          synchronized(mRecordingStateLock) {
1355              if (native_start(MediaSyncEvent.SYNC_EVENT_NONE, 0) == SUCCESS) {
1356                  handleFullVolumeRec(true);
1357                  mRecordingState = RECORDSTATE_RECORDING;
1358              }
1359          }
1360      }
1362      /**
1363       * Starts recording from the AudioRecord instance when the specified synchronization event
1364       * occurs on the specified audio session.
1365       * @throws IllegalStateException
1366       * @param syncEvent event that triggers the capture.
1367       * @see MediaSyncEvent
1368       */
1369      public void startRecording(MediaSyncEvent syncEvent)
1370      throws IllegalStateException {
1371          if (mState != STATE_INITIALIZED) {
1372              throw new IllegalStateException("startRecording() called on an "
1373                      + "uninitialized AudioRecord.");
1374          }
1375
1376          // start recording
1377          synchronized(mRecordingStateLock) {
1378              if (native_start(syncEvent.getType(), syncEvent.getAudioSessionId()) == SUCCESS) {
1379                  handleFullVolumeRec(true);
1380                  mRecordingState = RECORDSTATE_RECORDING;
1381              }
1382          }
1383      }

android.media.AudioRecord.stop

  • Added in API level 3
1385      /**
1386       * Stops recording.
1387       * @throws IllegalStateException
1388       */
1389      public void stop()
1390      throws IllegalStateException {
1391          if (mState != STATE_INITIALIZED) {
1392              throw new IllegalStateException("stop() called on an uninitialized AudioRecord.");
1393          }
1394
1395          // stop recording
1396          synchronized(mRecordingStateLock) {
1397              handleFullVolumeRec(false);
1398              native_stop();
1399              mRecordingState = RECORDSTATE_STOPPED;
1400          }
1401      }

android.media.AudioRecord.read

  • Added in API level 3/23
1417      //---------------------------------------------------------
1418      // Audio data supply
1419      //--------------------
1420      /**
1421       * Reads audio data from the audio hardware for recording into a byte array.
1422       * The format specified in the AudioRecord constructor should be
1423       * {@link AudioFormat#ENCODING_PCM_8BIT} to correspond to the data in the array.
1424       * @param audioData the array to which the recorded audio data is written.
1425       * @param offsetInBytes index in audioData from which the data is written expressed in bytes.
1426       * @param sizeInBytes the number of requested bytes.
1427       * @return zero or the positive number of bytes that were read, or one of the following
1428       *    error codes. The number of bytes will not exceed sizeInBytes.
1429       * <ul>
1430       * <li>{@link #ERROR_INVALID_OPERATION} if the object isn't properly initialized</li>
1431       * <li>{@link #ERROR_BAD_VALUE} if the parameters don't resolve to valid data and indexes</li>
1432       * <li>{@link #ERROR_DEAD_OBJECT} if the object is not valid anymore and
1433       *    needs to be recreated. The dead object error code is not returned if some data was
1434       *    successfully transferred. In this case, the error is returned at the next read()</li>
1435       * <li>{@link #ERROR} in case of other error</li>
1436       * </ul>
1437       */
1438      public int read(@NonNull byte[] audioData, int offsetInBytes, int sizeInBytes) {
1439          return read(audioData, offsetInBytes, sizeInBytes, READ_BLOCKING);
1440      }

android.media.ImageReader.acquireLatestImage

474      /**
475       * <p>
476       * Acquire the latest {@link Image} from the ImageReader's queue, dropping older
477       * {@link Image images}. Returns {@code null} if no new image is available.
478       * </p>
479       * <p>
480       * This operation will acquire all the images possible from the ImageReader,
481       * but {@link #close} all images that aren't the latest. This function is
482       * recommended to use over {@link #acquireNextImage} for most use-cases, as it's
483       * more suited for real-time processing.
484       * </p>
485       * <p>
486       * Note that {@link #getMaxImages maxImages} should be at least 2 for
487       * {@link #acquireLatestImage} to be any different than {@link #acquireNextImage} -
488       * discarding all-but-the-newest {@link Image} requires temporarily acquiring two
489       * {@link Image Images} at once. Or more generally, calling {@link #acquireLatestImage}
490       * with less than two images of margin, that is
491       * {@code (maxImages - currentAcquiredImages < 2)} will not discard as expected.
492       * </p>
493       * <p>
494       * This operation will fail by throwing an {@link IllegalStateException} if
495       * {@code maxImages} have been acquired with {@link #acquireLatestImage} or
496       * {@link #acquireNextImage}. In particular a sequence of {@link #acquireLatestImage}
497       * calls greater than {@link #getMaxImages} without calling {@link Image#close} in-between
498       * will exhaust the underlying queue. At such a time, {@link IllegalStateException}
499       * will be thrown until more images are
500       * released with {@link Image#close}.
501       * </p>
502       *
503       * @return latest frame of image data, or {@code null} if no image data is available.
504       * @throws IllegalStateException if too many images are currently acquired
505       */
506      public Image acquireLatestImage() {
507          Image image = acquireNextImage();
508          if (image == null) {
509              return null;
510          }
511          try {
512              for (;;) {
513                  Image next = acquireNextImageNoThrowISE();
514                  if (next == null) {
515                      Image result = image;
516                      image = null;
517                      return result;
518                  }
519                  image.close();
520                  image = next;
521              }
522          } finally {
523              if (image != null) {
524                  image.close();
525              }
526              if (mParent != null) {
527                  mParent.flushOther(this);
528              }
529          }
530      }

android.media.projection.MediaProjection.createVirtualDisplay

101      /**
102       * @hide
103       */
104      public VirtualDisplay createVirtualDisplay(@NonNull String name,
105              int width, int height, int dpi, boolean isSecure, @Nullable Surface surface,
106              @Nullable VirtualDisplay.Callback callback, @Nullable Handler handler) {
107          int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
108                  | DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION;
109          if (isSecure) {
110              flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
111          }
112          final VirtualDisplayConfig.Builder builder = new VirtualDisplayConfig.Builder(name, width,
113                  height, dpi).setFlags(flags);
114          if (surface != null) {
115              builder.setSurface(surface);
116          }
117          VirtualDisplay virtualDisplay = createVirtualDisplay(builder, callback, handler);
118          return virtualDisplay;
119      }

android.media.projection.MediaProjection.registerCallback 

69      /** Register a listener to receive notifications about when the {@link
70       * MediaProjection} changes state.
71       *
72       * @param callback The callback to call.
73       * @param handler The handler on which the callback should be invoked, or
74       * null if the callback should be invoked on the calling thread's looper.
75       *
76       * @see #unregisterCallback
77       */
78      public void registerCallback(Callback callback, Handler handler) {
79          if (callback == null) {
80              throw new IllegalArgumentException("callback should not be null");
81          }
82          if (handler == null) {
83              handler = new Handler();
84          }
85          mCallbacks.put(callback, new CallbackRecord(callback, handler));
86      }

4 示例


5 adb命令

adb shell media


6 参考资料

音频和视频  |  Android 开发者  |  Android Developers

MediaRecorder 概览  |  Android 开发者  |  Android Developers

MediaPlayer 概览  |  Android 开发者  |  Android Developers

Android 录音实现(MediaRecorder) - 简书

Android 录音实现(AudioRecord) - 简书

Android 实现录音功能_AfinalStone的专栏-CSDN博客_android 录音


本地录音 权限(android.permission.RECORD_AUDIO)

Manifest.permission  |  Android Developers

AndroidManifest.xml - OpenGrok cross reference for /frameworks/base/core/res/AndroidManifest.xml


本地录音 接口(MediaRecorder方案)

  • android.media.MediaRecorder.start()
  • android.media.MediaRecorder.stop()

http://androidxref.com/6.0.1_r10/xref/frameworks/base/media/java/android/media/MediaRecorder.java


本地录音 接口(AudioRecord方案)

  • android.media.AudioRecord.startRecording()
  • android.media.AudioRecord.stop()

http://androidxref.com/6.0.1_r10/xref/frameworks/base/media/java/android/media/AudioRecord.java


示例代码(MediaRecorder)

package com.learn_android.media_recorder;import androidx.appcompat.app.AppCompatActivity;import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;import java.io.IOException;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final String TAG = "MainActivity";private static String fileName = null;private MediaRecorder mediaRecorder = null;private Button start_record = null;private Button stop_record = null;private MediaPlayer mediaPlayer = null;private Button start_play = null;private Button stop_play = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fileName = getFilesDir().getPath();fileName += "/audiorecordtest.3gp";start_record = (Button) findViewById(R.id.start_record);stop_record = (Button) findViewById(R.id.stop_record);start_play = (Button) findViewById(R.id.start_play);stop_play = (Button) findViewById(R.id.stop_play);start_record.setOnClickListener(this);stop_record.setOnClickListener(this);start_play.setOnClickListener(this);stop_play.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.start_record:Log.i(TAG, "onClick: start record");startRecord();break;case R.id.stop_record:Log.i(TAG, "onClick: stop record");stopRecord();break;case R.id.start_play:Log.i(TAG, "onClick: start play");startPlay();break;case R.id.stop_play:Log.i(TAG, "onClick: stop play");stopPlay();break;default:break;}}public void startRecord() {mediaRecorder = new MediaRecorder();try {mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);mediaRecorder.setOutputFile(fileName);mediaRecorder.prepare();mediaRecorder.start();} catch (IOException e) {e.printStackTrace();}}public void stopRecord() {mediaRecorder.stop();mediaRecorder.release();}public void startPlay() {mediaPlayer = new MediaPlayer();try {mediaPlayer.setDataSource(fileName);mediaPlayer.prepare();mediaPlayer.start();} catch (IOException e) {e.printStackTrace();}}public void stopPlay() {mediaPlayer.release();mediaPlayer = null;}
}

Android-系统服务-MediaRecorder相关推荐

  1. 我的Android进阶之旅------gt;Android中MediaRecorder.stop()报错 java.lang.RuntimeException: stop failed....

    今天在调用MediaRecorder.stop(),报错了,java.lang.RuntimeException: stop failed. E/AndroidRuntime(7698): Cause ...

  2. Android 用MediaRecorder录制视频太短崩的问题

    具体表现: 调用MediaRecorder的start()与stop()间隔不能小于1秒(有时候大于1秒也崩),否则必崩. 错误信息: java.lang.RuntimeException: stop ...

  3. Android之mediarecorder中的方法以及工作流程的过程

    嵌套.关联的类 class MediaRecorder.AudioEncoder 定义音频编码 class MediaRecorder.AudioSource 定义声音资源 interface Med ...

  4. Android系统服务-WindowManager

    WindowManager是Android中一个重要的服务 (Service ).WindowManager Service 是全局的,是唯一的.它将用户的操作,翻译成为指令,发送给呈现在界面上的各个 ...

  5. android访问网络提示 服务不可用,Android系统服务不可用

    我打算从我的网站获取数据,然后将该信息放入微调器中.我用一些教程,我想出了这个代码Android系统服务不可用 package com.thenewboston.christian; import j ...

  6. Android使用MediaRecorder和Camera实现视频录制及播放功能整理

    转载请注明出处:http://blog.csdn.net/woshizisezise/article/details/51878566 这两天产品经理向我丢来一个新需求,需要在项目里添加一个视频录制的 ...

  7. Android开发 系统服务,android 系统服务 开发

    <Android系统服务开发>分析了安卓提供的硬件控制机制.编写团队目前均从事相关工作,直接对平台源代码及日志进行分析及测试,介绍了目前尚未普及的安卓平台的硬件控制基本原理及实际框架的劋作 ...

  8. Android 系统服务 - PMS 的启动过程

    相关文章链接: 1. Android Framework - 学习启动篇 2. Android 系统服务 - PMS 的启动过程 相关源码文件: frameworks/base/services/co ...

  9. Android系统服务-简介

    Introduction 我们知道Android系统服务挺多的,做程序时经常会用到,要想把这么多的服务都了解透彻还真不是一两天就能搞定的,首先我们得又有一个框架,脑海里要形成这样的模型,android ...

  10. android利用MediaRecorder实现录音功能

    android用手机录音保存到sd卡中: 布局文件: <?xml version="1.0" encoding="utf-8"?> <Line ...

最新文章

  1. 移动端适配方案 flexible.js
  2. Xcode模拟器相关操作
  3. 《研磨设计模式》chap20 享元模式 Flyweight (2)模式介绍
  4. 基于Neutron的Kubernetes SDN实践经验之谈
  5. java高位转低位注意事项,int转 short/byte溢出过程
  6. 程序型语言VS.编译型语言
  7. java 枚举的实现原理
  8. 总结前端常用控件和疑难杂症的解决方法
  9. 人性歪曲的心理调适 一【浮躁心理、偏激心理、自卑心理、自杀心理、愤怒心理】...
  10. SEO自动外链工具推荐:站群推广利器SEO,在线批量发外链让新站快速收录
  11. linux编译安卓源码,Ubuntu下编译Android源码
  12. HDR电视显示技术概况及标准发展前景汇总
  13. http://mybatis.org/dtd/mybatis-3-mapper.dtd 报红
  14. 2011推荐系统论坛游记:爱的反义词不是恨
  15. 微信公众帐号开发教程
  16. 目标检测网络之三叉戟TridentNet
  17. 低代码与BPM有什么区别?
  18. FTP主动模式和被动模式!
  19. 洛谷P4961 小埋与扫雷
  20. 吉林大学软院夏令营面试

热门文章

  1. 2017第三届美亚杯全国电子数据取证大赛个人赛write up
  2. 甘肃阿克塞百余只“岩壁精灵”雪中觅食
  3. 电脑公司特供版 GHOSTXPSP3_2013新春特别版
  4. 12v电量显示制作方法_制作12V电池电压表
  5. 日常计算机网络英语对话,日常话题英语口语
  6. python爬虫——爬取图书馆借阅数据
  7. js高德地图获取道路信息
  8. 浙江工业大学 计算机考研难吗,浙江工业大学考研难吗
  9. 魔兽世界美服部落人数最多服务器,美服部落转联盟服务正式开启 规模继续扩大...
  10. 一本大学计算机专业最新排名,中国校友会网2018中国大学计算机类各本科专业排行榜...