في هذا الكود 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 var mediaRecorder: MediaRecorder? = null
    private lateinit var outputFilePath: String
    private val lyricsHandler = Handler(Looper.getMainLooper())
    private lateinit var lyricsRunnable: Runnable
    private var mediaPlayer: MediaPlayer? = null
    private lateinit var playRecordingButton: Button

    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 {
            playRecording()
        }

        val soundEffectsButton1 = findViewById<Button>(R.id.soundEffectButton1)
        val soundEffectsButton2 = findViewById<Button>(R.id.soundEffectButton2)
        soundEffectsButton1.setOnClickListener {
            applySoundEffect1()
        }

        soundEffectsButton2.setOnClickListener {
            applySoundEffect2()
        }
    }

    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() {
        scheduleLyricsUpdate()
    }

    private fun scheduleLyricsUpdate() {
        lyricsRunnable = Runnable {
            updateLyrics()
            lyricsHandler.postDelayed(lyricsRunnable, 1000) // جدولة التحديث كل ثانية
        }
        lyricsHandler.post(lyricsRunnable)
    }

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

    private fun requestAudioPermissionAndStartRecording() {
        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 {
            startRecording()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == RECORD_AUDIO_REQUEST_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startRecording()
            } else {
                // الإذن مرفوض، يمكنك إظهار رسالة توضيحية هنا
            }
        }
    }

    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()
            player.stop()
        }
        mediaRecorder = null
    }

    override fun onStop() {
        super.onStop()
        lyricsHandler.removeCallbacks(lyricsRunnable)
        releasePlayer()
        mediaRecorder?.apply {
            stop()
            release()
        }
        mediaRecorder = null

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

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

    companion object {
        private const val RECORD_AUDIO_REQUEST_CODE = 200
    }

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

                    // تأكد من توافر PresetReverb قبل استخدامه
                    if (checkIfAudioEffectIsAvailable(PresetReverb.EFFECT_TYPE_VIRTUALIZER)) {
                        val effect = PresetReverb(0, audioSessionId).apply {
                            preset = PresetReverb.PRESET_LARGEROOM
                        }

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

                        setOnCompletionListener {
                            setAuxEffectSendLevel(0f)
                            attachAuxEffect(0)
                            effect.release()
                        }
                    } else {
                        Toast.makeText(applicationContext,"not work",Toast.LENGTH_SHORT).show()
                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }

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

                    // تأكد من توافر BassBoost قبل استخدامه
                    if (checkIfAudioEffectIsAvailable(BassBoost.EFFECT_TYPE_BASS_BOOST)) {
                        val effect = BassBoost(0, audioSessionId).apply {
                            setStrength(500)
                        }

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

                        setOnCompletionListener {
                            setAuxEffectSendLevel(0f)
                            attachAuxEffect(0)
                            effect.release()
                        }
                    } else {
                        Toast.makeText(applicationContext,"not work 2",Toast.LENGTH_SHORT).show()

                    }
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }

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

    private fun checkIfAudioEffectIsAvailable(effectUUID: UUID): Boolean {
        val effects = AudioEffect.queryEffects()

        return effects.any { it.uuid == effectUUID }
    }
} كيف يتم اضافة تاثيرات خارجيه غير متعلقه بالهاتف يرجي تعديل الكود ووضع عدد من التاثيرات الخارجية وضمها للتطبيق