-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.example.android.miwok; | ||
|
||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.app.FragmentPagerAdapter; | ||
|
||
/** | ||
* {@link CategoryAdapter} is a {@link FragmentPagerAdapter} that can provide the layout for | ||
* each list item based on a data source which is a list of {@link Word} objects. | ||
*/ | ||
public class CategoryAdapter extends FragmentPagerAdapter { | ||
|
||
/** | ||
* Create a new {@link CategoryAdapter} object. | ||
* | ||
* @param fm is the fragment manager that will keep each fragment's state in the adapter | ||
* across swipes. | ||
*/ | ||
public CategoryAdapter(FragmentManager fm) { | ||
super(fm); | ||
} | ||
|
||
/** | ||
* Return the {@link Fragment} that should be displayed for the given page number. | ||
*/ | ||
@Override | ||
public Fragment getItem(int position) { | ||
if (position == 0) { | ||
return new NumbersFragment(); | ||
} else if (position == 1) { | ||
return new FamilyFragment(); | ||
} else if (position == 2) { | ||
return new ColorsFragment(); | ||
} else { | ||
return new PhrasesFragment(); | ||
} | ||
} | ||
|
||
/** | ||
* Return the total number of pages. | ||
*/ | ||
@Override | ||
public int getCount() { | ||
return 4; | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,51 +7,9 @@ | |
android:orientation="vertical" | ||
tools:context="com.example.android.miwok.MainActivity"> | ||
|
||
<!-- Numbers category --> | ||
<FrameLayout | ||
<android.support.v4.view.ViewPager | ||
android:id="@+id/viewpager" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="@color/category_numbers"> | ||
<TextView | ||
android:id="@+id/numbers" | ||
style="@style/CategoryStyle" | ||
android:background="?android:attr/selectableItemBackground" | ||
android:text="@string/category_numbers" /> | ||
</FrameLayout> | ||
android:layout_height="match_parent"/> | ||
|
||
<!-- Family category --> | ||
<FrameLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="@color/category_family"> | ||
<TextView | ||
android:id="@+id/family" | ||
style="@style/CategoryStyle" | ||
android:background="?android:attr/selectableItemBackground" | ||
android:text="@string/category_family" /> | ||
</FrameLayout> | ||
|
||
<!-- Colors category --> | ||
<FrameLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="@color/category_colors"> | ||
<TextView | ||
android:id="@+id/colors" | ||
style="@style/CategoryStyle" | ||
android:background="?android:attr/selectableItemBackground" | ||
android:text="@string/category_colors" /> | ||
</FrameLayout> | ||
|
||
<!-- Phrases category --> | ||
<FrameLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="@color/category_phrases"> | ||
<TextView | ||
android:id="@+id/phrases" | ||
style="@style/CategoryStyle" | ||
android:background="?android:attr/selectableItemBackground" | ||
android:text="@string/category_phrases" /> | ||
</FrameLayout> | ||
</LinearLayout> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sslaia
|
12 comments
on commit c80d8bd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also change the listener behavior when AUDIOFOCUS_LOSS_TRANSIENT and AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK. We have this code:
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
which pauses the reproduction and start from the beginning when we regain audiofocus.
But, if we tap an audio, change the tab and tap a second audio, the first one start to play again when the secon ends, but out of his fragment, which I don't like. So I change the code to:
releaseMediaPlayer();
which call the method to clean up the media player.
The final code looks like this:
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 || focusChange
== AudioManager.AUDIOFOCUS_LOSS) && mMediaPlayer != null) {
releaseMediaPlayer();
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mMediaPlayer.start();
}
}
};
If you want to change it, don't forget to do it in the 4 Fragments files, NumbersFragment, FamilyFragment, ColorsFragment and PhrasesFragment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
releaseMediaPlayer do not work correctlu in onStop method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohqnk32 Apparently when you transition to right from the first tab (or to left from the last one) the onPause method won't be called, but the onResume of the new fragment will, so what you can do, and it worked for me, is to override both onPause and onResume, releasing the player in both of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HjalmarBasile This makes sense but for some reason releaseMediaPlayer();
is not working at all for me with these fragments. I have overrided onStop, onPause, and onResume.
EDIT
Turns out ViewPager needs to keep fragments to the left and right of the current fragment so it doesn't slow down swiping!
To correctly implement releaseMediaPlayer() while swiping between fragments, check out this code:
@Override
public void setUserVisibleHint(boolean isVisible) {
super.setUserVisibleHint(isVisible);
Log.e("ColorsFragment", "setUserVisibleHint: isVisible = " + isVisible);
if (!isVisible) {
releaseMediaPlayer();
}
}
Add this code to each Fragment if you want to release while swiping! (Colors, Numbers, etc...)
For more info check out the documentation on setUserVisibleHint() here: https://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)
@HjalmarBasile @mohqnk32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WOW
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I solved the problem using ViewPager's OnPageChangeListener to release the MediaPlayer at each page change: it stops the audio file rather abruptly, but I think this behavior can also make sense since when you change tabs there's no real point in continuing to play the previous audio.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can only see my NumbersFragment and no other fragments y so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh Sorry i found fragments exist but no names on top please help!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HjalmarBasile This makes sense but for some reason
releaseMediaPlayer();
is not working at all for me with these fragments. I have overrided onStop, onPause, and onResume.EDIT
Turns out ViewPager needs to keep fragments to the left and right of the current fragment so it doesn't slow down swiping!To correctly implement releaseMediaPlayer() while swiping between fragments, check out this code:
@Override public void setUserVisibleHint(boolean isVisible) { super.setUserVisibleHint(isVisible); Log.e("ColorsFragment", "setUserVisibleHint: isVisible = " + isVisible); if (!isVisible) { releaseMediaPlayer(); } }
Add this code to each Fragment if you want to release while swiping! (Colors, Numbers, etc...)
For more info check out the documentation on setUserVisibleHint() here: https://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)
@HjalmarBasile @mohqnk32
Nice find!
Far better than changing functionality.
The other easy solution is to release mediaPlayer resources on every change in audioFocus except Gain in the OnAudioFocusChangeListener
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear... Android Studio recommends import androidx support instead of android support v4. But then my app is always crash.
If anyone did this kind of changes, can tell me what else did you change to run your app?
I changed my imports to:
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
from:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah Im facing the same error apparently changing the imports causes issue in activity_main.xml file in view in which viewpager is defined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if you change android.support.v4.view.ViewPager
to androidx.viewpager.widget.ViewPager
the app doesn't crash and works
after completeing all the change im having trouble with sound.