Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save udacityandroid/3a2fdbf1e487a292d199bebe51c2d4e6 to your computer and use it in GitHub Desktop.
Save udacityandroid/3a2fdbf1e487a292d199bebe51c2d4e6 to your computer and use it in GitHub Desktop.
Copy code from the NumbersActivity and paste it into the NumbersFragment
/** Handles playback of all the sound files */
private MediaPlayer mMediaPlayer;
/** Handles audio focus when playing a sound file */
private AudioManager mAudioManager;
/**
* This listener gets triggered whenever the audio focus changes
* (i.e., we gain or lose audio focus because of another app or device).
*/
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();
}
};
@charlae4ive
Copy link

Nice

@owolabiezekiel
Copy link

can some one please share the new NumbersActivity.java file and the NumberFragments.java file?

@NicReg
Copy link

NicReg commented Jun 22, 2017

Interesting the Fragment theme

@rebekaszucs
Copy link

good content! making our app look even better!

@mezubeen
Copy link

i am really excited :)

@ronaldmgdev
Copy link

Where do we exactly paste the codes above?
Do we delete all codes from the fragment java file or ? I didnt understand where to paste the codes?
Help?

@sarveshta
Copy link

@mezubeen me too!

@angwandi
Copy link

The Code is pasted straight after the imports classes

@ID2GO
Copy link

ID2GO commented Apr 28, 2018

Thnx @angwandi, I thought as much

@Pijuvwy
Copy link

Pijuvwy commented May 12, 2018

Paste it after:

public class NumbersFragment extends Fragment {

And before:

public NumbersFragment() {
      // Required empty public constructor
}

@muhamadaliibrahim
Copy link

دەستخۆش WELL DONE

@mixspark
Copy link

very nice

@SayedMuhammed
Copy link

done

@mixspark
Copy link

ok

@alfredtally45
Copy link

NumberFragments.java can be found here :) https://github.com/udacity/ud839_Miwok/blob/d7effcef3bf7fdccdd045c974d67abd5b960fae7/app/src/main/java/com/example/android/miwok/NumbersFragment.java

@evii
Thanks very much for the code

you referecnecd category_numbers but cannot be found in the colors directory,please help
"WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_numbers);"

@xMagicXs
Copy link

Paste it after:

public class NumbersFragment extends Fragment {

And before:

public NumbersFragment() {
      // Required empty public constructor
}

thank you :)

@FTW-Khushal
Copy link

I was here

@emu-code
Copy link

emu-code commented Oct 3, 2020

Great how this helps many more over the years

@SabalNiroula
Copy link

@SabalNiroula
Copy link

@vicky4421
Copy link

vicky4421 commented Dec 29, 2022

I am getting null pointer exception on adapter reference of WordAdapter class which we created when calling method ListView.setAdapter

WordAdapter itemsAdapter = new WordAdapter(getActivity(), words, R.color.category_numbers);
//ListView listView = findViewById(R.id.list);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(itemsAdapter);

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