Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd sound when image changes
    primarykey
    data
    text
    <p>This is my attempt: when the button is pressed a different random image appears(this part works). What I can't get to work is the other part I'd like it do to. Depending on which image appears will depend on what soundbite is played. i.e. if the image is a black car, there are 2 separate files that'll play; one that says black and the other will say car. Any help is greatly appreciated. Here's my code:</p> <pre><code>public class Black extends Activity { ImageView imageView; MediaPlayer mp; int sound=525,sound1=525; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.black); Button Button1 = (Button) findViewById(R.id.button1); imageView= (ImageView)findViewById(R.id.image1); Button1.setOnClickListener( new OnClickListener(){ public void onClick(View v){ onClickImageView(v); } }); } public void onClickImageView(View view) { int[] picture= { R.drawable.black,R.drawable.blackairplane,R.drawable.blackbear,R.drawable.blackcar,R.drawable.blackcircle, R.drawable.blackkite,R.drawable.blacksquare,R.drawable.blacktriangle}; Random r = new Random(); int n=r.nextInt(7); imageView.setImageResource(picture[n]); switch (picture[n]) { case R.drawable.black: sound = R.raw.sayblack; sound1 = R.raw.sayblack; break; case R.drawable.blackairplane: sound = R.raw.sayblack; sound1= R.raw.sayairplane; break; case R.drawable.blackbear: sound = R.raw.sayblack; sound1 = R.raw.saybear; break; case R.drawable.blackcar: sound = R.raw.sayblack; sound1= R.raw.saycar; break; case R.drawable.blackcircle: sound = R.raw.sayblack; sound1=R.raw.saycircle; break; case R.drawable.blackkite: sound = R.raw.sayblack; sound1 = + R.raw.saykite; break; case R.drawable.blacksquare: sound = R.raw.sayblack; sound1 = R.raw.saysquare; break; case R.drawable.blacktriangle: sound = R.raw.sayblack; sound1= R.raw.saytriangle; break; } if(mp!=null){ mp.release(); } if(sound!=0){ mp = MediaPlayer.create(getBaseContext(), sound); mp.start(); } mp.setOnCompletionListener (new MediaPlayer.OnCompletionListener(){ @Override public void onCompletion(MediaPlayer arg0) { // TODO Auto-generated method stub if(sound1!=0 &amp;&amp; sound!=0){ mp= MediaPlayer.create(getBaseContext(),sound1); mp.start(); sound=sound1=525; }} }); } } </code></pre> <p>and here's my .xml file:</p> <pre><code> &lt;Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="40dp" android:background="@color/white" android:text="@string/black" /&gt; &lt;ImageView android:id="@+id/image1" android:layout_width="320dp" android:layout_height="250dp" android:scaleType="fitXY" android:contentDescription="@string/black" /&gt; </code></pre> <p>... and this is the correct code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload