Step by step tutorial how to create a simple Music Player on Android Studio 3.3.1. Create new project with Application name: Simple music player; Minimum SDK: API 15Android 4.0.3 (IceCreamSandwich). In app->res->layout-> create New Directory: raw and copy-paste a sound from your computer, with name: sound.mp3. In app->res->layout->activity_main.xml write the code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.275"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.500" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pause"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.725"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.500" />

</android.support.constraint.ConstraintLayout>
  In app->java->MainActivity.java write the code:
package com.example.toyo.simplemusicplayer;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = MediaPlayer.create(this, R.raw.sound);
        Button playButton = (Button) findViewById(R.id.start);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.start();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        Toast.makeText(MainActivity.this, "The Song is Over", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        Button pauseButton = (Button) findViewById(R.id.stop);

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.pause();
            }
        });
    }
}
Run app in mobile device or Emulator (API 28 Android 9.0) and see how it works. Follow video tutorial: Download this app for free and install it on your mobile device to see how it works: https://android-coffee.com/wp-content/uploads/projecs/Simplemusicplayer.apk Download project with Source Code from here: https://android-coffee.com/wp-content/uploads/projecs/Simplemusicplayer.zip Please subscribe here to our YouTube channel. Thank you! Check next tutorials to learn all about Android. For further questions leave a message.

5 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

Good article i really appreciate you.

good article i really appreciate you

so nice

can you make tutorial on How to make Simple Music Player in Android Studio using exo player

i also really appreciate you. good job

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image Refresh

*