Created
May 14, 2016 00:34
-
-
Save udacityandroid/3a2fdbf1e487a292d199bebe51c2d4e6 to your computer and use it in GitHub Desktop.
Copy code from the NumbersActivity and paste it into the NumbersFragment
This file contains 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
/** 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(); | |
} | |
}; |
NumberFragments.java can be found here :) https://github.com/udacity/ud839_Miwok/blob/d7effcef3bf7fdccdd045c974d67abd5b960fae7/app/src/main/java/com/example/android/miwok/NumbersFragment.java
Great how this helps many more over the years
many years
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
many