في هذا الكود class MainActivity : AppCompatActivity() {

    private lateinit var player: SimpleExoPlayer
    private lateinit var playButton: Button
    private lateinit var lyricsTextView: TextView
    private lateinit var startRecordingButton: Button
    private lateinit var stopRecordingButton: Button
    private lateinit var outputFilePath: String
    private var mediaPlayer: MediaPlayer? = null
    private var mediaRecorder: MediaRecorder? = null
    private lateinit var playRecordingButton: Button

    private val lyricsScope = CoroutineScope(Dispatchers.Main)
    private lateinit var lyricsJob: Job

    private val soundEffectsScope = CoroutineScope(Dispatchers.Default)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        playButton = findViewById(R.id.playButton)
        lyricsTextView = findViewById(R.id.lyricsTextView)
        startRecordingButton = findViewById(R.id.startRecordingButton)
        stopRecordingButton = findViewById(R.id.stopRecordingButton)

        playButton.setOnClickListener {
            initializePlayer()
            displayLyrics()
        }

        startRecordingButton.setOnClickListener {
            requestAudioPermissionAndStartRecording()
        }

        stopRecordingButton.setOnClickListener {
            stopRecording()
        }

        playRecordingButton = findViewById(R.id.playRecordingButton)

        playRecordingButton.setOnClickListener {
            startPlayback()
        }

        val soundEffectsButton1 = findViewById<Button>(R.id.soundEffectButton1)
        val soundEffectsButton2 = findViewById<Button>(R.id.soundEffectButton2)

        soundEffectsButton1.setOnClickListener {
            soundEffectsScope.launch {
                applySoundEffect1()
            }
        }

        soundEffectsButton2.setOnClickListener {
            soundEffectsScope.launch {
                applySoundEffect2()
            }
        }
    }

    // Coroutines يتم إرجاع إذن الصوت التلقائيًا بواسطة هذه الدالة الخارجية التي تستخدم
    private fun requestAudioPermission() = if (ContextCompat.checkSelfPermission(
            this,
            android.Manifest.permission.RECORD_AUDIO
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(android.Manifest.permission.RECORD_AUDIO),
            RECORD_AUDIO_REQUEST_CODE
        )
    } else {
        true
    }

    private fun initializePlayer() {
        player = SimpleExoPlayer.Builder(this).build()

        val dataSourceFactory = DefaultDataSourceFactory(
            this,
            Util.getUserAgent(this, "KaraokeApp")
        )

        val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(Uri.parse("https://directsendwhats.xyz/background_music.mp3"))

                    player.setMediaSource(mediaSource)
                    player.prepare()
                    player.playWhenReady = true
    }

    private fun displayLyrics() {
        lyricsJob = lyricsScope.launch {
            while (true) {
                updateLyrics()
                delay(1000)
            }
        }
    }

    private fun updateLyrics() {
        val currentPosition = player.currentPosition
// استخدم currentPosition لتحديد الجزء المناسب من كلمات الأغنية
// وقم بتحديث lyricsTextView بناءً على ذلك
    }

    private fun requestAudioPermissionAndStartRecording() {
        if (requestAudioPermission() as Boolean) {
            startRecording()
        }
    }

    private fun startRecording() {
        outputFilePath = "${getExternalFilesDir(Environment.DIRECTORY_MUSIC)?.absolutePath}/recording.3gp"
        mediaRecorder = MediaRecorder().apply {
            setAudioSource(MediaRecorder.AudioSource.MIC)
            setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
            setOutputFile(outputFilePath)
            setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)

            try {
                prepare()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            start()
        }
    }

    private fun stopRecording() {
        mediaRecorder?.apply {
            stop()
            release()
        }
        mediaRecorder = null
    }

    override fun onStop() {
        super.onStop()
        if (::lyricsJob.isInitialized) lyricsJob.cancel()
        releasePlayer()
        mediaRecorder?.apply {
            stop()
            release()
        }
        mediaRecorder = null

        mediaPlayer?.apply {
            stop()
            release()
        }
        mediaPlayer = null
    }

    private fun releasePlayer() {
        if (::player.isInitialized) {
            player.release()
        }
    }

// تطبيق التأثيرات الصوتية

    private suspend fun applySoundEffect1() {
        withContext(Dispatchers.IO) {
            if (outputFilePath.isNotEmpty()) {
                mediaPlayer = MediaPlayer().apply {
                    try {
                        setDataSource(outputFilePath)

                        val effect = PresetReverb(1, 0).apply {
                            preset = PresetReverb.PRESET_LARGEROOM
                        }

                        setAuxEffectSendLevel(1f)
                        attachAuxEffect(effect.id)
                        prepare()
                        start()

                        setOnCompletionListener {
                            setAuxEffectSendLevel(0f)
                            attachAuxEffect(0)
                            effect.release()
                        }
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
            }
        }
    }

    private suspend fun applySoundEffect2() {
        withContext(Dispatchers.IO) {
            if (outputFilePath.isNotEmpty()) {
                mediaPlayer = MediaPlayer().apply {
                    try {
                        setDataSource(outputFilePath)

                        val effect = BassBoost(1, 0).apply {
                            setStrength(500)
                        }

                        setAuxEffectSendLevel(1f)
                        attachAuxEffect(effect.id)
                        prepare()
                        start()

                        setOnCompletionListener {
                            setAuxEffectSendLevel(0f)
                            attachAuxEffect(0)
                            effect.release()
                        }
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
            }
        }
    }

// تشغيل التسجيل

    private fun startPlayback() {
        if (outputFilePath.isNotEmpty()) {
            mediaPlayer = MediaPlayer().apply {
                try {
                    setDataSource(outputFilePath)
                    prepare()
                    start()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }

    companion object {
        private const val RECORD_AUDIO_REQUEST_CODE = 200
    }
} ما هو حل المشكلة set(): AudioFlinger could not create effect 0634f220-ddd4-11db-a0fc-0002a5d5c51b / ec7178ec-e5e1-4432-a3f4-4657e6795210, status: -1
2023-05-01 13:01:30.387 28574-11308 AudioEffects-JNI        com.dm.myapplication2                E  AudioEffect initCheck failed -3
2023-05-01 13:01:30.387 28574-11308 AudioEffect-JAVA        com.dm.myapplication2                E  Error code -3 when initializing AudioEffect.
2023-05-01 13:01:30.410 28574-11308 AndroidRuntime          com.dm.myapplication2                E  FATAL EXCEPTION: DefaultDispatcher-worker-2
                                                                                                    Process: com.dm.myapplication2, PID: 28574
                                                                                                    java.lang.RuntimeException: Cannot initialize effect engine for type: 0634f220-ddd4-11db-a0fc-0002a5d5c51b Error: -3
                                                                                                    	at android.media.audiofx.AudioEffect.<init>(AudioEffect.java:543)
                                                                                                    	at android.media.audiofx.AudioEffect.<init>(AudioEffect.java:501)
                                                                                                    	at android.media.audiofx.AudioEffect.<init>(AudioEffect.java:475)
                                                                                                    	at android.media.audiofx.BassBoost.<init>(BassBoost.java:94)
                                                                                                    	at com.dm.myapplication2.MainActivity$applySoundEffect2$2.invokeSuspend(MainActivity.kt:229)