Home » Android » Use of image multiple image button

Use of image multiple image button

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am new to android i am creating an application in which i have a layout which has three image button i want that when ever each button is clicked it play its own sound not the same sound is repeated for every image.

Another thing when i click an image button it produce sound if i want to pause it so it pause and on clicking again it start from where i pause.

Another thing when i click an image button it produce sound if i want to pause it so it pause and when i click another image button the new sound start which is set on that image.
XML CODE

<ScrollView 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:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="horizontal|none"
        tools:ignore="HardcodedText,ContentDescription" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="640dp"
        android:background="@drawable/main"
        tools:context=".Tanween" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="It means"
            android:textAppearance="?android:attr/textAppearanceLarge"
            tools:ignore="RtlHardcoded" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:text="1. Tanween-Fathatain"
            android:textAppearance="?android:attr/textAppearanceLarge"
            tools:ignore="RtlHardcoded" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_below="@+id/textView2"
            android:text="The sound"
            android:textAppearance="?android:attr/textAppearanceLarge"
            tools:ignore="RtlHardcoded" />

        <ImageButton
            android:id="@+id/imageButton24"
            android:layout_width="120sp"
            android:layout_height="60sp"
            android:src="@drawable/ta1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView3"
            android:layout_marginLeft="162dp"
            android:layout_marginStart="162dp"
            android:layout_marginTop="8dp"
             />

        <TextView
            android:id="@+id/textView38"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/imageButton24"
            android:layout_marginTop="10dp"
            android:text="2.Tanween-Kasratain"
            android:textSize="22sp"/>

        <TextView
            android:id="@+id/textView39"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView38"
            android:textSize="22sp"
            android:text="The sound" />

        <ImageButton
            android:id="@+id/imageButton28"
            android:layout_width="110sp"
            android:layout_height="65sp"
            android:layout_alignLeft="@+id/imageButton24"
            android:layout_alignStart="@+id/imageButton24"
            android:layout_below="@+id/textView39"
            android:layout_marginLeft="6dp"
            android:layout_marginStart="6dp"
            android:layout_marginTop="6dp"
            android:src="@drawable/ta2" />

        <TextView
            android:id="@+id/textView40"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/imageButton28"
            android:layout_marginTop="8dp"
            android:text="3.Tanween-Dammatain"
            android:textSize="22sp"/>

        <TextView
            android:id="@+id/textView41"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView40"
            android:text="The sound"
            android:textSize="22sp"/>

        <ImageButton
            android:id="@+id/imageButton29"
            android:layout_width="180sp"
            android:layout_height="60sp"
            android:src="@drawable/ta3"
            android:layout_below="@+id/textView41"
            android:layout_alignLeft="@+id/imageButton28"
            android:layout_alignStart="@+id/imageButton28" />
    </RelativeLayout>
 </ScrollView>

JAVA Code

package com.example.tajweedapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;

public class Tanween extends Activity {


    MediaPlayer mp = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tanween);

        mp= MediaPlayer.create(Tanween.this,R.raw.sound1);
        ImageButton b=(ImageButton) findViewById(R.id.imageButton24);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View V) {

                if(mp.isPlaying()){
                    mp.pause();
                }else
                {
                    mp.start();
                }
            }
        });

        mp= MediaPlayer.create(Tanween.this,R.raw.sound2);
        ImageButton a=(ImageButton) findViewById(R.id.imageButton28);

        a.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View V) {

                if(mp.isPlaying()){
                    mp.pause();
                }else
                {
                    mp.start();

                }
            }
        });

        mp= MediaPlayer.create(Tanween.this,R.raw.sound);
        ImageButton c=(ImageButton) findViewById(R.id.imageButton29);

        c.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View V) {

                if(mp.isPlaying()){
                mp.pause();
                }else
                {
                    mp.start();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tanween, menu);
        return true;
    }
    public void onPause(){
        super.onPause();
        mp.stop();
        mp.release();

    }

}
Answers:

I didn’t try the code myself but I think you can fix this by just using moving this mp= MediaPlayer.create(Tanween.this,R.raw.sound); code inside its corresponding button onClick listener.

b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View V) {

                mp= MediaPlayer.create(Tanween.this,R.raw.sound1);

                    if(mp.isPlaying()){
                        mp.pause();
                    }else
                    {
                        mp.start();
                    }
                }
            });

            ImageButton a=(ImageButton) findViewById(R.id.imageButton28);

            a.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View V) {
            mp= MediaPlayer.create(Tanween.this,R.raw.sound2);

                    if(mp.isPlaying()){
                        mp.pause();
                    }else
                    {
                        mp.start();

                    }
                }
            });

            ImageButton c=(ImageButton) findViewById(R.id.imageButton29);

            c.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View V) {

            mp= MediaPlayer.create(Tanween.this,R.raw.sound);

                    if(mp.isPlaying()){
                    mp.pause();
                    }else
                    {
                        mp.start();
                    }
                }
            });