[英]Why can't I create a file object in Kotlin?
New to Kotlin. Kotlin 新手。 Trying to save an audio recording to storage.尝试将录音保存到存储中。 I'm currently stuck on trying to use MediaRecord.setOutputFile(filename).我目前坚持尝试使用 MediaRecord.setOutputFile(filename)。 However, it is erroring out because of this error:但是,由于此错误,它出错了:
java.io.FileNotFoundException: /storage/emulated/0/Recording1.mp3: open failed: EPERM (Operation not permitted) java.io.FileNotFoundException: /storage/emulated/0/Recording1.mp3: open failed: EPERM (Operation not allowed)
So now I'd like to create a File
before setOutputFile is called to try to solve this problem.所以现在我想在调用 setOutputFile 之前创建一个File
来尝试解决这个问题。 But for some reason Android Studio keeps underlining the line below saying "Expecting a top level declaration".但出于某种原因,Android Studio 一直在下面的“期望顶级声明”行下划线。
File fileval = File.createNewFile("blah.mp3"); //This is the line
The full code:完整代码:
package com.example.testapp.data
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.media.MediaRecorder;
import android.os.Environment
import android.os.PersistableBundle
import com.example.testapp.R
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.media.MediaPlayer
import android.os.Build
import android.os.Environment.getDataDirectory
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.AppCompatButton
import java.io.File
import java.io.IOException
private const val LOG_TAG = "AudioRecordTest"
private const val REQUEST_RECORD_AUDIO_PERMISSION = 200
File fileval = File.createNewFile("blah.mp3"); //This is the line
public class RecordActivity : AppCompatActivity() {
private var mr: MediaRecorder? = null
private var recordbtn: Button? = null
private var filename: String? = null
private var state: Boolean = false
private var recordingStopped: Boolean = false
private var player: MediaPlayer? = null
private var playbtn: Button? = null
// Requesting permission to RECORD_AUDIO
private var permissionToRecordAccepted = false
private var permissions: Array<String> = arrayOf(Manifest.permission.RECORD_AUDIO)
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_record)
requestPermissions(permissions, REQUEST_RECORD_AUDIO_PERMISSION)
recordbtn = RecordButton(this)
playbtn = PlayButton(this)
filename = Environment.getExternalStorageDirectory().getAbsolutePath()
filename += "/Recording1.mp3"
mr = MediaRecorder()
mr?.setAudioSource(MediaRecorder.AudioSource.MIC)
mr?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
mr?.setOutputFile(filename)
mr?.setAudioEncoder(3) //AAC; need to review what the best choice here is.
mr?.prepare()
}
//more code...
}
You're using Java syntax, not Kotlin syntax.您使用的是 Java 语法,而不是 Kotlin 语法。 It's它是
val fileval: File = File.createNewFile("blah.mp3")
Use below code in the AppCompatActivity()在 AppCompatActivity() 中使用以下代码
fileName = "${externalCacheDir!!.absolutePath}/Recording1.3gp"
mr = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
setOutputFile(fileName)
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
try {
prepare()
} catch (e: IOException) {
Log.e(LOG_TAG, "prepare() failed")
}
start()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.