如何用android 11录制音频



我正在制作一个在android 11设备上录制音频的应用程序。然而,经过几天的努力,我仍然无法设置权限。我一直得到这样的错误

I/System.out: java.io.FileNotFoundException: /storage/emulated/0/Recordings/2022-12-14T10:11:37.089test.3gp: open failed: EPERM (Operation not permitted)

调用record .start()后,APP将关闭。看起来它需要更多的许可才能这样做。下面是我的代码:

package com.example.eslab_final_impromptu
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.media.MediaRecorder
import android.media.SoundPool
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.eslab_final_impromptu.databinding.FragmentDetailBinding
import java.io.IOException
import java.time.LocalDateTime

class DetailFragment : Fragment() {
private var _binding: FragmentDetailBinding? = null
private val binding get() = _binding!!
private var recorder: MediaRecorder? = null
private var recording=false
private var isRecordable=true
private val requestPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission())
{ isGranted: Boolean ->
if (isGranted) {
// Permission is granted. Continue the action or workflow in your
// app.
} else {
isRecordable=false
Log.d("ddddd","permission")
// Explain to the user that the feature is unavailable because the
// feature requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentDetailBinding.inflate(inflater, container, false)
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.RECORD_AUDIO )!= PackageManager.PERMISSION_GRANTED
) {
Log.d("ddddd","audio")
requestPermission.launch(Manifest.permission.RECORD_AUDIO)
}
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE )!= PackageManager.PERMISSION_GRANTED
) {
Log.d("ddddd","write")
requestPermission.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.sheet.setOnClickListener {
val action =
DetailFragmentDirections.actionDetailFragmentToSheetFragment()
this.findNavController().navigate(action)
}
binding.settings.setOnClickListener {
}
binding.disconnect.setOnClickListener {
val action =
DetailFragmentDirections.actionDetailFragmentToHomeFragment()
this.findNavController().navigate(action)
}
binding.record.setOnClickListener{
if(!recording)
record()
else
stopRecord()
}
}
override fun onDestroyView() {
super.onDestroyView()
}
fun record(){
Log.d("ddddd",LocalDateTime.now().toString())
if(!isRecordable)
return
var mFileName:String = Environment.getExternalStorageDirectory().absolutePath+"/Recordings/"+ LocalDateTime.now().toString()+"test.3gp"
println(Environment.getExternalStorageState())
recorder = MediaRecorder().apply {
setAudioSource( MediaRecorder.AudioSource.DEFAULT)
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
setOutputFile(mFileName)
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
try {
prepare()
} catch (e: IOException) {
println(e)
}
start()
}
recording=true
}
fun stopRecord(){
recorder?.apply {
stop()
release()
}
recorder = null
recording=false
}
}

我现在请求RECORD_AUDIO和WRITE_EXTERNAL_STORAGE。我还需要点什么吗?我的设备是Android 11和SDK30。

Google发布了关于在应用程序特定存储中写入文件的新存储更新。在SDK 30或更高版本中不能访问WRITE_EXTERNAL_STORAGE权限。阅读:https://developer.android.com/about/versions/11/privacy/storage

删除WRITE_EXTERNAL_STORAGE权限请求。

尝试以下解决方案:

var mFileName:String = getContext().externalMediaDirs[0].absolutePath+"/" + System.currentTimeMillis()+"test.3gp"

相关内容

  • 没有找到相关文章

最新更新