Tutorials  /  JavaScript

Step-by-Step Guide: Creating an Audio Player for Android using MediaPlayer and SeekBar

Ccentron Redaktion · September 2024 ·3 min read ·JavaScript, Tutorial

Explore in our latest blog post how to develop a user-friendly audio player for Android using MediaPlayer and SeekBar. Learn step by step how to implement play/stop functions and dynamically change the song position. Dive into the world of Android development and take your music apps to the next level!

Android MediaPlayer

The MediaPlayer class is used for playing audio and video files. The commonly used methods of the MediaPlayer class are:

  • start()
  • stop()
  • release() - To avoid memory leaks.
  • seekTo(position) - This is used with the SeekBar.
  • isPlaying() - Indicates whether the song is playing or not.
  • getDuration() - Used to get the total duration, which helps determine the upper limit of our SeekBar. This function returns the duration in milliseconds.
  • setDataSource(FileDescriptor fd) - Used to set the file to be played.
  • setVolume(float leftVolume, float rightVolume) - Used to set the volume level. The value is a float number between 0 and 1.
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Project Structure

We need three things to create an application that plays audio and allows you to change the position of the current song track:

  • MediaPlayer
  • SeekBar with Text - To display the current progress time next to the thumb.
  • Runnable Thread - To update the SeekBar.

Conclusion

By clicking on the FloatingActionButton, the playSong function is triggered, where we pause the song and reset the MediaPlayer. Once mediaPlayer.prepare() is called, the song details are available. We can now get the duration and set it to the maximum position of the SeekBar. Setting setLooping to false prevents the song from playing infinitely until the user stops it. We start the thread that triggers the run method, which is part of the Runnable interface we implemented. Within the run method, we update the progress every second, triggering the onProgressChanged method of the SeekBar listener. When the user stops dragging the SeekBar, onStopTrackingTouch is triggered, where we update the song position on the MediaPlayer instance with the seekTo method. Once the song is completed, we reset the position of the SeekBar and release the MediaPlayer instance.

Closing Words

This tutorial demonstrates how to implement a simple audio player in your Android application. You can download the project from GitHub and play the song yourself.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren