ارجوا تعديل هذا الكود ليعمل بطريقه صحيحه وربط الاجزاء بعضها ببعض ليكون مثالي  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()
        }
        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)

                    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 fun applySoundEffect2() {
        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 playRecording() {
        if (outputFilePath.isNotEmpty()) {
            mediaPlayer = MediaPlayer().apply {
                try {
                    setDataSource(outputFilePath)
                    prepare()
                    start()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }
}