when running the application for the first time it crashes (nothing on logcat). then you can see the requests for permissions and after you give the permission you can open the application and it works fine.
当第一次运行应用程序时它崩溃(logcat上没有任何东西)。然后你可以看到权限请求,并在你给予权限后,你可以打开应用程序,它工作正常。
public class MainActivity extends AppCompatActivity {
String [] PERMISSIONS = {Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
int PERMISSION_ALL = 1;
Button record;
Button play;
Button stop;
MediaPlayer mediaPlayer;
MediaRecorder mediaRecorder;
String outPutFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---------------------
//asking for permission
//---------------------
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
record = (Button) findViewById(R.id.recordbutton);
play = (Button) findViewById(R.id.playbutton);
stop = (Button) findViewById(R.id.stopbutton);
if (outPutFile==null){
play.setEnabled(false);
}
stop.setEnabled(false);
outPutFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);
//---------------------
//recording
//---------------------
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(),"error", Toast.LENGTH_LONG).show();
}
mediaRecorder.start();
record.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(getApplicationContext(),"recording", Toast.LENGTH_LONG).show();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(getApplicationContext(), "Audio recorded", Toast.LENGTH_LONG).show();
}
});
//---------------------
//playing the record
//---------------------
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outPutFile);
mediaPlayer.prepare();
Toast.makeText(getApplicationContext(), "Audio Playing", Toast.LENGTH_LONG).show();
} catch (Exception e) {
}
mediaPlayer.start();
record.setEnabled(true);
}
});
}
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
}
Why is that?
这是为什么?
logcat-
logcat-
07-30 19:03:03.812 4491-4491/com.example.tsuryohananov.voicerecorder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tsuryohananov.voicerecorder, PID: 4491
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tsuryohananov.voicerecorder/com.example.tsuryohananov.voicerecorder.MainActivity}: java.lang.RuntimeException: setAudioSource failed.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.RuntimeException: setAudioSource failed.
at android.media.MediaRecorder.setAudioSource(Native Method)
at com.example.tsuryohananov.voicerecorder.MainActivity.onCreate(MainActivity.java:64)
at android.app.Activity.performCreate(Activity.java:6980)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
1 个解决方案
#1
0
I moved that codes:
我移动了那些代码:
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);
to
至
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
record.setEnabled(false);
stop.setEnabled(true);
mediaRecorder.start();
Toast.makeText(getApplicationContext(),"recording", Toast.LENGTH_LONG).show();
}
});
problem was that at first running the mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
问题是,首先运行mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
were running before the user gave the permissions.
在用户授予权限之前运行。
I assume that there is other ways, maybe like:
我认为还有其他方法,可能像:
if (hasPermissions(this, PERMISSIONS)) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);
}
#1
0
I moved that codes:
我移动了那些代码:
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);
to
至
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
record.setEnabled(false);
stop.setEnabled(true);
mediaRecorder.start();
Toast.makeText(getApplicationContext(),"recording", Toast.LENGTH_LONG).show();
}
});
problem was that at first running the mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
问题是,首先运行mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
were running before the user gave the permissions.
在用户授予权限之前运行。
I assume that there is other ways, maybe like:
我认为还有其他方法,可能像:
if (hasPermissions(this, PERMISSIONS)) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outPutFile);
}