Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save udacityandroid/5808ce6e0c0b816bcf9a3125e8eeced0 to your computer and use it in GitHub Desktop.
Save udacityandroid/5808ce6e0c0b816bcf9a3125e8eeced0 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);
}
}
});
}
@FTW-Khushal
Copy link

`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.

             // Pause playback and reset player to the start of the file. That way, we can
             // play the word from the beginning when we resume playback.
             mMediaPlayer.pause();
             mMediaPlayer.seekTo(0);
         } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
             // The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback.
             mMediaPlayer.start();
         } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
             // The AUDIOFOCUS_LOSS case means we've lost audio focus and
             // Stop playback and clean up resources
             releaseMediaPlayer();
         }
     }
    

    };
    /**

    • 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);

     /** TODO: Insert all the code from the NumberActivity’s onCreate() method after the setContentView method call */
    

// getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    //Todo make an array name words

    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));

    // Create an {@link ArrayAdapter}, whose data source is a list of Strings. The
    // adapter knows how to create layouts for each item in the list, using the
    // simple_list_item_1.xml layout resource defined in the Android framework.
    // This list item layout contains a single {@link TextView}, which the adapter will set to
    // display a single word.

// LinearLayout ln = (LinearLayout) this.findViewById(R.id.linear_layout);
// ln.setBackground(Drawable.createFromPath(getResources().getString(R.color.category_numbers)));

    WordAdapter itemsAdapter = new WordAdapter(getActivity(),R.layout.list_item, 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 file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link ArrayAdapter} we created above, so that the
    // {@link ListView} will display list items for each word in the list of words.
    // Do this by calling the setAdapter method on the {@link ListView} object and pass in
    // 1 argument, which is the {@link ArrayAdapter} with the variable name itemsAdapter.
    listView.setAdapter(itemsAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            Word word = words.get(i);

            // 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.

                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                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);
            }
        }


    });

    return rootView;
}
private void releaseMediaPlayer() {
    // If the media player is not null, then it may be currently playing a sound.
    if (mMediaPlayer != null) {
        // Regardless of the current state of the media player, release its resources
        // because we no longer need it.
        mMediaPlayer.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.
        mMediaPlayer = null;

        // Regardless of whether or not we were granted audio focus, abandon it. This also
        // unregisters the AudioFocusChangeListener so we don't get anymore callbacks.
        mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
    }
}

@Override
public void onStop() {
    super.onStop();
    releaseMediaPlayer();
}

}
`

This is the which works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment