Search code examples
javaandroidxmlalphabetical

MediaPlayer is not playing audio each button click same time


I'm using MediaPlayer for alphabet. while clicking a single button audio is playing. each button click is not responsible. while audio end then working another button. but i want to set each button click play audio same time. how to do this? please inform me that how to solve this problem.

sor_borno.xml

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:padding="10dp"
    android:layout_gravity="center"
    android:background="@drawable/onlybg"
    tools:context="net.gurujibd.bornomala.alphabet.SorBorno">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linearLayout"
        android:layout_above="@+id/linearLayout3"
        android:layout_alignLeft="@+id/linearLayout3"
        android:layout_alignStart="@+id/linearLayout3">
<Button
    android:id="@+id/Soreo"
    android:onClick="soreo"
    android:layout_width="100dp"
    android:layout_height="85dp"
    android:background="@drawable/soreo"/>

        <Button
            android:id="@+id/Sorea"
            android:onClick="sorea"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/sorea"/>

        <Button
            android:id="@+id/Roshshoi"
            android:onClick="roshshoi"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/roshshoi"/>
        <Button
            android:id="@+id/Dirghoi"
            android:onClick="dirghoi"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/dirghoi"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linearLayout2"
        android:layout_below="@+id/linearLayout3"
        android:layout_centerHorizontal="true">
        <Button
            android:id="@+id/Oi"
            android:onClick="oi"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/oi"/>

        <Button
            android:id="@+id/Oo"
            android:onClick="oo"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/oo"/>

        <Button
            android:id="@+id/Ou"
            android:onClick="ou"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/ou"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linearLayout3"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">
        <Button
            android:id="@+id/Roshshou"
            android:onClick="roshshou"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/roshshou"/>

        <Button
            android:id="@+id/Durghou"
            android:onClick="durghou"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/durghou"/>

        <Button
            android:id="@+id/Ree"
            android:onClick="ree"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/ree"/>
        <Button
            android:id="@+id/Aa"
            android:onClick="aa"
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:background="@drawable/aa"/>

    </LinearLayout>

</RelativeLayout>

SorBorno.java

package net.gurujibd.bornomala.alphabet;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class SorBorno extends AppCompatActivity {

    InterstitialAd mInterstitialAd;
    private InterstitialAd interstitial;

    Button Soreo, Sorea, Roshshoi, Dirghoi, Roshshou, Durghou, Ree, Aa, Oi, Oo, Ou;

    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.sor_borno);

        AdRequest adRequest = new AdRequest.Builder().build();
        interstitial = new InterstitialAd(SorBorno.this);
        interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));

        interstitial.loadAd(adRequest);
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                displayInterstitial();
            }
        });
    }

    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }

    public void soreo (View v){
        Button button = (Button)findViewById(R.id.Soreo);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.soreo);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void sorea (View v){
        Button button = (Button)findViewById(R.id.Sorea);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.sorea);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void roshshoi (View v){
        Button button = (Button)findViewById(R.id.Roshshoi);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.roshshoi);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void dirghoi (View v){
        Button button = (Button)findViewById(R.id.Dirghoi);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.dirghoi);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void roshshou (View v){
        Button button = (Button)findViewById(R.id.Roshshou);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.roshshou);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void durghou (View v){
        Button button = (Button)findViewById(R.id.Durghou);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.durghou);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void ree (View v){
        Button button = (Button)findViewById(R.id.Ree);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.ree);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void aa (View v){
        Button button = (Button)findViewById(R.id.Aa);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.aa);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void oi (View v){
        Button button = (Button)findViewById(R.id.Oi);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.oi);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void oo (View v){
        Button button = (Button)findViewById(R.id.Oo);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.oo);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void ou (View v){
        Button button = (Button)findViewById(R.id.Ou);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
            player = MediaPlayer.create(this, R.raw.ou);
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }

    public void stopPlayer (){
        if (player!=null){
            player.stop();
            player.reset();
            player.release();
            player=null;
        }
    }

    @Override
    protected void onStop (){
        super.onStop();
        stopPlayer();
    }

}

hope i will get help from here. i just want to play voice each other button click in same time.


Solution

  • Before Init new Audio or Media call This Method In Your

    public void sorea (View v)
    {
        Button button = (Button)findViewById(R.id.Sorea);
        final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
        button.startAnimation(myAnim);
        if (player == null){
    
      //Call the StopPlaying() to release player
      stopPlaying();
      player = MediaPlayer.create(this, R.raw.sorea);
      player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayer();
                }
            });
        }
        player.start();
    }
    
     private void stopPlaying() {
            if (player != null) {
                player.stop();
                player.release();
                player= null;
           }
        }