diff --git a/app/src/main/java/com/example/android/miwok/ColorsActivity.java b/app/src/main/java/com/example/android/miwok/ColorsActivity.java index f545306dc..3f2f4c0c8 100644 --- a/app/src/main/java/com/example/android/miwok/ColorsActivity.java +++ b/app/src/main/java/com/example/android/miwok/ColorsActivity.java @@ -15,13 +15,25 @@ */ package com.example.android.miwok; +import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; import android.widget.ListView; import java.util.ArrayList; public class ColorsActivity extends AppCompatActivity { + private MediaPlayer mMediaPlayer; + private ArrayList mWords; + + private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() { + @Override + public void onCompletion(MediaPlayer mediaPlayer) { + Helper.release(mediaPlayer); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { @@ -36,19 +48,33 @@ protected void onCreate(Bundle savedInstanceState) { // set the adapter on listview listView.setAdapter(wordAdapter); + + mWords = initialiseList(); +// configure onclick + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int position, long l) { + Word word = mWords.get(position); + Helper.release(mMediaPlayer); + mMediaPlayer = MediaPlayer.create(ColorsActivity.this, word.getAudioResourceId()); + mMediaPlayer.start(); + + mMediaPlayer.setOnCompletionListener(mListener); + } + }); } private ArrayList initialiseList() { ArrayList list = new ArrayList(); - list.add(new Word("red","weṭeṭṭi", R.drawable.color_red)); - list.add(new Word("green","chokokki", R.drawable.color_green)); - list.add(new Word("brown","ṭakaakki", R.drawable.color_brown)); - list.add(new Word("gray","ṭopoppi", R.drawable.color_gray)); - list.add(new Word("black","kululli", R.drawable.color_black)); - list.add(new Word("white","kelelli", R.drawable.color_white)); - list.add(new Word("dusty yellow","ṭopiisә", R.drawable.color_dusty_yellow)); - list.add(new Word("mustard yellow","chiwiiṭә", R.drawable.color_mustard_yellow)); + list.add(new Word("red","weṭeṭṭi", R.drawable.color_red, R.raw.color_red)); + list.add(new Word("green","chokokki", R.drawable.color_green, R.raw.color_green)); + list.add(new Word("brown","ṭakaakki", R.drawable.color_brown, R.raw.color_brown)); + list.add(new Word("gray","ṭopoppi", R.drawable.color_gray, R.raw.color_gray)); + list.add(new Word("black","kululli", R.drawable.color_black, R.raw.color_black)); + list.add(new Word("white","kelelli", R.drawable.color_white, R.raw.color_white)); + list.add(new Word("dusty yellow","ṭopiisә", R.drawable.color_dusty_yellow, R.raw.color_dusty_yellow)); + list.add(new Word("mustard yellow","chiwiiṭә", R.drawable.color_mustard_yellow, R.raw.color_mustard_yellow)); return list; } diff --git a/app/src/main/java/com/example/android/miwok/FamilyActivity.java b/app/src/main/java/com/example/android/miwok/FamilyActivity.java index 9ed421ff1..7afc70b1a 100644 --- a/app/src/main/java/com/example/android/miwok/FamilyActivity.java +++ b/app/src/main/java/com/example/android/miwok/FamilyActivity.java @@ -15,13 +15,25 @@ */ package com.example.android.miwok; +import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; import android.widget.ListView; import java.util.ArrayList; public class FamilyActivity extends AppCompatActivity { + private MediaPlayer mMediaPlayer; + private ArrayList mWords; + + private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() { + @Override + public void onCompletion(MediaPlayer mediaPlayer) { + Helper.release(mediaPlayer); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { @@ -30,25 +42,38 @@ protected void onCreate(Bundle savedInstanceState) { // get a reference to the listview ListView listView = (ListView) findViewById(R.id.list); + mWords = initialiseList(); // create the array adapter - WordAdapter wordAdapter = new WordAdapter(this, initialiseList(),R.color.category_family); + WordAdapter wordAdapter = new WordAdapter(this, mWords,R.color.category_family); // set the adapter on listview listView.setAdapter(wordAdapter); + +// configure onclick + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int position, long l) { + Word word = mWords.get(position); + Helper.release(mMediaPlayer); + mMediaPlayer = MediaPlayer.create(FamilyActivity.this, word.getAudioResourceId()); + mMediaPlayer.start(); + mMediaPlayer.setOnCompletionListener(mListener); + } + }); } private ArrayList initialiseList() { ArrayList list = new ArrayList(); - list.add(new Word("father","әpә", R.drawable.family_father)); - list.add(new Word("mother","әṭa", R.drawable.family_mother)); - list.add(new Word("son","angsi", R.drawable.family_son)); - list.add(new Word("daughter","tune", R.drawable.family_daughter)); - list.add(new Word("older brother","taachi", R.drawable.family_older_brother)); - list.add(new Word("younger brother","chalitti", R.drawable.family_younger_brother)); - list.add(new Word("older sister","teṭe", R.drawable.family_older_sister)); - list.add(new Word("younger sister","kolliti", R.drawable.family_younger_sister)); - list.add(new Word("grandmother","ama", R.drawable.family_grandmother)); - list.add(new Word("grandfather","paapa", R.drawable.family_grandfather)); + list.add(new Word("father","әpә", R.drawable.family_father, R.raw.family_father)); + list.add(new Word("mother","әṭa", R.drawable.family_mother, R.raw.family_mother)); + list.add(new Word("son","angsi", R.drawable.family_son, R.raw.family_son)); + list.add(new Word("daughter","tune", R.drawable.family_daughter, R.raw.family_daughter)); + list.add(new Word("older brother","taachi", R.drawable.family_older_brother, R.raw.family_older_brother)); + list.add(new Word("younger brother","chalitti", R.drawable.family_younger_brother, R.raw.family_younger_brother)); + list.add(new Word("older sister","teṭe", R.drawable.family_older_sister, R.raw.family_older_sister)); + list.add(new Word("younger sister","kolliti", R.drawable.family_younger_sister, R.raw.family_younger_sister)); + list.add(new Word("grandmother","ama", R.drawable.family_grandmother, R.raw.family_grandmother)); + list.add(new Word("grandfather","paapa", R.drawable.family_grandfather, R.raw.family_grandfather)); return list; } diff --git a/app/src/main/java/com/example/android/miwok/Helper.java b/app/src/main/java/com/example/android/miwok/Helper.java new file mode 100644 index 000000000..22624bfa3 --- /dev/null +++ b/app/src/main/java/com/example/android/miwok/Helper.java @@ -0,0 +1,26 @@ +package com.example.android.miwok; + +import android.media.MediaPlayer; + +/** + * Created by User on 25/03/2018. + */ + +public final class Helper { + public static void release(MediaPlayer mediaPlayer) { +/** + * Clean up the media player by releasing its resources. + */ + // If the media player is not null, then it may be currently playing a sound. + if (mediaPlayer != null) { + // Regardless of the current state of the media player, release its resources + // because we no longer need it. + mediaPlayer.release(); + + // Set the media player back to null. For our code, we've decided that + // setting the media player to null is an easy way to tell that the media player + // is not configured to play an audio file at the moment. + mediaPlayer = null; + } + } +} diff --git a/app/src/main/java/com/example/android/miwok/NumbersActivity.java b/app/src/main/java/com/example/android/miwok/NumbersActivity.java index c50102deb..65fac0753 100644 --- a/app/src/main/java/com/example/android/miwok/NumbersActivity.java +++ b/app/src/main/java/com/example/android/miwok/NumbersActivity.java @@ -30,6 +30,14 @@ public class NumbersActivity extends AppCompatActivity { private MediaPlayer mMediaPlayer; + private ArrayList mWords; + + private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() { + @Override + public void onCompletion(MediaPlayer mediaPlayer) { + Helper.release(mediaPlayer); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { @@ -39,8 +47,9 @@ protected void onCreate(Bundle savedInstanceState) { // get a reference to the listview ListView listView = (ListView) findViewById(R.id.list); + mWords = initialiseList(); // create the array adapter - WordAdapter wordAdapter = new WordAdapter(this, initialiseList(),R.color.category_numbers); + WordAdapter wordAdapter = new WordAdapter(this, mWords,R.color.category_numbers); // set the adapter on listview listView.setAdapter(wordAdapter); @@ -48,25 +57,28 @@ protected void onCreate(Bundle savedInstanceState) { // configure onclick listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override - public void onItemClick(AdapterView adapterView, View view, int i, long l) { - mMediaPlayer = MediaPlayer.create(NumbersActivity.this, R.raw.number_one); + public void onItemClick(AdapterView adapterView, View view, int position, long l) { + Word word = mWords.get(position); + Helper.release(mMediaPlayer); + mMediaPlayer = MediaPlayer.create(NumbersActivity.this, word.getAudioResourceId()); mMediaPlayer.start(); + mMediaPlayer.setOnCompletionListener(mListener); } }); } private ArrayList initialiseList() { - ArrayList words = new ArrayList(); - words.add(new Word("one","lutti", R.drawable.number_one)); - words.add(new Word("two","otiiko",R.drawable.number_two)); - words.add(new Word("three","tolookosu", R.drawable.number_three)); - words.add(new Word("four","oyyisa", R.drawable.number_four)); - words.add(new Word("five","massokka", R.drawable.number_five)); - words.add(new Word("six","temmokka", R.drawable.number_six)); - words.add(new Word("seven","kenekaku", R.drawable.number_seven)); - words.add(new Word("eight","kawinta", R.drawable.number_eight)); - words.add(new Word("nine","wo'e", R.drawable.number_nine)); - words.add(new Word("ten","na aacha", R.drawable.number_ten)); + final ArrayList words = new ArrayList(); + words.add(new Word("one","lutti", R.drawable.number_one, R.raw.number_one)); + words.add(new Word("two","otiiko",R.drawable.number_two, R.raw.number_two)); + words.add(new Word("three","tolookosu", R.drawable.number_three, R.raw.number_three)); + words.add(new Word("four","oyyisa", R.drawable.number_four, R.raw.number_four)); + words.add(new Word("five","massokka", R.drawable.number_five, R.raw.number_five)); + words.add(new Word("six","temmokka", R.drawable.number_six, R.raw.number_six)); + words.add(new Word("seven","kenekaku", R.drawable.number_seven, R.raw.number_seven)); + words.add(new Word("eight","kawinta", R.drawable.number_eight, R.raw.number_eight)); + words.add(new Word("nine","wo'e", R.drawable.number_nine, R.raw.number_nine)); + words.add(new Word("ten","na aacha", R.drawable.number_ten, R.raw.number_ten)); return words; } diff --git a/app/src/main/java/com/example/android/miwok/PhrasesActivity.java b/app/src/main/java/com/example/android/miwok/PhrasesActivity.java index eb7f42402..bb148123b 100644 --- a/app/src/main/java/com/example/android/miwok/PhrasesActivity.java +++ b/app/src/main/java/com/example/android/miwok/PhrasesActivity.java @@ -15,13 +15,25 @@ */ package com.example.android.miwok; +import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; import android.widget.ListView; import java.util.ArrayList; public class PhrasesActivity extends AppCompatActivity { + private MediaPlayer mMediaPlayer; + private ArrayList mWords; + + private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() { + @Override + public void onCompletion(MediaPlayer mediaPlayer) { + Helper.release(mediaPlayer); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { @@ -31,25 +43,38 @@ protected void onCreate(Bundle savedInstanceState) { // get a reference to the listview ListView listView = (ListView) findViewById(R.id.list); + mWords = initialiseList(); // create the array adapter - WordAdapter wordAdapter = new WordAdapter(this, initialiseList(), R.color.category_phrases); + WordAdapter wordAdapter = new WordAdapter(this, mWords, R.color.category_phrases); // set the adapter on listview listView.setAdapter(wordAdapter); + +// configure onclick + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int position, long l) { + Word word = mWords.get(position); + Helper.release(mMediaPlayer); + mMediaPlayer = MediaPlayer.create(PhrasesActivity.this, word.getAudioResourceId()); + mMediaPlayer.start(); + mMediaPlayer.setOnCompletionListener(mListener); + } + }); } private ArrayList initialiseList() { ArrayList list = new ArrayList(); - list.add(new Word("Where are you going?","minto wuksus")); - list.add(new Word("What is your name?","tinnә oyaase'nә")); - list.add(new Word("My name is...","oyaaset...")); - list.add(new Word("How are you feeling?","michәksәs?")); - list.add(new Word("I’m feeling good.","kuchi achit")); - list.add(new Word("Are you coming?","әәnәs'aa?")); - list.add(new Word("Yes, I’m coming.","hәә’ әәnәm")); - list.add(new Word("I’m coming.","әәnәm")); - list.add(new Word("Let’s go.","yoowutis")); - list.add(new Word("Come here.","әnni'nem")); + list.add(new Word("Where are you going?","minto wuksus", R.raw.phrase_where_are_you_going)); + list.add(new Word("What is your name?","tinnә oyaase'nә", R.raw.phrase_what_is_your_name)); + list.add(new Word("My name is...","oyaaset...", R.raw.phrase_my_name_is)); + list.add(new Word("How are you feeling?","michәksәs?", R.raw.phrase_how_are_you_feeling)); + list.add(new Word("I’m feeling good.","kuchi achit", R.raw.phrase_im_feeling_good)); + list.add(new Word("Are you coming?","әәnәs'aa?", R.raw.phrase_are_you_coming)); + list.add(new Word("Yes, I’m coming.","hәә’ әәnәm", R.raw.phrase_yes_im_coming)); + list.add(new Word("I’m coming.","әәnәm", R.raw.phrase_im_coming)); + list.add(new Word("Let’s go.","yoowutis", R.raw.phrase_lets_go)); + list.add(new Word("Come here.","әnni'nem", R.raw.phrase_come_here)); return list; } diff --git a/app/src/main/java/com/example/android/miwok/Word.java b/app/src/main/java/com/example/android/miwok/Word.java index 5784d838f..647c21843 100644 --- a/app/src/main/java/com/example/android/miwok/Word.java +++ b/app/src/main/java/com/example/android/miwok/Word.java @@ -6,6 +6,7 @@ public final class Word { + private int mAudioResourceId; private String mtDefaultTranslation; private String mtMiwokTranslation; private int mImageResourceId; @@ -13,15 +14,17 @@ public final class Word { private final int NO_IMAGE_PROVIDED = -1; - public Word(String defaultTranslation, String miwokTranslation){ + public Word(String defaultTranslation, String miwokTranslation, int audioResourceId){ this.mtDefaultTranslation = defaultTranslation; this.mtMiwokTranslation = miwokTranslation; + this.mAudioResourceId = audioResourceId; } - public Word(String defaultTranslation, String miwokTranslation, int imageResourceId){ + public Word(String defaultTranslation, String miwokTranslation, int imageResourceId, int audioResourceId){ this.mtDefaultTranslation = defaultTranslation; this.mtMiwokTranslation = miwokTranslation; this.mImageResourceId = imageResourceId; + this.mAudioResourceId = audioResourceId; } public String getDefaultTranslation(){ @@ -43,4 +46,8 @@ public boolean hasImage(){ public String ToString(){ return mtDefaultTranslation + " " + mtMiwokTranslation; } + + public int getAudioResourceId() { + return mAudioResourceId; + } }