91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

android mediarecorder如何錄制音頻

小樊
81
2024-11-20 07:57:16
欄目: 編程語言

在Android中,使用MediaRecorder類來錄制音頻需要遵循以下步驟:

  1. 獲取必要的權限: 在AndroidManifest.xml文件中添加錄音權限:

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
  2. 檢查并請求權限(針對Android 6.0及以上版本): 在運行時檢查并請求錄音權限。

    private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
    
    private void requestRecordAudioPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO_PERMISSION);
        } else {
            startRecording();
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startRecording();
            } else {
                Toast.makeText(this, "Permission denied to record audio", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
  3. 配置MediaRecorder

    private MediaRecorder mediaRecorder;
    private String filePath;
    
    private void startRecording() {
        filePath = 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(filePath);
    
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            Toast.makeText(this, "Recording started", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
  4. 停止錄制

    private void stopRecording() {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
            Toast.makeText(this, "Recording stopped", Toast.LENGTH_SHORT).show();
        }
    }
    
  5. 處理權限請求結果: 在Activity中重寫onRequestPermissionsResult方法來處理權限請求結果。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button startButton = findViewById(R.id.start_button);
        Button stopButton = findViewById(R.id.stop_button);
    
        startButton.setOnClickListener(v -> requestRecordAudioPermission());
        stopButton.setOnClickListener(v -> stopRecording());
    }
    
  6. 處理權限請求結果: 在Activity中重寫onRequestPermissionsResult方法來處理權限請求結果。

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startRecording();
            } else {
                Toast.makeText(this, "Permission denied to record audio", Toast.LENGTH_SHORT).show();
            }
        }
    }
    

通過以上步驟,你可以在Android應用中使用MediaRecorder類錄制音頻。請確保在實際應用中處理所有可能的錯誤情況,并提供用戶友好的反饋。

0
湘西| 当涂县| 富宁县| 宜良县| 遵义县| 东乌| 洞口县| 曲阜市| 衡东县| 木兰县| 含山县| 吉木萨尔县| 河北省| 连平县| 潞城市| 五河县| 牟定县| 达拉特旗| 甘肃省| 霸州市| 云和县| 普定县| 杭锦后旗| 辉县市| 三穗县| 山丹县| 迁安市| 张家港市| 乌兰浩特市| 耒阳市| 通许县| 鄂伦春自治旗| 崇左市| 开平市| 龙岩市| 四子王旗| 芒康县| 辰溪县| 石林| 荥经县| 柳河县|