我正在尝试将视频从我的 android 应用程序录制到我的台式机(笔记本电脑)。我使用 Socket 编程来做到这一点,在我的 android 代码中 Parcel File Descriptor 抛出空指针异常并且应用程序崩溃了。任何人帮助我。
private void SetupMediaRecorder ()throws IOException{
task t = new task();
Log.e("TCP Client", "C: MediaRecorder...");
pfd = ParcelFileDescriptor.fromSocket(t.m_socket);
Log.e("TCP Client", "C: SET PATH...");
mMediaRecoder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecoder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecoder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecoder.setOutputFile(pfd.getFileDescriptor());
mMediaRecoder.setVideoEncodingBitRate(40);
mMediaRecoder.setVideoFrameRate(4);
mMediaRecoder.setVideoSize(mVideoSize.getWidth(),mVideoSize.getHeight());
mMediaRecoder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecoder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecoder.setOrientationHint(mTotalRotation);
mMediaRecoder.prepare();
}
public class task extends AsyncTask {
Socket m_socket = null;
@Override
protected Object doInBackground(Object[] params) {
try {
Log.e("TCP Client", "C: Connecting...");
m_socket = new Socket ("192.168.1.5",31001);
Log.e("TCP Client", "PFD...");
Log.e("TCP Client", "C: OUT ASYNC...");
pfd = ParcelFileDescriptor.fromSocket(m_socket);
Log.e("TCP Client", "C: PFD initialize...");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
这是异常详细信息。
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp E/TCP Client: C: MediaRecorder...
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileDescriptor java.net.Socket.getFileDescriptor$()' on a null object reference
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at android.os.ParcelFileDescriptor.fromSocket(ParcelFileDescriptor.java:354)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at com.example.habibrehman.cameraappp.Storage.SetupMediaRecorder(Storage.java:542)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at com.example.habibrehman.cameraappp.Storage.StartRecording(Storage.java:366)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at com.example.habibrehman.cameraappp.Storage.CheckStoragePermission(Storage.java:511)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at com.example.habibrehman.cameraappp.Storage.access$1100(Storage.java:84)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at com.example.habibrehman.cameraappp.Storage$3.onClick(Storage.java:240)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at android.view.View.performClick(View.java:5716)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at android.view.View$PerformClick.run(View.java:22596)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at android.os.Looper.loop(Looper.java:148)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7325)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
10-26 00:09:19.921 15696-15696/com.example.habibrehman.cameraappp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
由于上述异常,MediaRecorder 进入无效状态。
10-26 00:09:19.931 15696-15696/com.example.habibrehman.cameraappp E/MediaRecorder: start called in an invalid state: 1
10-26 00:09:19.931 15696-15696/com.example.habibrehman.cameraappp D/AndroidRuntime: Shutting down VM
10-26 00:09:19.931 15696-15696/com.example.habibrehman.cameraappp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.habibrehman.cameraappp, PID: 15696
java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:938)
at com.example.habibrehman.cameraappp.Storage.CheckStoragePermission(Storage.java:512)
at com.example.habibrehman.cameraappp.Storage.access$1100(Storage.java:84)
at com.example.habibrehman.cameraappp.Storage$3.onClick(Storage.java:240)
at android.view.View.performClick(View.java:5716)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-26 00:11:53.821 15696-15702/com.example.habibrehman.cameraappp W/art: Suspending all threads took: 9.171ms