如何在播放和停止音频时更改列表视图中的图像
how to change the image in a listview when an audio is playing and stopping
我有一个列表视图,其中有两个文本视图和一个图像视图,我还有一个与列表视图右侧对齐的播放图像。我想在播放音频时将播放图像更改为播放。 .并且在音频停止时也转回播放..
我创建了一个 boolean
isPlaying 来检查并将其设置为 true 和 false..但似乎什么也没有发生..
请指导...
以下是我的片段class..
public class FamilyFragment extends Fragment {
static boolean isActive = true;
private int lastSelectedPosition = -1;
/** Handles playback of all the sound files */
private MediaPlayer mMediaPlayer;
/** Handles audio focus when playing a sound file */
private AudioManager mAudioManager;
private ArrayList<Word> words = new ArrayList<Word>();
final WordAdapter adapter;
/**
* 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.
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
lastSelectedPosition = -1;
adapter.notifyDataSetChanged();
}
releaseMediaPlayer();
}
};
public FamilyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.word_list, container, false);
// Create and setup the {@link AudioManager} to request audio focus
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
// Create a list of words
words.add(new Word(R.string.family_father, R.string.miwok_family_father,
R.drawable.family_father,R.string.miwok_family_father, R.raw.family_father));
words.add(new Word(R.string.family_mother, R.string.miwok_family_mother,
R.drawable.family_mother,R.string.miwok_family_mother, R.raw.family_mother));
words.add(new Word(R.string.family_son, R.string.miwok_family_son,
R.drawable.family_son, R.string.miwok_family_son, R.raw.family_son));
words.add(new Word(R.string.family_daughter, R.string.miwok_family_daughter,
R.drawable.family_daughter,R.string.miwok_family_daughter, R.raw.family_daughter));
words.add(new Word(R.string.family_older_brother, R.string.miwok_family_older_brother,
R.drawable.family_older_brother, R.string.miwok_family_older_brother, R.raw.family_older_brother));
// 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.
adapter = new WordAdapter(getActivity(), words, R.color.category_family);
// 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.
final ListView listView = (ListView) rootView.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);
word.isPlaying = true;
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
}
lastSelectedPosition = position;
adapter.notifyDataSetChanged();
// 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(getActivity(), word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
word.isPlaying = true;
adapter.notifyDataSetChanged();
// 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;
}
@Override
public void onStart() {
super.onStart();
isActive = true;
}
@Override
public void onStop() {
super.onStop();
isActive = false;
// When the activity is stopped, release the media player resources because we won't
// be playing any more sounds.
releaseMediaPlayer();
}
/**
* Clean up the media player by releasing its resources.
*/
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);
}
}
}
并且在我的适配器中我有一个 if
else
将 boolean isPlaying
转换为 true
或 false
如下..
if (currentWord.isPlaying == true){
playIcon.setVisibility(View.INVISIBLE);
playIconPlaying.setVisibility(View.VISIBLE);
notifyDataSetChanged();
} else if (currentWord.isPlaying == false){
playIcon.setVisibility(View.VISIBLE);
playIconPlaying.setVisibility(View.INVISIBLE);
notifyDataSetChanged();
}
请提出合适的解决方案..
您的 Word class 应该有名为 isPlaying 的变量。它不应该在 class.
片段中
class Word{
boolean isPlaying;
//other variables
}
class yourFragment{
private int lastSelectedPosition = -1;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long l) {
// Add the below lines to the existing code. Toggle play/pause
Word word = words.get(position);
word.isPlaying = true;
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
}
lastSelectedPosition = position;
adapter.notifyDataSetChanged();
}
}
}
将适配器 getView 更改为
if (word.isPlaying == true){
playIcon.setVisibility(View.INVISIBLE);
playIconPlaying.setVisibility(View.VISIBLE);
} else if (word.isPlaying == false){
playIcon.setVisibility(View.VISIBLE);
playIconPlaying.setVisibility(View.INVISIBLE);
}
我有一个列表视图,其中有两个文本视图和一个图像视图,我还有一个与列表视图右侧对齐的播放图像。我想在播放音频时将播放图像更改为播放。 .并且在音频停止时也转回播放..
我创建了一个 boolean
isPlaying 来检查并将其设置为 true 和 false..但似乎什么也没有发生..
请指导...
以下是我的片段class..
public class FamilyFragment extends Fragment {
static boolean isActive = true;
private int lastSelectedPosition = -1;
/** Handles playback of all the sound files */
private MediaPlayer mMediaPlayer;
/** Handles audio focus when playing a sound file */
private AudioManager mAudioManager;
private ArrayList<Word> words = new ArrayList<Word>();
final WordAdapter adapter;
/**
* 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.
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
lastSelectedPosition = -1;
adapter.notifyDataSetChanged();
}
releaseMediaPlayer();
}
};
public FamilyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.word_list, container, false);
// Create and setup the {@link AudioManager} to request audio focus
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
// Create a list of words
words.add(new Word(R.string.family_father, R.string.miwok_family_father,
R.drawable.family_father,R.string.miwok_family_father, R.raw.family_father));
words.add(new Word(R.string.family_mother, R.string.miwok_family_mother,
R.drawable.family_mother,R.string.miwok_family_mother, R.raw.family_mother));
words.add(new Word(R.string.family_son, R.string.miwok_family_son,
R.drawable.family_son, R.string.miwok_family_son, R.raw.family_son));
words.add(new Word(R.string.family_daughter, R.string.miwok_family_daughter,
R.drawable.family_daughter,R.string.miwok_family_daughter, R.raw.family_daughter));
words.add(new Word(R.string.family_older_brother, R.string.miwok_family_older_brother,
R.drawable.family_older_brother, R.string.miwok_family_older_brother, R.raw.family_older_brother));
// 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.
adapter = new WordAdapter(getActivity(), words, R.color.category_family);
// 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.
final ListView listView = (ListView) rootView.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);
word.isPlaying = true;
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
}
lastSelectedPosition = position;
adapter.notifyDataSetChanged();
// 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(getActivity(), word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
word.isPlaying = true;
adapter.notifyDataSetChanged();
// 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;
}
@Override
public void onStart() {
super.onStart();
isActive = true;
}
@Override
public void onStop() {
super.onStop();
isActive = false;
// When the activity is stopped, release the media player resources because we won't
// be playing any more sounds.
releaseMediaPlayer();
}
/**
* Clean up the media player by releasing its resources.
*/
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);
}
}
}
并且在我的适配器中我有一个 if
else
将 boolean isPlaying
转换为 true
或 false
如下..
if (currentWord.isPlaying == true){
playIcon.setVisibility(View.INVISIBLE);
playIconPlaying.setVisibility(View.VISIBLE);
notifyDataSetChanged();
} else if (currentWord.isPlaying == false){
playIcon.setVisibility(View.VISIBLE);
playIconPlaying.setVisibility(View.INVISIBLE);
notifyDataSetChanged();
}
请提出合适的解决方案..
您的 Word class 应该有名为 isPlaying 的变量。它不应该在 class.
片段中class Word{
boolean isPlaying;
//other variables
}
class yourFragment{
private int lastSelectedPosition = -1;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long l) {
// Add the below lines to the existing code. Toggle play/pause
Word word = words.get(position);
word.isPlaying = true;
if(lastSelectedPosition != -1){
Word lastWord = words.get(lastSelectedPosition);
lastWord.isPlaying = false;
}
lastSelectedPosition = position;
adapter.notifyDataSetChanged();
}
}
}
将适配器 getView 更改为
if (word.isPlaying == true){
playIcon.setVisibility(View.INVISIBLE);
playIconPlaying.setVisibility(View.VISIBLE);
} else if (word.isPlaying == false){
playIcon.setVisibility(View.VISIBLE);
playIconPlaying.setVisibility(View.INVISIBLE);
}