Skip to content

Commit

Permalink
added media player to play sound for options with async
Browse files Browse the repository at this point in the history
  • Loading branch information
RMoha2021 committed Mar 25, 2018
1 parent b718d41 commit 99c33e5
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 46 deletions.
42 changes: 34 additions & 8 deletions app/src/main/java/com/example/android/miwok/ColorsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Word> mWords;

private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Helper.release(mediaPlayer);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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<Word> initialiseList() {
ArrayList<Word> list = new ArrayList<Word>();
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;
}
Expand Down
47 changes: 36 additions & 11 deletions app/src/main/java/com/example/android/miwok/FamilyActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Word> mWords;

private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Helper.release(mediaPlayer);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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<Word> initialiseList() {
ArrayList<Word> list = new ArrayList<Word>();
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;
}
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/com/example/android/miwok/Helper.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
40 changes: 26 additions & 14 deletions app/src/main/java/com/example/android/miwok/NumbersActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
public class NumbersActivity extends AppCompatActivity {

private MediaPlayer mMediaPlayer;
private ArrayList<Word> mWords;

private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Helper.release(mediaPlayer);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -39,34 +47,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_numbers);
WordAdapter wordAdapter = new WordAdapter(this, mWords,R.color.category_numbers);

// set the adapter on listview
listView.setAdapter(wordAdapter);

// 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<Word> initialiseList() {
ArrayList<Word> words = new ArrayList<Word>();
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<Word> words = new ArrayList<Word>();
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;
}
Expand Down
47 changes: 36 additions & 11 deletions app/src/main/java/com/example/android/miwok/PhrasesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Word> mWords;

private MediaPlayer.OnCompletionListener mListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Helper.release(mediaPlayer);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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<Word> initialiseList() {
ArrayList<Word> list = new ArrayList<Word>();
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;
}
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/example/android/miwok/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

public final class Word {

private int mAudioResourceId;
private String mtDefaultTranslation;
private String mtMiwokTranslation;
private int mImageResourceId;
private boolean mHasImage;
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(){
Expand All @@ -43,4 +46,8 @@ public boolean hasImage(){
public String ToString(){
return mtDefaultTranslation + " " + mtMiwokTranslation;
}

public int getAudioResourceId() {
return mAudioResourceId;
}
}

0 comments on commit 99c33e5

Please sign in to comment.