我目前正在尝试构建包含录音机的应用程序。在这样做的同时,我遇到了一个我无法解决的问题,即使我已经搜索了整个互联网并阅读了许多帖子。所以这就是事情。我创建了一个MediaRecorder实例。但是,每次我尝试通过OnClick运行.start()方法时,应用程序崩溃。我已经在Manifest文件中询问了权限,也通过main onCreate()方法手动询问了权限。然而,似乎它与此无关。我还看到很多主题告诉人们在.start()之前运行.prepare()方法。但是,在开始研究之前,我已经做到了这一点。因此,情况并非如此。我在下面添加了所有信息。
崩溃日志:
--------- beginning of crash
04-04 17:36:16.631 2605-2605/com.liucveikis.beatboxjam E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.liucveikis.beatboxjam, PID: 2605
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.liucveikis.beatboxjam.SoundRecorderActivity\$1.onClick(SoundRecorderActivity.java:56)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
04-04 17:36:16.632 1612-1983/system_process W/ActivityManager: Force finishing activity com.liucveikis.beatboxjam/.SoundRecorderActivity
清单包括:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
手动许可要求:
int YOUR_REQUEST_CODE = 200;
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
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);
}
}
录音活动:
package com.liucveikis.beatboxjam;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class SoundRecorderActivity extends AppCompatActivity {
private Button play, stop, record, save;
private MediaRecorder audioRecorder;
private String outputFile;
private boolean permissionToRecordAccepted = false;
private boolean permissionToWriteAccepted = false;
private String [] permissions = {"android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_recorder);
play = (Button) findViewById(R.id.playRecord);
stop = (Button) findViewById(R.id.stopRecording);
record = (Button) findViewById(R.id.startRecording);
save = (Button) findViewById(R.id.saveRecord);
play.setEnabled(false);
stop.setEnabled(false);
save.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
audioRecorder = new MediaRecorder();
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AAC_ADTS);
audioRecorder.setOutputFile(outputFile);
record.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
try {
audioRecorder.prepare();
audioRecorder.start();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
record.setEnabled(false);
stop.setEnabled(false);
Toast.makeText(getApplicationContext(), "Recording Started", Toast.LENGTH_LONG).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioRecorder.stop();
audioRecorder.release();
audioRecorder = null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recorded Successfully", Toast.LENGTH_LONG).show();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Record Is Playing", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
2017-04-05对录音活动的更改 - 它仍然无效。
package com.liucveikis.beatboxjam;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class SoundRecorderActivity extends AppCompatActivity {
private static final String LOG_TAG = "LALALALALALALA";
private Button play, stop, record, save;
private MediaRecorder audioRecorder;
private String outputFile;
private boolean permissionToRecordAccepted = false;
private boolean permissionToWriteAccepted = false;
private String [] permissions = {"android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_recorder);
play = (Button) findViewById(R.id.playRecord);
stop = (Button) findViewById(R.id.stopRecording);
record = (Button) findViewById(R.id.startRecording);
save = (Button) findViewById(R.id.saveRecord);
play.setEnabled(false);
stop.setEnabled(false);
save.setEnabled(false);
/* audioRecorder = new MediaRecorder();
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AAC_ADTS);
audioRecorder.setOutputFile(outputFile);*/
record.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
outputFile = getFilesDir().getAbsolutePath() + "/recording.3gp";
audioRecorder = new MediaRecorder();
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
audioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
audioRecorder.setOutputFile(outputFile);
try {
audioRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
audioRecorder.start();
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording Started", Toast.LENGTH_LONG).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioRecorder.stop();
audioRecorder.release();
audioRecorder = null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recorded Successfully", Toast.LENGTH_LONG).show();
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "Record Is Playing", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
-------------------编辑:2017-04-17 ------------------ -
好吧,所以我完全重写了Recorder课程。但是,现在我面临着一个不同的问题。这个东西在Android 6.0(API 23)和Android 7.0(API 24)上完美运行,但在Android 7.1.1(API 25)上崩溃。有没有关于MediaRecorder的一些变化?授予权限,无需担心。
package com.liucveikis.beatboxjam;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.IOException;
public class SoundRecorderActivity extends AppCompatActivity {
private Button startRecording, stopRecording, playRecord, saveRecord;
private MediaPlayer player;
private MediaRecorder recorder;
private String FILE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_recorder);
startRecording = (Button)findViewById(R.id.startRecording);
stopRecording = (Button)findViewById(R.id.stopRecording);
playRecord = (Button)findViewById(R.id.playRecord);
saveRecord = (Button)findViewById(R.id.saveRecord);
startRecording.setEnabled(true);
stopRecording.setEnabled(false);
playRecord.setEnabled(false);
saveRecord.setEnabled(false);
FILE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tempRecord.3gp";
startRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
startRecording();
stopRecording.setEnabled(true);
startRecording.setEnabled(false);
} catch (IOException e) {
e.printStackTrace();
}
}
});
stopRecording.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopRecording();
stopRecording.setEnabled(false);
startRecording.setEnabled(true);
playRecord.setEnabled(true);
}
});
playRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
startPlaying();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void startRecording() throws IOException {
if(recorder!=null){
recorder.stop();
recorder.release();
}
File fileOutput = new File(FILE);
if(fileOutput!=null){
fileOutput.delete();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(FILE);
recorder.prepare();
recorder.start();
}
public void stopRecording(){
recorder.stop();
recorder.release();
}
public void startPlaying() throws IOException {
if(player!=null){
player.stop();
player.release();
}
player = new MediaPlayer();
player.setDataSource(FILE);
player.prepare();
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
player.release();
}
});
}
}
错误:
04-12 19:46:24.911 1335-1335/? W/APM_AudioPolicyManager: getInputForAttr() failed opening input: samplingRate 8000, format 1, channelMask 10
04-12 19:46:24.911 1343-1343/? E/AudioRecord: Could not get audio input for session 73, record source 1, sample rate 8000, format 0x1, channel mask 0x10, flags 0
04-12 19:46:24.911 1343-1343/? E/StagefrightRecorder: audio source is not initialized
04-12 19:46:24.911 2592-2592/com.liucveikis.beatboxjam E/MediaRecorder: start failed: -2147483648
04-12 19:46:24.911 2592-2592/com.liucveikis.beatboxjam D/AndroidRuntime: Shutting down VM
--------- beginning of crash
04-12 19:46:24.912 2592-2592/com.liucveikis.beatboxjam E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.liucveikis.beatboxjam, PID: 2592
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.liucveikis.beatboxjam.SoundRecorderActivity.startRecording(SoundRecorderActivity.java:101)
at com.liucveikis.beatboxjam.SoundRecorderActivity$1.onClick(SoundRecorderActivity.java:45)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
04-12 19:46:24.914 1614-1680/system_process W/ActivityManager: Force finishing activity com.liucveikis.beatboxjam/.SoundRecorderActivity
答案 0 :(得分:-1)
在setContent视图后初始化您的媒体播放器。
MediaPlayer mediaplayer = new MediaPlayer();