من هذا الكود 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()
        }
    }


    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 playRecording() {
        if (outputFilePath.isNotEmpty()) {
            mediaPlayer = MediaPlayer().apply {
                try {
                    setDataSource(outputFilePath)
                    prepare()
                    start()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }
} كيف يتم تحسين جودة الصوت بعد التسجيل وكيفية اضافة ازرار تاثيرات علي الصوت مع كتابة الكود كاملا بعد الاضافات