Android Media Player Wont Play After Stop
Asked Answered
A

7

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();

                            }

                        }   
                    }
                }
            };
        }
    }

        );}}
Antoninus answered 14/4, 2013 at 16:49 Comment(0)
V
10

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

mp.reset();
mp.setDataSource(MEDIA_PATH);
mp.prepare();
mp.start();
Verisimilar answered 14/4, 2013 at 16:55 Comment(2)
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.Commutation
what if song play in looping? how to reset?Monongahela
C
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);
        }
    });}
Counteraccusation answered 26/2, 2015 at 6:56 Comment(0)
I
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>
Impatiens answered 28/1, 2020 at 7:17 Comment(0)
E
0

Then you should use pause function instead of stop

mediaplayer.pause();

by this you can play it again easily by

mediaplayer.start();
Embowed answered 23/7, 2022 at 7:32 Comment(0)
W
0

Easy solution is don't call stop() function. Call below 2 functions instead.

    mp.seekTo(0)
    mp.pause()

This is kotlin code and java are same just with ; after each line. Here mp es mediaPlayer object reference. seekTo(0) will take the media player to first position and pause() function will pause the player. When user will click play again it will play normally without any issue.

Wendt answered 9/11, 2023 at 16:51 Comment(0)
M
-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);
        }
    });
Mandiemandingo answered 10/10, 2016 at 14:31 Comment(3)
This seems like a really weird solution. What are the benefits of having an array of MediaPlayer objects?Requisition
I am not sure how this works .. But anyway we will be using only one MediaPlayer Object ie .. mediaPlayer[0] @RequisitionMandiemandingo
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 songsRequisition
A
-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);
Ambrosine answered 20/8, 2020 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.