0
マイクから音声を録音するボイスチャットアプリケーションを作成しています。録音を開始するとうまく始まりますが、録音を停止すると、録音が停止して2回目にクラッシュし、音を録音して停止し、外部記憶装置にオーディオファイルを作成します最初の問題です。メディアレコーダーが停止するとクラッシュする
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)
ログを完全にアップロードできますか – Bek
また、プロジェクトマニフェストを共有してください。 – ManmeetP
あなたは 'WRITE_EXTERNAL_STORAGE'パーミッションを要求しましたか? – nhoxbypass