gpt4 book ai didi

android - Android-FATAL EXCEPTION:主要,无法启动 Activity ,setAudioSource

转载 作者:行者123 更新时间:2023-12-03 01:44:56 26 4
gpt4 key购买 nike

我只是在尝试进行基本的音频记录和播放 Activity ,但始终收到此错误。

这是代码:

package com.example.wesle.noisemachine;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.widget.Toast;
import java.io.IOException;

public class ReceiveScreen extends AppCompatActivity {

private Button buttonStart, buttonStop, buttonPlay;
private MediaRecorder mediaRecorder = new MediaRecorder();
private String outputFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_screen);

buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonStop.setEnabled(false);
buttonPlay.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outputFile);

buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException ise) {
System.out.println("ISE Catch");
} catch (IOException ioe) {
System.out.println("ISE Catch");

}
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);


Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();


}
});

buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

mediaRecorder.stop();
mediaRecorder.release();

buttonStop.setEnabled(false);
buttonPlay.setEnabled(true);
buttonStart.setEnabled(true);

Toast.makeText(getApplicationContext(), "Recording Completed", Toast.LENGTH_LONG).show();
}
});

buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MediaPlayer mediaPlayer = new MediaPlayer();

try {
mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
System.out.println("E Catch");
}
Toast.makeText(getApplicationContext(), "Recording Playing", Toast.LENGTH_LONG).show();
}
});

//Code for the back button
Button backbuttonR = (Button) findViewById(R.id.backbuttonR);
backbuttonR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ReceiveScreen.this, MainActivity.class));
}
});
}
}

这是错误:

FATAL EXCEPTION: main Process: com.example.wesle.mathsucks, PID: 4255 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wesle.mathsucks/com.example.wesle.noisemachine.ReceiveScreen}: java.lang.RuntimeException: setAudioSource failed. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.RuntimeException: setAudioSource failed. at android.media.MediaRecorder.setAudioSource(Native Method) at com.example.wesle.noisemachine.ReceiveScreen.onCreate(ReceiveScreen.java:35) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)



和 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wesle.noisemachine">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.example.wesle.noisemachine.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.wesle.noisemachine.TransmitScreen" />
<activity android:name="com.example.wesle.noisemachine.ReceiveScreen"></activity>
</application>

</manifest>

最佳答案

mediaPlayer.setDataSource(String);

失败,请通过 refer to this获取更多信息。

因此您可以改用FileDescriptor

创建一个方法
private FileDescriptor openFile(String path)
throws FileNotFoundException, IOException {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
return fos.getFD();
}

然后打电话
 try {
mediaPlayer.setDataSource(openFile(outputFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

关于android - Android-FATAL EXCEPTION:主要,无法启动 Activity ,setAudioSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44485145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com