我正在创建一个语音聊天应用程序,我必须从麦克风录制声音。当我开始录制它开始正常,但是当我停止录制时它第一次崩溃,第二次我录制声音并停止它工作正常并在我的外部存储器中创建音频文件是什么问题给予问题第一次。
ChatActivity.java
package com.example.usamaakmal.npproject;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.time.LocalDateTime;
import java.util.Timer;
public class ChatActivity extends AppCompatActivity {
ToggleButton recButton;
MediaRecorder recorder = null;
boolean recoredAlreadClicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
this.setTitle("Chat Room");
File dir = new File(Environment.getExternalStorageDirectory()+"/NPProject");
if (!dir.isDirectory()){
dir.mkdir();
} else {
deleteDir(dir);
}
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.send:
return true;
case R.id.play:
playRecording();
return true;
case R.id.record:
if (recoredAlreadClicked){
item.setIcon(R.drawable.ic_mic_black_24dp);
item.setTitle("Record");
Toast.makeText(ChatActivity.this, "Stopping Recording", Toast.LENGTH_SHORT).show();
try {
stopRec();
} catch (IOException e) {
e.printStackTrace();
}
recoredAlreadClicked = !recoredAlreadClicked;
return false;
}else{
item.setIcon(R.drawable.ic_home_black_24dp);
item.setTitle("Stop");
Toast.makeText(ChatActivity.this, "Starting Recording", Toast.LENGTH_SHORT).show();
startRec();
recoredAlreadClicked = !recoredAlreadClicked;
return true;
}
}
return false;
}
};
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
public void playRecording() {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/NPProject/audio.m4a");
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
private void startRec() {
Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();
if (recorder != null) {
recorder.release();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
recorder.setOutputFile(Environment.getExternalStorageDirectory()+"/NPProject/"+ SocketHelper.getName() +"-"+System.currentTimeMillis()+".m4a");
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
Log.e("giftlist", "io problems while preparing [" +
getFilesDir() + "]: " + e.getMessage());
}
}
private void stopRec() throws IOException {
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;
}
// Toast.makeText(ChatActivity.this, ""+convertAudioToByte().length, Toast.LENGTH_SHORT).show();
}
public byte[] convertAudioToByte() throws IOException {
FileInputStream inputStream = new FileInputStream(Environment.getExternalStorageDirectory()+"/NPProject/audio.m4a");
FileChannel fc = inputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int)fc.size());
fc.read(buffer);
return buffer.array();
}
public void convertByteToAudio(byte[] b) throws IOException {
FileOutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory()+ "/NPProject/audio.m4a");
outputStream.write(b);
outputStream.flush();
outputStream.close();
}
}
这是应用程序崩溃时我获得的活动的堆栈跟踪
E/MediaRecorder: stop called in an invalid state: 4
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.usamaakmal.npproject, PID: 12556
java.lang.IllegalStateException
at android.media.MediaRecorder.native_stop(Native Method)
at android.media.MediaRecorder.stop(MediaRecorder.java:1244)
at com.example.usamaakmal.npproject.ChatActivity.stopRec(ChatActivity.java:138)
at com.example.usamaakmal.npproject.ChatActivity.access$000(ChatActivity.java:31)
at com.example.usamaakmal.npproject.ChatActivity$1.onNavigationItemSelected(ChatActivity.java:71)
at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:182)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:95)
at android.view.View.performClick(View.java:5646)
at android.view.View$PerformClick.run(View.java:22459)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
答案 0 :(得分:-1)
我通过授予应用程序在设置中录制麦克风来解决此问题,我没有添加运行时权限,因此这就是问题。