在线情况
楼主
  • 头像
  • 级别
  • 徽章
  • 职务总版主
  • 声望+46
  • 积分17058
  • 经验1436058
  • 文章25379
  • 注册2003-01-20
[译]Android录制声音(一)Recorder
[P]再看Android SDK的录制声音的例子时看见文章比较短就翻译了一下,请大家提出宝贵意见:[/P][P] [/P][P]Android SDK \docs\guide\topics\media\audio-capture.html 的翻译[/P][P]音频录制[/P][P]Android多媒体框架支持对常见音频的录制和编码,如果硬件支持,可以使用 MediaRecorder APIs 来非常方便的编写音频程序。[/P][P]通过学习这篇文档,你可编写应用程序通过设备的麦克来录制声音,并且保持和播放声音。[/P][P]  注意:Android模拟器不支持录制声音的功能,必须有实际设备。[/P][P] [/P][P]运行录制声音程序[/P][P]   音频的录制要比音频或者视频的播放要复杂,但是相对也比较简单,课遵循以下步骤:[/P][P]1.      创建MediaRecorder实例: android.media.MediaRecorder[/P][P]2.      设置音频来源:MediaRecorder.setAudioSource();[/P][P]3.      设置输出音频格式:MediaRecorder.setOutputFormat();[/P][P]4.      设置输出文件名:MediaRecorder.setOutputFile();[/P][P]5.      设置输出编码格式:MediaRecorder.setAudioEncorder();[/P][P]6.      在开始录制之前调用MediaRecorder.prepare();[/P][P]7.      开始录制声音,调用:MediaRecorder.start();[/P][P]8.      停止录制声音,调用:MediaRecorder.stop();[/P][P]9.      当录制完声音的时候,调用MediaRecorder.release()来释放占用的相关资源[/P][P] [/P][P]例子:录制音频并播放[/P][P]下面的例子展示了怎样录制并播放声音[/P][P]这个APP需要添加两个权限[/P][P][COLOR=rgb(255, 0, 0)] <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.RECORD_AUDIO" />
[/COLOR][/P][P]/*
 * The application needs to have the permission to write to external storage
 * if the output file is written to the external storage, and also the
 * permission to record audio. These permissions must be set in the
 * application's AndroidManifest.xml file, with something like:
 *
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.RECORD_AUDIO" />
 *
 */
package com.android.audiorecordtest;

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;


public classAudioRecordTestextendsActivity
{
     private staticfinalString LOG_TAG="AudioRecordTest";
     private staticString mFileName=null;

     private RecordButton mRecordButton=null;
     private MediaRecorder mRecorder=null;

     private PlayButton   mPlayButton=null;
     private MediaPlayer   mPlayer=null;

     private void onRecord(boolean start){
         if (start){
             startRecording();
         } else{
             stopRecording();
         }
     }

     private void onPlay(boolean start){
         if (start){
             startPlaying();
         } else{
             stopPlaying();
         }
     }

     private void startPlaying(){
         mPlayer = newMediaPlayer();
         try {
             mPlayer.setDataSource(mFileName);
             mPlayer.prepare();
             mPlayer.start();
         } catch(IOException e){
             Log.e(LOG_TAG,"prepare() failed");
         }
     }

     private void stopPlaying(){
         mPlayer.release();
         mPlayer = null;
     }

     private void startRecording(){
         mRecorder = newMediaRecorder();
         mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
         mRecorder.setOutputFile(mFileName);
         mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

         try {
             mRecorder.prepare();
         } catch(IOException e){
             Log.e(LOG_TAG,"prepare() failed");
         }

         mRecorder.start();
     }

     private void stopRecording(){
         mRecorder.stop();
         mRecorder.release();
         mRecorder = null;
     }

     class RecordButtonextendsButton{
         boolean mStartRecording=true;

         OnClickListener clicker=newOnClickListener(){
             public void onClick(View v){
                 onRecord(mStartRecording);
                 if (mStartRecording){
                     setText("Stop recording");
                 } else{
                     setText("Start recording");
                 }
                 mStartRecording =!mStartRecording;
             }
         };

         public RecordButton(Context ctx){
             super(ctx);
             setText("Start recording");
             setOnClickListener(clicker);
         }
     }

     class PlayButtonextendsButton{
         boolean mStartPlaying  = true;

         OnClickListener clicker=newOnClickListener(){
             public void onClick(View v){
                 onPlay(mStartPlaying);
                 if (mStartPlaying){
                     setText("Stop playing");
                 } else{
                     setText("Start playing");
                 }
                 mStartPlaying =!mStartPlaying;
             }
         };

         public PlayButton(Context ctx){
             super(ctx);
             setText("Start playing");
             setOnClickListener(clicker);
         }
     }

     public AudioRecordTest(){
         mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
         mFileName += "/audiorecordtest.3gp";
     }

     @Override
     public void onCreate(Bundle icicle){
         super.onCreate(icicle);

         LinearLayout ll  = newLinearLayout(this);
         mRecordButton =  new RecordButton(this);
         ll.addView(mRecordButton,
             new LinearLayout.LayoutParams(
                 ViewGroup.LayoutParams.WRAP_CONTENT,
                 ViewGroup.LayoutParams.WRAP_CONTENT,
                 0));
         mPlayButton = newPlayButton(this);
         ll.addView(mPlayButton,
             new LinearLayout.LayoutParams(
                 ViewGroup.LayoutParams.WRAP_CONTENT,
                 ViewGroup.LayoutParams.WRAP_CONTENT,
                 0));
         setContentView(ll);
     }

     @Override
     public void onPause(){
         super.onPause();
         if (mRecorder!=null){
             mRecorder.release();
             mRecorder =  null;
         }

         if (mPlayer!=null){
             mPlayer.release();
             mPlayer = null;
         }
     }
}[/P][P] [/P][P]这是SDK的中带的代码,我已经在自己的手机上成功运行没有任何错误![/P][P]
[/P][P][URL=http://blog.csdn.net/zqiang_55/article/details/6953682]http://blog.csdn.net/zqiang_55/article/details/6953682[/URL][/P]
LeadBBS 官方论坛(asp论坛)感谢您的参与
在线情况
2
  • 头像
  • 级别
  • 徽章
  • 职务总版主
  • 声望+46
  • 积分17058
  • 经验1436058
  • 文章25379
  • 注册2003-01-20

在上一篇的学习中发现,android在设定录音的编码时需要同时设置录音的输出格式,当时不知道怎样的编码对着怎样的格式,最近在看sdk时发现了一点

 

Table 1. Core media format and codec support.

 

TypeFormat / CodecEncoderDecoderDetailsSupported File Type(s) / Container Formats
AudioAAC LC/LTPMono/Stereo content in any combination of standard bit rates up to 160 kbps and sampling rates from 8 to 48kHz• 3GPP (.3gp)
• MPEG-4 (.mp4, .m4a)
• ADTS raw AAC (.aac, decode in Android 3.1+, encode in Android 4.0+, ADIF not supported)
• MPEG-TS (.ts, not seekable, Android 3.0+)
HE-AACv1 (AAC+) 
HE-AACv2 (enhanced AAC+) 
AMR-NB4.75 to 12.2 kbps sampled @ 8kHz  3GPP (.3gp)
AMR-WB9 rates from 6.60 kbit/s to 23.85 kbit/s sampled @ 16kHz  3GPP (.3gp)
FLAC 
(Android 3.1+)
Mono/Stereo (no multichannel). Sample rates up to 48 kHz (but up to 44.1 kHz is recommended on devices with 44.1 kHz output, as the 48 to 44.1 kHz downsampler does not include a low-pass filter). 16-bit recommended; no dither applied for 24-bit.  FLAC (.flac) only
MP3 Mono/Stereo 8-320Kbps constant (CBR) or variable bit-rate (VBR)  MP3 (.mp3)
MIDI MIDI Type 0 and 1. DLS Version 1 and 2. XMF and Mobile XMF. Support for ringtone formats RTTTL/RTX, OTA, and iMelody• Type 0 and 1 (.mid, .xmf, .mxmf)
• RTTTL/RTX (.rtttl, .rtx)
• OTA (.ota)
• iMelody (.imy)
Vorbis  • Ogg (.ogg)
• Matroska (.mkv, Android 4.0+)
PCM/WAVE 8- and 16-bit linear PCM (rates up to limit of hardware)  WAVE (.wav)
ImageJPEGBase+progressive  JPEG (.jpg)
GIF  GIF (.gif)
PNG PNG (.png)
BMP  BMP (.bmp)
WEBP
(Android 4.0+)

(Android 4.0+)
   WebP (.webp)
VideoH.263 • 3GPP (.3gp)
• MPEG-4 (.mp4)
H.264 AVC
(Android 3.0+)
Baseline Profile (BP)• 3GPP (.3gp)
• MPEG-4 (.mp4)
• MPEG-TS (.ts, AAC audio only, not seekable, Android 3.0+)
MPEG-4 SP  3GPP (.3gp)
VP8 
(Android 2.3.3+)
Streamable only in Android 4.0 and aboveWebM (.webm)
• Matroska (.mkv, Android 4.0+)

Video Encoding Recommendations

Table 2, below, lists examples of video encoding profiles and parameters that the Android media framework supports for playback. In addition to these encoding parameter recommendations, a device's availablevideo recording profiles can be used as a proxy for media playback capabilities. These profiles can be inspected using theCamcorderProfile class, which is available since API level 8.

Table 2. Examples of supported video encoding parameters.

 

 SD (Low quality)SD (High quality)HD (Not available on all devices)
Video codecH.264 Baseline ProfileH.264 Baseline ProfileH.264 Baseline Profile
Video resolution176 x 144 px480 x 360 px1280 x 720 px
Video frame rate12 fps30 fps30 fps
Video bitrate56 Kbps500 Kbps2 Mbps
Audio codecAAC-LCAAC-LCAAC-LC
Audio channels1 (mono)2 (stereo)2 (stereo)
Audio bitrate24 Kbps128 Kbps192 Kbps

For video content that is streamed over HTTP or RTSP, there are additional requirements:

LeadBBS 官方论坛(asp论坛)感谢您的参与
Page created in 0.0078 seconds with 4 queries.