Android Studio示例代码中的setAudioSource失败

时间:2016-05-20 16:15:24

标签: android android-studio audio

我是Android和Java的新手。

我在Windows 10上使用最新的Android Studio。

我只是创建了一个空活动并粘贴了

中的代码

https://developer.android.com/guide/topics/media/audio-capture.html

到我的MainActivity。

根据其评论,我还在清单中添加了uses-permission。

我已经阅读了所有Q& As,我相信清单是好的。

但是我一直在使用callstack在发布结束时使setAudioSource失败。

我的Nexus 5和Nexus 5虚拟设备的错误相同。

你能帮助我吗?谢谢!

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kyang.myapplication">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我的代码如下:

package com.example.kyang.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;

public class MainActivity extends Activity {

    private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;

    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;

    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    private void startRecording() {

            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);


        try {

            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            setOnClickListener(clicker);
        }
    }

    public MainActivity() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }


    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Override
    protected void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
                new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        0));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
                new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        0));
        setContentView(ll);
    }
}

Android Monitor Verbose模式下的输出:

05-20 09:34:18.552 15335-15335/com.example.kyang.myapplication I/Process: Sending signal. PID: 15335 SIG: 9
05-20 09:35:07.850 16251-16251/com.example.kyang.myapplication W/ActivityThread: Application com.example.kyang.myapplication is waiting for the debugger on port 8100...
05-20 09:35:07.854 16251-16251/com.example.kyang.myapplication I/System.out: Sending WAIT chunk
05-20 09:35:08.273 16251-16257/com.example.kyang.myapplication I/art: Debugger is active
05-20 09:35:08.468 16251-16251/com.example.kyang.myapplication I/System.out: Debugger has connected
05-20 09:35:08.468 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:08.669 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:08.872 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:09.086 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:09.287 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:09.488 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:09.690 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:09.891 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:10.092 16251-16251/com.example.kyang.myapplication I/System.out: waiting for debugger to settle...
05-20 09:35:10.293 16251-16251/com.example.kyang.myapplication I/System.out: debugger has settled (1377)
05-20 09:35:10.310 16251-16251/com.example.kyang.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.kyang.myapplication-2/lib/x86_64
05-20 09:35:10.571 16251-16251/com.example.kyang.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.kyang.myapplication-2/lib/x86_64
05-20 09:35:10.883 16251-16304/com.example.kyang.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                                 [ 05-20 09:35:10.893 16251:16251 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0x7fdfb5ffea40, tid 16251


                                                                                 [ 05-20 09:35:10.940 16251:16304 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0x7fdfb9ac6400, tid 16304
05-20 09:35:10.976 16251-16304/com.example.kyang.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
05-20 09:35:12.406 16251-16251/com.example.kyang.myapplication D/AndroidRuntime: Shutting down VM


                                                                                 --------- beginning of crash
05-20 09:35:12.410 16251-16251/com.example.kyang.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.kyang.myapplication, PID: 16251
                                                                                 java.lang.RuntimeException: setAudioSource failed.
                                                                                     at android.media.MediaRecorder.setAudioSource(Native Method)
                                                                                     at com.example.kyang.myapplication.MainActivity.startRecording(MainActivity.java:65)
                                                                                     at com.example.kyang.myapplication.MainActivity.onRecord(MainActivity.java:33)
                                                                                     at com.example.kyang.myapplication.MainActivity.access$000(MainActivity.java:20)
                                                                                     at com.example.kyang.myapplication.MainActivity$RecordButton$1.onClick(MainActivity.java:90)
                                                                                     at android.view.View.performClick(View.java:5198)
                                                                                     at android.view.View$PerformClick.run(View.java:21147)
                                                                                     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:5417)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:3)

是的!您可以从Activity或Fragment调用requestPermissions()。此示例适用于来自活动的调用

int YOUR_REQUEST_CODE = 200; // could be something else..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) //check if permission request is necessary
{
     ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.RECORD_AUDIO, android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, YOUR_REQUEST_CODE});
}

然后,如果授予了权限,您可以签入onRequestPermissionResult:

@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        if(requestCode == YOUR_REQUEST_CODE)
        {
            Log.i(TAG, "Permission granted");
            //do what you wanted to do
        }
    } else {
        Log.d(TAG, "Permission failed");
    }
}

对于良好实践,您应该检查是否已经授予了权限(以避免每次询问都会让用户烦恼..)

if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(getApplicationContext(),permissionString))

答案 1 :(得分:0)

最后我发现我需要去设置 - &gt; App-&gt; Permission-&gt;手动滑动麦克风和存储。为什么我没有发现任何提及此事。

有没有办法明确提示用户提供这些权限,而不是使用隐藏在手机深处的设置进行静默崩溃?