使用图像多个图像按钮

时间:2017-10-22 18:01:16

标签: android

我是Android的新手我正在创建一个应用程序,其中我有一个布局,其中有三个图像按钮,我希望当每个按钮被点击时,它播放自己的声音,不是每个图像重复相同的声音。

当我单击图像按钮时,如果我想暂停它以使其暂停并再次点击它会从我暂停的位置开始,它会产生声音。

当我单击图像按钮时,如果我想暂停它以使其暂停,当我单击另一个图像按钮时,它会产生声音,这是在该图像上设置的新声音开始。 XML代码

<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代码

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

    }

}

1 个答案:

答案 0 :(得分:0)

我自己没有尝试过代码,但我认为您可以通过在其相应的按钮onClick侦听器中移动此mp= MediaPlayer.create(Tanween.this,R.raw.sound);代码来解决此问题。

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