Building a Music Player App with ExoPlayer

梦幻舞者 2021-11-08 ⋅ 15 阅读

Introduction

In this blog post, we will explore how to build a music player app using ExoPlayer, a powerful media playback library for Android. ExoPlayer provides a flexible and extensible foundation for playing audio and video files. We will be using Kotlin and Java for Android development.

Setting up the project

To get started, make sure you have Android Studio installed on your machine. Create a new Android project and add the necessary dependencies for ExoPlayer in your project's build.gradle file.

dependencies {
    implementation 'com.google.android.exoplayer:exoplayer-core:2.14.2'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.14.2'
}

Creating the UI

Next, let's design the user interface for our music player app. You can use XML layout files to define the layout of your app. Create a main activity layout file with a toolbar, a list view for displaying the songs, and playback controls.

<!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <ListView
        android:id="@+id/song_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/player_controls"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/play_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Play" />

        <Button
            android:id="@+id/stop_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop" />
    </LinearLayout>
</LinearLayout>

Implementing the Music Player

Now, let's start implementing the functionality of our music player app. In the MainActivity class, initialize the ExoPlayer instance and prepare it for playback.

class MainActivity : AppCompatActivity() {
    private var player: SimpleExoPlayer? = null

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

        // Initialize the player
        val playerView = findViewById<PlayerView>(R.id.player_view)
        player = ExoPlayerFactory.newSimpleInstance(this)
        playerView.player = player

        // Prepare the media source
        val uri = Uri.parse("https://example.com/music.mp3")
        val mediaSource = buildMediaSource(uri)
        player?.prepare(mediaSource)
    }

    private fun buildMediaSource(uri: Uri): MediaSource {
        // Build a DefaultDataSourceFactory to fetch the media data
        val userAgent = Util.getUserAgent(this, "MusicPlayerApp")
        val dataSourceFactory = DefaultDataSourceFactory(this, userAgent)

        // Create a MediaSource using the URI and the data source factory
        return ExtractorMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri)
    }

    override fun onDestroy() {
        player?.release()
        super.onDestroy()
    }
}

Adding Playback Controls

To control the playback, we will add event listeners to the play and stop buttons in our UI. In the MainActivity class, add click listeners and implement the necessary methods.

class MainActivity : AppCompatActivity() {
    // ...

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

        // ...

        val playButton = findViewById<Button>(R.id.play_button)
        val stopButton = findViewById<Button>(R.id.stop_button)

        playButton.setOnClickListener {
            player?.playWhenReady = true
        }

        stopButton.setOnClickListener {
            player?.playWhenReady = false
        }
    }
}

Conclusion

In this tutorial, we have learned how to build a music player app using ExoPlayer in Kotlin and Java. We set up the project, created the user interface, and implemented the music playback functionality. ExoPlayer provides a powerful and flexible solution for media playback in Android apps. We hope this tutorial has been helpful in building your own music player app. Happy coding!

References


全部评论: 0

    我有话说: