我正在开发一个Android Studio应用程序,并且一直工作到一定程度。我添加了MediaRecorder
来记录音频,并编写了用于记录和停止记录的代码。当我尝试测试代码时,所有内容都会正确编译并下载。但是,当应用程序打开时,它将立即关闭。检查logcat时,似乎没有明显的错误。
Logcat:
2019-06-24 15:11:19.302 30658-30658/? I/art: Late-enabling -Xcheck:jni
2019-06-24 15:11:19.495 30658-30658/com.example.visualizercopy W/System: ClassLoader referenced unknown path: /data/app/com.example.visualizercopy-1/lib/arm64
2019-06-24 15:11:19.515 30658-30658/com.example.visualizercopy I/InstantRun: starting instant run server: is main process
2019-06-24 15:11:19.732 30658-30658/com.example.visualizercopy I/Timeline: Timeline: Activity_launch_request intent: act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.google.android.packageinstaller time:351586171
2019-06-24 15:11:19.887 30658-30722/com.example.visualizercopy I/Adreno: QUALCOMM build : d3015e9, I25dc76dc3f
Build Date : 01/26/17
OpenGL ES Shader Compiler Version: XE031.09.00.03
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.BF64.1.2.3_RB1.07.00.00.258.011
Remote Branch : NONE
Reconstruct Branch : NOTHING
2019-06-24 15:11:19.895 30658-30722/com.example.visualizercopy I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-24 15:11:19.895 30658-30722/com.example.visualizercopy D/OpenGLRenderer: Swap behavior 1
2019-06-24 15:11:20.071 30658-30658/com.example.visualizercopy D/Editor: setInputTypeforClipTray(): 0
2019-06-24 15:11:20.157 30658-30658/com.example.visualizercopy D/Editor: setInputTypeforClipTray(): 0
2019-06-24 15:11:20.161 30658-30658/com.example.visualizercopy D/Editor: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
2019-06-24 15:11:20.336 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
2019-06-24 15:11:20.481 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.483 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.484 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
2019-06-24 15:11:20.523 30658-30658/com.example.visualizercopy D/Editor: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
2019-06-24 15:11:20.612 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2019-06-24 15:11:20.615 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
2019-06-24 15:11:20.615 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2019-06-24 15:11:20.620 30658-30658/com.example.visualizercopy W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
我试图在网上寻找解决问题的方法,但似乎没有一个奏效。我怎样才能解决这个问题? -编辑- 我添加的代码:
public class MainActivity extends Activity implements ESenseConnectionListener {
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private static String FILE_NAME_MIC="recording.3gp";
private MediaRecorder recorder;
private boolean permissionToRecordAccepted = false;
private String [] permissions = {Manifest.permission.RECORD_AUDIO};
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions , @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_RECORD_AUDIO_PERMISSION:
permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
break;
}
if (!permissionToRecordAccepted ) finish();
}
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(FILE_NAME_MIC);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Log.e("Record", "prepare() failed");
}
recorder.start();
}
private void stopRecording(){
recorder.stop();
recorder.release();
recorder = null;
}
等级:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.visualizercopy"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="io.esense">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<dist:module dist:instant="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
答案 0 :(得分:2)
您不能像这样将随机名称传递给MediaRecorder:
private static String FILE_NAME_MIC="recording.3gp";
FILE_NAME_MIC必须表示存储空间中的位置。
final static String FILE_NAME_MIC = Environment.getExternalStorageDirectory() + "/" + "MyAppDirectory"+ "/recording.mp3";
现在,我们知道我们想向/从我们的设备写入/读取内容,但是我们不能仅仅这样做。我们需要获得许可才能这样做。首先,您需要向清单中添加权限,例如:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
接下来,我们需要请求多个权限。该链接可以教您所有需要了解的内容以及更多信息:
结论:您的FILE_NAME_MIC和权限是“崩溃”的根源。
尝试并删除:
if (!permissionToRecordAccepted ) finish();
看看会发生什么。