Media Recorder 示例应用程序崩溃 - 为啥会这样?

Posted

技术标签:

【中文标题】Media Recorder 示例应用程序崩溃 - 为啥会这样?【英文标题】:Media Recorder example app crashes - why so?Media Recorder 示例应用程序崩溃 - 为什么会这样? 【发布时间】:2019-12-03 22:56:44 【问题描述】:

我尝试运行一个示例以在 android 中使用相机进行录制,但它崩溃了,并且控制台中显示的错误没有给我一个验证错误来帮助我了解发生了什么。 我将向您展示代码,因此如果有人可以帮助我理解应用程序崩溃的原因。 在 activity_main.xml 中只有 2 个按钮调用函数开始和停止录制。

import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity 
    MediaRecorder recorder;
    File audiofile = null;
    static final String TAG = "MediaRecording";
    Button startButton,stopButton;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        isStoragePermissionGranted();
        startButton = (Button) findViewById(R.id.button1);
        stopButton = (Button) findViewById(R.id.button2);
    

    public  boolean isStoragePermissionGranted() 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) 
            Log.v(TAG,"Permission is granted");
         else 

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, 1);
        
    
    else  //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
    

    public void startRecording(View view) throws IOException 
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
        //Creating file  
        File dir = Environment.getExternalStorageDirectory();
        try 
            audiofile = File.createTempFile("sound", ".3gp", dir);
         catch (IOException e) 
            Log.e(TAG, "external storage access error");
            return;
        
        //Creating MediaRecorder and specifying audio source, output format, encoder & output format  
        recorder = new MediaRecorder();
        recorder.setAudiosource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(audiofile.getAbsolutePath());
        recorder.prepare();
        recorder.start();
    

    public void stopRecording(View view) 
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        //stopping recorder  
        recorder.stop();
        recorder.release();
        //after stopping the recorder, create the sound file and add it to media library.  
        addRecordingToMediaLibrary();
    

    protected void addRecordingToMediaLibrary() 
        //creating content values of size 4  
        ContentValues values = new ContentValues(4);
        long current = System.currentTimeMillis();
        values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
        values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
        values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
        values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());

        //creating content resolver and storing it in the external content uri  
        ContentResolver contentResolver = getContentResolver();
        Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Uri newUri = contentResolver.insert(base, values);

        //sending broadcast message to scan the media file so that it can be available  
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
        Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
    

我在 Logcat 中添加了错误,以帮助您

07-25 17:51:16.244 20019-20019/com.example.decoding E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.decoding, PID: 20019
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.view.View$DeclaredOnClickListener.onClick(View.java:4740)
        at android.view.View.performClick(View.java:5697)
        at android.widget.TextView.performClick(TextView.java:10826)
        at android.view.View$PerformClick.run(View.java:22526)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7224)
        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)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.view.View$DeclaredOnClickListener.onClick(View.java:4735)
        at android.view.View.performClick(View.java:5697) 
        at android.widget.TextView.performClick(TextView.java:10826) 
        at android.view.View$PerformClick.run(View.java:22526) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        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) 
     Caused by: java.lang.RuntimeException: setAudioSource failed.
        at android.media.MediaRecorder._setAudioSource(Native Method)
        at android.media.MediaRecorder.setAudioSource(MediaRecorder.java:488)
        at com.example.decoding.MainActivity.startRecording(MainActivity.java:46)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.view.View$DeclaredOnClickListener.onClick(View.java:4735) 
        at android.view.View.performClick(View.java:5697) 
        at android.widget.TextView.performClick(TextView.java:10826) 
        at android.view.View$PerformClick.run(View.java:22526) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        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) 
    ```

【问题讨论】:

如果应用程序崩溃,您应该在 Android Studio 的 Logcat 窗格中显示堆栈跟踪。请尝试找到它并添加到问题中。 我不知道在***问题中添加错误的正确方法,所以我决定使用``````,你可以在代码之后看到它。 您使用的是哪个 Android 版本和设备? 【参考方案1】:

打开你的 android manifest 并添加这一行:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

为了将来参考,这是您的 logcat 的相关部分:

Caused by: java.lang.RuntimeException: setAudioSource failed.
    at android.media.MediaRecorder._setAudioSource(Native Method)
    at android.media.MediaRecorder.setAudioSource(MediaRecorder.java:488)
    at com.example.decoding.MainActivity.startRecording(MainActivity.java:46)

【讨论】:

不,先生,我的清单已经有 【参考方案2】:

通过以编程方式请求许可来解决,即使是麦克风,这里是代码:

public void isStoragePermissionGranted() 
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) 
        
        // Else ask for permission
        else 
            ActivityCompat.requestPermissions(MainActivity.this, new String[]
                     Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO, 0);

        

【讨论】:

以上是关于Media Recorder 示例应用程序崩溃 - 为啥会这样?的主要内容,如果未能解决你的问题,请参考以下文章

Android Media Recorder 无法在 Google Glass 上录制长视频

获取音频文件的持续时间

使用 javax.microedition.media.Player 时应用程序崩溃并出现异常

Ionic2:iOS 应用程序在使用cordova-plugin-media 录制音频并再次播放时崩溃

这个堆栈跟踪表明了啥? iOS8 上由 Xamarin.Media.MediaPicker.TakePhotoAsync 引起的崩溃

使用 Recorder.js 在音频记录中“获取音频时出错”