我很困惑为什么我不能使用OutputFormat.OUTPUT_FORMAT_MPEG2TS作为Android版本2.3.6设备上MediaRecorder.setOutputFormat
方法调用的参数?
在android源代码中有这个代码位:
/**
* Defines the output format. These constants are used with
* {@link MediaRecorder#setOutputFormat(int)}.
*/
public final class OutputFormat {
/* Do not change these values without updating their counterparts
* in include/media/mediarecorder.h!
*/
private OutputFormat() {}
public static final int DEFAULT = 0;
/** 3GPP media file format*/
public static final int THREE_GPP = 1;
/** MPEG4 media file format*/
public static final int MPEG_4 = 2;
/** The following formats are audio only .aac or .amr formats **/
/** @deprecated Deprecated in favor of AMR_NB */
/** Deprecated in favor of MediaRecorder.OutputFormat.AMR_NB */
/** AMR NB file format */
public static final int RAW_AMR = 3;
/** AMR NB file format */
public static final int AMR_NB = 3;
/** AMR WB file format */
public static final int AMR_WB = 4;
/** @hide AAC ADIF file format */
public static final int AAC_ADIF = 5;
/** @hide AAC ADTS file format */
public static final int AAC_ADTS = 6;
/** @hide Stream over a socket, limited to a single stream */
public static final int OUTPUT_FORMAT_RTP_AVP = 7;
/** @hide H.264/AAC data encapsulated in MPEG2/TS */
public static final int OUTPUT_FORMAT_MPEG2TS = 8;
};
测试设备是三星galaxy note,显示android版本2.3.6。
MediaRecorder.setAudioSource(7)调用不会引发错误,即使它也是隐藏选项(MediaRecorder.AudioSource.VOICE_COMMUNICATION == 7)。
MediaRecorder.setOutputFormat(8)调用抛出异常whit这个日志输出:
03-21 17:45:27.330: E/MediaRecorder(30622): setOutputFormat failed: -2147483648
以下是MediaRecorder.setOutputFormat
来电时失败的代码:
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
public class CameraStreamer extends Activity implements OnClickListener, SurfaceHolder.Callback
{
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.main);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);
cameraView.setOnClickListener(this);
}
private void initRecorder() {
recorder.setAudioSource(7); //MediaRecorder.AudioSource.VOICE_COMMUNICATION); //MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(8); //MediaRecorder.OutputFormat.OUTPUT_FORMAT_MPEG2TS);
// for reference only
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
int width=320, height=240;
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setVideoSize(width, height);
recorder.setVideoFrameRate(25);
recorder.setVideoEncodingBitRate(cpHigh.videoBitRate);
recorder.setAudioEncodingBitRate(cpHigh.audioBitRate);
recorder.setAudioChannels(1);
recorder.setAudioSamplingRate(cpHigh.audioSampleRate);
// recorder.setProfile(cpHigh); // This sets format, framerate, size, bitrate, channels, sampling rate, encoders
recorder.setOutputFile("/sdcard/videocapture_example.ts");
recorder.setMaxDuration(50000); // 50 seconds
recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
public void onClick(View v) {
if (recording) {
recorder.stop();
recording = false;
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
} else {
recording = true;
recorder.start();
}
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder arg0) {
prepareRecorder();
}
public void surfaceDestroyed(SurfaceHolder arg0) {
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
finish();
}
}
答案 0 :(得分:0)
实际上,这一切都取决于特定设备上的硬件和软件实现。
运行android 4.0的三星galaxy note可以在文件或套接字描述符中输出MPEG-TS流。