为什么我不能在kotlin中创建一个file对象?

wmvff8tz  于 2021-07-04  发布在  Java
关注(0)|答案(2)|浏览(515)

Kotlin的新朋友。正在尝试将音频录制保存到存储器。我目前一直在尝试使用mediarecord.setoutputfile(文件名)。但是,它是错误的,因为这个错误:
java.io.filenotfoundexception:/storage/emulated/0/recording1.mp3:打开失败:eperm(不允许操作)
现在我想创建一个 File 在调用setoutputfile来解决此问题之前。但由于某些原因,androidstudio一直在下面强调“期待一个顶级声明”。

File fileval = File.createNewFile("blah.mp3"); //This is the line

完整代码:

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...

}
vngu2lb8

vngu2lb81#

您使用的是java语法,而不是kotlin语法。是的

val fileval: File = File.createNewFile("blah.mp3")
blpfk2vs

blpfk2vs2#

在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()
        }

相关问题