Код: Выделить всё
Мое приложение основано на записи аудиофайла с кнопкой, которая начинает запись, а при повторном нажатии запись прекращается. Он компилируется и запускается, но затем при нажатии кнопки происходит сбой, и в студии Android я получаю следующие ошибки:
Несколько корневых тегов в строке 22
Начало тега не закрывается в строке 22.
Несколько корневых тегов в строке 23.
Элемент верхнего уровня не завершается в строке 24.Неверное имя закрывающего тега в строке 26.
Неверное имя закрывающего тега в строке 27.
Неверное имя закрывающего тега в строке 31.< /p>
Элемент верхнего уровня не завершен в строке 32
Спасибо!
Код: Выделить всё
package com.example.transkript;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button recordButton;
private MediaRecorder recorder;
private boolean isRecording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recordButton = findViewById(R.id.recordButton);
recordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecording) {
stopRecording();
} else {
startRecording();
}
}
});
}
private void startRecording() {
// Create a new MediaRecorder instance
recorder = new MediaRecorder();
// Set the audio source to the microphone
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// Set the output format to 3GPP (media recorder will choose the appropriate audio codec)
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// Set the output file path
recorder.setOutputFile(getFilePath());
// Set the audio encoder to AMR NB for efficiency
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
// Prepare the recorder
recorder.prepare();
// Start recording
recorder.start();
// Update the UI to reflect the recording state
isRecording = true;
recordButton.setText("Stop Recording");
} catch (IOException e) {
// Handle exceptions
e.printStackTrace();
}
}
private void stopRecording() {
if (recorder != null) {
// Stop the recording
recorder.stop();
// Release the MediaRecorder object
recorder.release();
recorder = null;
// Update the UI to reflect the recording state
isRecording = false;
recordButton.setText("Record");
}
}
private String getFilePath() {
// Get the external storage directory
File externalStorageDir = Environment.getExternalStorageDirectory();
// Check if the external storage is available and writable
if (externalStorageDir == null || !externalStorageDir.canWrite()) {
Log.e("MainActivity", "External storage is not available or writable.");
return null;
}
// Create a new directory for your app's files
File appDirectory = new File(externalStorageDir, "MyAppAudioFiles");
if (!appDirectory.exists()) {
if (!appDirectory.mkdirs()) {
Log.e("MainActivity", "Failed to create directory for audio files.");
return null;
}
}
// Create a new file in the app's directory
File audioFile = new File(appDirectory, "myrecording.3gp");
// Check if the file can be created
if (!audioFile.exists()) {
try {
if (!audioFile.createNewFile()) {
Log.e("MainActivity", "Failed to create audio file.");
return null;
}
} catch (IOException e) {
Log.e("MainActivity", "Failed to create audio file.", e);
return null;
}
}
// Return the absolute path of the file
return audioFile.getAbsolutePath();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (recorder != null) {
recorder.release();
recorder = null;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... -androidma
Мобильная версия