我正在尝试在应用程序中实现相机。主要代码在一个片段中。调用时,出现此错误setVideoSoruce失败。 在其他问题中找不到答案。 ................................................... ........................
尝试更改权限并添加更多权限,但是没有用。
这是错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sozoapp, PID: 28002
java.lang.RuntimeException: setVideoSource failed.
at android.media.MediaRecorder._setVideoSource(Native Method)
at android.media.MediaRecorder.setVideoSource(MediaRecorder.java:750)
at com.example.sozoapp.Fragments.ThiefFragment.initRecorder(ThiefFragment.java:92)
at com.example.sozoapp.Fragments.ThiefFragment.onCreateView(ThiefFragment.java:56)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2612)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:874)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1228)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1293)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2066)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1856)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1811)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1717)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
I/Process: Sending signal. PID: 28002 SIG: 9
Application terminated.
片段:
package com.example.sozoapp.Fragments;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.fragment.app.Fragment;
import com.example.sozoapp.R;
import java.io.IOException;
import java.util.Random;
public class ThiefFragment extends Fragment implements View.OnClickListener, SurfaceHolder.Callback {
private Button btnStartRec;
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
private int randomNum;
private PlacesFragment.OnFragmentInteractionListener mListener;
public ThiefFragment(){
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_thief, container, false );
recorder = new MediaRecorder();
initRecorder();
btnStartRec = view.findViewById(R.id.btnCaptureVideo);
btnStartRec.setOnClickListener(this);
SurfaceView cameraView = view.findViewById(R.id.surfaceCamera);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);
cameraView.setOnClickListener(this);
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@SuppressLint({ "SdCardPath", "NewApi" })
private void initRecorder() {
Random random = new Random();
int max = 100000;
int min = 000001;
int range = max - min + 1;
randomNum = random.nextInt(range) + min + 1 - 10;
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
recorder.setOrientationHint(90);
} else {
recorder.setOrientationHint(180);
}
recorder.setOutputFile("/sdcard/MediaSozoVideos"+randomNum+ ".mp4");
}
private void prepareRecorder(){
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
prepareRecorder();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
if (recording){
recorder.stop();
recording = false;
}
recorder.release();
}catch (Exception e){
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnCaptureVideo:
try {
if (recording){
recorder.stop();
recording = false;
} else {
recording = true;
recorder.start();
}
}catch (Exception e){
}
default:
break;
}
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="@+id/surfaceCamera"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnCaptureVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Start Recording" />
</RelativeLayout>
清单文件权限:
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or
fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-feature android:name="android.hardware.camera2.full" />
<dist:module dist:instant="true" />
<uses-feature android:name="android.hardware.camera" />