Created
May 14, 2016 00:47
-
-
Save udacityandroid/5808ce6e0c0b816bcf9a3125e8eeced0 to your computer and use it in GitHub Desktop.
After fixing 4 errors in NumbersFragment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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
`package com.example.android.miwok;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
/**
A simple {@link Fragment} subclass.
*/
public class NumbersFragment extends Fragment {
private MediaPlayer mMediaPlayer;
private AudioManager mAudioManager;
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@OverRide
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// The AUDIOFOCUS_LOSS_TRANSIENT case means that we've lost audio focus for a
// short amount of time. The AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK case means that
// our app is allowed to continue playing sound but at a lower volume. We'll treat
// both cases the same way because our app is playing short sound files.
};
/**
This listener gets triggered when the {@link MediaPlayer} has completed
playing the audio file.
*/
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@OverRide
public void onCompletion(MediaPlayer mediaPlayer) {
// Now that the sound file has finished playing, release the media player resources.
releaseMediaPlayer();
}
};
public NumbersFragment() {
// Required empty public constructor
}
@OverRide
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list, container, false);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// LinearLayout ln = (LinearLayout) this.findViewById(R.id.linear_layout);
// ln.setBackground(Drawable.createFromPath(getResources().getString(R.color.category_numbers)));
}
`
This is the which works for me