10

I have music playlist for 5 songs. I just want that play and stop buttons work as long as im in app. And that i can stop music when i want to and start another.

How this works now...The music plays on PLAY button, and when i click STOP button it stops, but then i want to play some other song, or same song again, nothing happens. Please help.

public class glavna extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.activity_main);



    final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.pj1);
    final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.pj2);
    final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.pj3);
    final MediaPlayer MPRadio4 = MediaPlayer.create(this, R.raw.pj4);
    final MediaPlayer MPRadio5 = MediaPlayer.create(this, R.raw.pj5);

    final RadioButton rb1, rb2, rb3, rb4, rb5;      

    rb1 = (RadioButton) findViewById(R.id.radio1);
    rb2 = (RadioButton) findViewById(R.id.radio2);
    rb3 = (RadioButton) findViewById(R.id.radio3);
    rb4 = (RadioButton) findViewById(R.id.radio4);
    rb5 = (RadioButton) findViewById(R.id.radio5);


    Button btn = (Button) findViewById(R.id.buttonplay);
    Button btnStop = (Button) findViewById(R.id.buttonStop);

    btnStop.setOnClickListener(new View.OnClickListener() {

    public void onClick(View b){

        MPRadio1.stop();
        MPRadio2.stop();
        MPRadio3.stop();
        MPRadio4.stop();
        MPRadio5.stop();


    };
    });


    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            if(rb1.isChecked())
            {       

            MPRadio1.start();

            }
        else
            {
            if(rb2.isChecked())
            {

                MPRadio2.start();
            }
                else
                {
                    if(rb3.isChecked())
                    {
                    MPRadio3.start();

                }
                    else
                    {
                        if(rb4.isChecked())
                        {
                        MPRadio4.start();

                    }
                        else
                        {
                            if(rb5.isChecked())
                            {
                            MPRadio5.start();

                            }

                        }   
                    }
                }
            };
        }
    }

        );}}
user2027663
  • 123
  • 1
  • 1
  • 5

6 Answers6

10

to play song again reset media player, set data source again and start

mp.reset();
mp.setDataSource(MEDIA_PATH);
mp.prepare();
mp.start();
Farnabaz
  • 4,030
  • 1
  • 22
  • 42
  • I'm sure this is correct, but the state diagram presented in the documentation doesn't seem to support this assertion. I feel like the MediaPlayer API is poorly designed in general. – IcedDante Feb 26 '17 at 00:57
  • 1
    what if song play in looping? how to reset? – Anand Savjani Aug 25 '18 at 11:32
1

When press play button after stop ,then play button never works--for this problem we can create object again in stop button. for eg;-stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mPlayer.isPlaying())
            mPlayer.stop();
            mPlayer  = MediaPlayer.create(mediaplayeractivity.this, R.raw.adidas);
        }
    });}
sahil garg
  • 19
  • 1
0

You can check this Demo Example Here, i am looping a Audio clicp continously and stopping on Button Cick and playing again.

package com.Test.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.SECONDS;

public class MainActivity extends AppCompatActivity {

    Button scan;
    Button stop;

    MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scan=(Button)findViewById(R.id.scan);
        stop=(Button)findViewById(R.id.stop);
        mp = MediaPlayer.create(this, R.raw.trackeralertseparation);
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mp.setLooping(true);
                mp.start();
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        mp.reset();
                        mp.release();
                    }
                });
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(mp.isPlaying()){
                    mp.stop();
                }
                mp=MediaPlayer.create(getApplicationContext(), R.raw.trackeralertseparation);
            }
        });
    }
}

The Design File is Below with simple Button.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">


        <Button
            android:id="@+id/scan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="195dp"
            android:gravity="center_vertical"
            android:padding="4dp"
            android:text="Play"
            android:textSize="26sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stop"
            android:padding="4dp"
            android:layout_centerInParent="true"
            android:textSize="26sp"
            android:textStyle="bold"/>

    </RelativeLayout>
vinay shetty
  • 895
  • 1
  • 9
  • 15
0

Then you should use pause function instead of stop

mediaplayer.pause();

by this you can play it again easily by

mediaplayer.start();
Afshin Izadi
  • 527
  • 1
  • 6
  • 13
-1

When the play wont work after you hit the stop button... just make your mediaplayer object as an object array that should fix it..

final MediaPlayer[] mediaPlayer = {MediaPlayer.create(getApplicationContext(), R.raw.song_name)};

stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mediaPlayer[0].stop();

            mediaPlayer[0] = MediaPlayer.create(getApplicationContext(),R.raw.song_name);
        }
    });
Allan_Aj5
  • 125
  • 1
  • 4
  • This seems like a really weird solution. What are the benefits of having an array of MediaPlayer objects? – Seabass77 Feb 18 '18 at 20:05
  • I am not sure how this works .. But anyway we will be using only one MediaPlayer Object ie .. mediaPlayer[0] @Seabass77 – Allan_Aj5 Feb 20 '18 at 12:59
  • Well I don't think there's ever an instance where you should have more than one media player... but if it works and you don't see any significant performance issues then it guess it's all good Edit: What I mean is that you should pause and start the same media player if you want to play 2 different songs – Seabass77 Feb 20 '18 at 13:09
-1

When you stop music, must declare the song again in create.

the easy way using Android Studio:

mPlayer  = MediaPlayer.create(mediaplayeractivity.this, R.raw.adidas);