How to specify OUTPUT_FORMAT_MPEG2TS on an Android device

I'm confused, why can't I use OutputFormat.OUTPUT_FORMAT_MPEG2TS as a parameter to call the MediaRecorder.setOutputFormat method on a device with Android version 2.3.6?

The android source has this bit of code:

 /** * 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; }; 

The testing device is a note from a Samsung galaxy showing version 2.3.6 for Android.

The request MediaRecorder.setAudioSource (7) does not throw an error even if it is also hidden (MediaRecorder.AudioSource.VOICE_COMMUNICATION == 7).

MediaRecorder.setOutputFormat (8) throws an exception with this log output:

  03-21 17:45:27.330: E/MediaRecorder(30622): setOutputFormat failed: -2147483648 

Here is the code that fails when calling 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 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(); } } 
+4
source share
1 answer

In practice, it all depends on the hardware and software implementation on a particular device.

A Samsung Galaxy Note running Android 4.0 can output an MPEG-TS stream to a file or socket handle.

0
source

Source: https://habr.com/ru/post/1402695/


All Articles