2017-10-22 7 views
0

私はアンドロイドに新しいです私は3つのイメージボタンを持っているレイアウトを持っているアプリケーションを作成しています私はそれが同じサウンドが繰り返されないすべての画像に対してイメージの複数のイメージボタンの使用

画像ボタンをクリックすると音が鳴ります。一時停止すると一時停止し、もう一度クリックすると一時停止します。

画像ボタンをクリックすると音が鳴ります。一時停止すると一時停止し、別の画像ボタンをクリックするとその画像に設定されている新しい音が開始されます。 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コード

パッケージ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(); 

    } 

} 
+0

私たちに見せることができるJavaコードがありますか? –

答えて

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

私はこのコードを試してみてください私の問題を解決するのを助けてください –

関連する問題