MainActivity.java
user_3205012
java
a month ago
6.5 kB
1
Indexable
Never
//leduymanh package com.duymanh.voicerecorder; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.content.pm.PackageManager; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.io.File; import java.io.IOException; import java.util.Locale; public class MainActivity extends AppCompatActivity { private Button btnStart, btnStop, btnPlay; private MediaRecorder mediaRecorder; private MediaPlayer mediaPlayer; private String fileName; private TextView tvTimer; private Handler handler; private Runnable updateTimer; private long startTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = findViewById(R.id.btnStart); btnStop = findViewById(R.id.btnStop); btnPlay = findViewById(R.id.btnPlay); tvTimer = findViewById(R.id.tvTimer); handler = new Handler(); // Kiểm tra và yêu cầu quyền ghi âm và lưu trữ nếu chưa được cấp if (checkPermissions()) { initializeUI(); } else { requestPermissions(); } } private void initializeUI() { btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startRecording(); btnStart.setEnabled(false); btnStop.setEnabled(true); btnPlay.setEnabled(false); } }); btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stopRecording(); btnStart.setEnabled(true); btnStop.setEnabled(false); btnPlay.setEnabled(true); } }); btnPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { playRecording(); } }); } private void startRecording() { // Sử dụng thư mục riêng của ứng dụng để lưu trữ File outputDir = getExternalFilesDir(Environment.DIRECTORY_MUSIC); if (outputDir != null) { fileName = outputDir.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(fileName); try { mediaRecorder.prepare(); mediaRecorder.start(); startTimer(); Toast.makeText(this, "Bắt đầu ghi âm", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } private void stopRecording() { if (mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; stopTimer(); Toast.makeText(this, "Đã lưu ghi âm: " + fileName, Toast.LENGTH_SHORT).show(); } } private void playRecording() { mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(fileName); mediaPlayer.prepare(); mediaPlayer.start(); startTimer(); Toast.makeText(this, "Đang phát ghi âm", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { stopTimer(); Toast.makeText(MainActivity.this, "Đã phát xong", Toast.LENGTH_SHORT).show(); mediaPlayer.release(); mediaPlayer = null; } }); } private void startTimer() { startTime = System.currentTimeMillis(); updateTimer = new Runnable() { @Override public void run() { long elapsedTime = System.currentTimeMillis() - startTime; int seconds = (int) (elapsedTime / 1000); int minutes = seconds / 60; seconds = seconds % 60; tvTimer.setText(String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds)); handler.postDelayed(this, 1000); } }; handler.post(updateTimer); } private void stopTimer() { handler.removeCallbacks(updateTimer); tvTimer.setText("00:00"); } // Kiểm tra quyền private boolean checkPermissions() { return ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; } // Yêu cầu quyền private void requestPermissions() { ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }, 1000); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 1000) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initializeUI(); } else { Toast.makeText(this, "Bạn cần cấp quyền để sử dụng ứng dụng", Toast.LENGTH_SHORT).show(); } } } }
Leave a Comment