package com.example.android.miwok;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class ColorsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.word_list);

        // Create a list of words
        ArrayList<Word> words = new ArrayList<Word>();
        words.add(new Word("red", "weṭeṭṭi", R.drawable.color_red, R.raw.color_red));
        words.add(new Word("mustard yellow", "chiwiiṭә", R.drawable.color_mustard_yellow, R.raw.color_mustard_yellow));
        words.add(new Word("dusty yellow", "ṭopiisә", R.drawable.color_dusty_yellow, R.raw.color_dusty_yellow));
        words.add(new Word("green", "chokokki", R.drawable.color_green, R.raw.color_green));
        words.add(new Word("brown", "ṭakaakki", R.drawable.color_brown, R.raw.color_brown));
        words.add(new Word("gray", "ṭopoppi", R.drawable.color_gray, R.raw.color_gray));
        words.add(new Word("black", "kululli", R.drawable.color_black, R.raw.color_black));
        words.add(new Word("white", "kelelli", R.drawable.color_white, R.raw.color_white));

        // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
        // adapter knows how to create list items for each item in the list.
        WordAdapter adapter = new WordAdapter(this, words, R.color.category_colors);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // word_listyout file.
        ListView listView = (ListView) findViewById(R.id.list);

        // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
        // {@link ListView} will display list items for each {@link Word} in the list.
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(getApplicationContext(), "Playing sound", Toast.LENGTH_SHORT).show();

                if (i == 0) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_red);
                    mediaPlayer.start();
                }
                if (i == 1) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_mustard_yellow);
                    mediaPlayer.start();
                }
                if (i == 2) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_dusty_yellow);
                    mediaPlayer.start();
                }
                if (i == 3) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_green);
                    mediaPlayer.start();
                }
                if (i == 4) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_brown);
                    mediaPlayer.start();
                }
                if (i == 5) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_gray);
                    mediaPlayer.start();
                }
                if (i == 6) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_black);
                    mediaPlayer.start();
                }
                if (i == 7) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(ColorsActivity.this, R.raw.color_white);
                    mediaPlayer.start();
                }
            }
        });
    }
}


@@ -343,24 +343,421 @@
 Activity %7B%0A%0A
+    //Handles playback of all the sound files%0A    private MediaPlayer mediaPlayer;%0A%0A    //This listener gets triggered when the %7B@link MediaPlayer%7D has completed playing the audio file.%0A    //%0A    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() %7B%0A        @Override%0A        public void onCompletion(MediaPlayer mediaPlayer) %7B%0A        %7D%0A    %7D;%0A%0A
     @Overrid
@@ -754,24 +754,24 @@
   @Override%0A
-
     protecte
@@ -2729,17 +2729,54 @@
 ng l) %7B%0A
+                releaseMediaPlayer();
 %0A
-
         
@@ -4433,32 +4433,1092 @@
                %7D
+%0A%0A                //Setup a listener on the media player, so that we can stop and release the media player%0A                //once the sound has finished playing.%0A                mediaPlayer.setOnCompletionListener(mCompletionListener);%0A%0A            %7D%0A%0A            /**%0A             * Clean up the media player by releasing its resources.%0A             */%0A            private void releaseMediaPlayer() %7B%0A                // If the media player is not null, then it may be currently playing a sound.%0A                if (mediaPlayer != null) %7B%0A                    // Regardless of the current state of the media player, release its resources%0A                    // because we no longer need it.%0A                    mediaPlayer.release();%0A%0A                    // Set the media player back to null. For our code, we've decided that%0A                    // setting the media player to null is an easy way to tell that the media player%0A                    // is not configured to play an audio file at the moment.%0A                    mediaPlayer = null;%0A                %7D
 %0A            %7D%0A 
@@ -5534,11 +5534,9 @@
 ;%0A    %7D%0A
+
 %7D
-%0A%0A

Update to the NumbersActivity - Java file