相关文章推荐
In this tutorial, we will learn how to create a music player app in Kotlin using MediaPlayer and ExoPlayer libraries. These libraries provide an easy way to play audio and video files in your Android applications. By the end of this tutorial, you will have a fully functioning music player app.

Requirements

  • Android Studio
  • Kotlin Knowledge
  • Step 1: Create a new Android Studio Project

    Open Android Studio and create a new project. Choose 'Empty Activity' and click 'Next'. Enter your desired application name, package name, and save location. Select 'Kotlin' as the programming language and click 'Finish'.

    Step 2: Add Required Dependencies

    In your app's 'build.gradle' file, add the following dependencies:

    dependencies { implementation 'com.google.android.exoplayer:exoplayer:2.X.X'

    Replace '2.X.X' with the latest version of ExoPlayer. Sync the project after adding the dependency.

    Step 3: Create the User Interface (UI)

    Open 'activity_main.xml' and add the following code to create a simple UI for our music player:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <com.google.android.exoplayer2.ui.PlayerView android:id="@+id/playerView" android:layout_width="match_parent" android:layout_height="wrap_content" app:show_timeout="0" app:resize_mode="fill"/> <Button android:id="@+id/playButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/stopButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" android:layout_gravity="center_horizontal" /> </LinearLayout>

    Step 4: Implement the Music Player

    Open 'MainActivity.kt' and add the following code:

    import android.net.Uri import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.google.android.exoplayer2.ExoPlayerFactory import com.google.android.exoplayer2.source.ExtractorMediaSource import com.google.android.exoplayer2.trackselection.DefaultTrackSelector import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { private val player by lazy { ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector()) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) playerView.player = player playButton.setOnClickListener { val source = ExtractorMediaSource.Factory( DefaultHttpDataSourceFactory("MusicPlayer") ).createMediaSource(Uri.parse("https://your-music-url.mp3")) player.prepare(source) player.playWhenReady = true stopButton.setOnClickListener { player.stop() override fun onDestroy() { super.onDestroy() player.release()

    Replace 'https://your-music-url.mp3' with the URL of the music file you want to play.

    Step 5: Run the App

    Build and run the app on an emulator or a physical device. The music player should now be able to play and stop the music file.

    Conclusion

    In this tutorial, we learned how to create a simple music player app in Kotlin using MediaPlayer and ExoPlayer libraries. This app can play and stop music files from a given URL. You can extend the functionality by adding more features, such as playlists and local file playback. If you need to hire Android developers , check out Reintech's services.

    Are you an engineering manager tired of recruiting?

    Checkout out how we can help you to focus on delivering technical excellence and growing your product by hiring remote developers and creating high-performing teams.

    Learn more
     
    推荐文章