Challenge 1: PlayBack Speed Control

I’m sure this is not the best way to do it… but i didn’t know how to do the best way…
maybe i’m not patient enough…

I found out that we can add only one element inside <layout> ... </layout> in xml for bindingview…

therefore i added a Constrained layout and made the recyclerview and seekbar inside them… that was a messy design but here it is anyway…

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="584dp"
            app:layout_constraintBottom_toTopOf="@+id/seek_bar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <SeekBar
            android:id="@+id/seek_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="5"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/recycler_view"
            android:visibility="visible"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

The trick i made… which is not professional as i think it should be is creating a value inside BeatBox.kt that can link the seekBar with the rate value inside soundpool.play(...)
and applied that value within soundpool…

private const val TAG = "BeatBox"
private const val SOUND_FOLDER = "sample_sound"
private const val MAX_SOUNDS = 5

var rate = 1.0f

class BeatBox(private val assets: AssetManager) {


<!--- The rest of the code is the same --->


  fun play(sound:Sound){
        sound.soundID?.let {
            soundPool.play(it, 1.0f, 1.0f,1,0, rate)
        }
    }

Inside MainActivity I applied the seekBar and linked the progress with the value

 binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                val value = progress.toFloat()
                rate = value

            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }

        })

as per the documentation the command should be setRate(soundID, rate)

i didn’t know how to do that between both files… maybe using companion object?

I added the SeekBar in my recyclerView and the design was okay. In fact, it was just like the one in the book. What I did was set the recyclerView “layout_height” to match constraint(0dp) to fill the whole screen and also constrain the top of the seekBar to the bottom of the recyclerView. I think if you try this, the design will be okay

1 Like