Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sakibguy/6b10dd0e950a3d6e7387a2b8fe0861bd to your computer and use it in GitHub Desktop.
Save sakibguy/6b10dd0e950a3d6e7387a2b8fe0861bd to your computer and use it in GitHub Desktop.
After fixing 4 errors in NumbersFragment.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// Create and setup the {@link AudioManager} to request audio focus
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Create a list of words
final ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word(R.string.number_one, R.string.miwok_number_one,
R.drawable.number_one, R.raw.number_one));
words.add(new Word(R.string.number_two, R.string.miwok_number_two,
R.drawable.number_two, R.raw.number_two));
words.add(new Word(R.string.number_three, R.string.miwok_number_three,
R.drawable.number_three, R.raw.number_three));
words.add(new Word(R.string.number_four, R.string.miwok_number_four,
R.drawable.number_four, R.raw.number_four));
words.add(new Word(R.string.number_five, R.string.miwok_number_five,
R.drawable.number_five, R.raw.number_five));
words.add(new Word(R.string.number_six, R.string.miwok_number_six,
R.drawable.number_six, R.raw.number_six));
words.add(new Word(R.string.number_seven, R.string.miwok_number_seven,
R.drawable.number_seven, R.raw.number_seven));
words.add(new Word(R.string.number_eight, R.string.miwok_number_eight,
R.drawable.number_eight, R.raw.number_eight));
words.add(new Word(R.string.number_nine, R.string.miwok_number_nine,
R.drawable.number_nine, R.raw.number_nine));
words.add(new Word(R.string.number_ten, R.string.miwok_number_ten,
R.drawable.number_ten, R.raw.number_ten));
// 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_numbers);
// 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_list.xml layout 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);
// Set a click listener to play the audio when the list item is clicked on
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Release the media player if it currently exists because we are about to
// play a different sound file
releaseMediaPlayer();
// Get the {@link Word} object at the given position the user clicked on
Word word = words.get(position);
// Request audio focus so in order to play the audio file. The app needs to play a
// short audio file, so we will request audio focus with a short amount of time
// with AUDIOFOCUS_GAIN_TRANSIENT.
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// We have audio focus now.
// Create and setup the {@link MediaPlayer} for the audio resource associated
// with the current word
mMediaPlayer = MediaPlayer.create(NumbersActivity.this, word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
// Setup a listener on the media player, so that we can stop and release the
// media player once the sound has finished playing.
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment