我正在尝试从this SO answer实施解决方案,以便将视频录制到套接字(然后从播放器端的套接字读取它,最终目标是实时播放视频)
但是当试图调用madiarecorder的start()时会出现异常:start failed:-1010
API说明了这个常数:
public static final int MEDIA_ERROR_UNSUPPORTED
在API级别17中添加了比特流符合相关编码 标准或文件规范,但媒体框架不支持 特征
但我猜测如果人们检查这个答案是否有效,就会以某种方式支持 - 所以请
指着我,我的错是什么?
这是我正在使用的代码:
public class MainActivity extends Activity {
private Camera mCamera;
private MediaRecorder mMediaRecorder;
private Socket socket;
private Preview mPreView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
try {
socket = new Socket("10.0.0.4",8080);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// this is your network socket
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
mPreView=new Preview(this);
mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
mCamera.lock();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// this is the unofficially supported MPEG2TS format, suitable for
// streaming (Android 3.0+)
mMediaRecorder.setOutputFormat(8);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mMediaRecorder.setOutputFile(pfd.getFileDescriptor());
// mMediaRecorder.setPreviewDisplay(null);
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaRecorder.start();
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class Preview extends ViewGroup implements SurfaceHolder.Callback
{
SurfaceView mSurface;
SurfaceHolder mHolder;
public Preview(Context context) {
super(context);
// TODO Auto-generated constructor stub
mSurface=new SurfaceView(context);
addView(mSurface);
mHolder=mSurface.getHolder();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
}
}
}
为什么媒体录音机不支持它?我该如何解决?
谢谢你抽出时间。答案 0 :(得分:1)
我找到了解决方案:
我替换了这一行:
mMediaRecorder.setOutputFormat(8);
用这个:
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
答案 1 :(得分:0)
如果MediaRecorder启动失败,请务必检查介质记录器的设置并确保它们完全可用于您的硬件,因此我会检查以下内容:
mMediaRecorder.setVideoSource();
mMediaRecorder.setOutputFormat();
mMediaRecorder.setVideoEncoder();
等。
如果其中任何一个不正确或不可用于硬件,那么这就是MediaRecorder会让您知道的方式