B4A Library [B4X][XUI] xImageSliderIG - Instagram style image and video slider

This Instagram style image and video slider is compatible with B4A and B4I.

xImageSliderIG
Author:
@Biswajit
Version: 2.02
Dependency:
  1. B4A: JavaObject, XUI, GestureDetector, AHViewPager(ver. 3), OkHttpUtils2, ExoPlayer
  2. B4I: iXUI, iHttpUtils2, iUI8
Features:
  1. Instagram like Image Counter
  2. Instagram like Dot Indicator
  3. Instagram like Pinch Zoom (only for image item)
  4. Cover blank space with blurry image/color
  5. Customizable Indicator color and animation duration
  6. Single Tap and Double Tap
  7. Fit / Fill / Stretch Image
  8. Lazy loading for both image and video
  9. Loading indicator
  10. Support both local and remote items
  11. Retry loading option for failed remote image
  12. Update item list on runtime
  13. Support video playback both local and remote (uri/dash/hls/smoothstream) (NEW)
  14. Auto-pause while going offscreen (NEW)
  15. Auto-resume while visible 85% (NEW)
  16. Autoplay and start muted video (NEW)
  17. Hide/Show (inside/outside) dot indicators (NEW)
  18. Support video scaling (Fill / Fit / Stretch) (NEW)
Method:
  • SetItems(imgs As List)
    Set/Update items as a list to the slider along with the value as a map you want to receive on tap events
    Image item map format:
    data: your data that will be returned on tap
    image: bitmap / url
    Video item map format:
    data: your data that will be returned on double tap
    video: url / path (combined)
    type: file/uri/dash/hls/smoothstream
  • SetZoomPanel(ZoomPanelContainer As B4XView)
    Set zoom panel (Usually Activity / Root panel) to enable the pinch zoom feature
  • ShowNext
    Show the next item
  • ShowPrev
    Show the previous item
  • PauseCurrentVideo
    Pause the current video item
  • PlayCurrentVideo
    Play the current video item
  • ToggleMute
    Toggle mute of the current video item
  • UpdatePlayback
    Update the current playback state
    Required to pause the playback on activity_pause / page_disappear
    And to resume the playback on activity_resume / page_appear
Property:
  • ActivityHasActionBar As Boolean
    Set it to true if the activity/page has an Action bar (b4a) / Navigation bar (B4I). Default is False. Only needed if the pinch zoom is enabled.
  • CurrentIndex As Int
Designer Property:
  • Show Indicator Dots: Show indicator dots. (OUTSIDE / INSIDE / HIDE) (Default: OUTSIDE)
  • Dots Color: Change the inactive dot indicator color. (Default: #CCCCCC)
  • Active Dot Color: Change the active dot color. (Default: #4286F4)
  • Scale Content: Slider item scale type. (FIT / FILL / STRETCH) (Default: FIT)
  • Cover Free Space: Show a blurry image around the slide image if there is any free space. Only applicable for image item and if FIT scale type is selected. (Default: TRUE)
  • Free Space Color: Show a solid color around the slide item if there is any free space. Only applicable if Cover Free Space is unchecked for image items. (Default: #F4F4F4)
  • Item Count: Show slide count label (Eg. 5/10) (Default: TRUE)
  • Dot Transition Duration: Dots transition time in milliseconds. (Default: 500)
  • Zoom Actual View: If checked the slide will be hidden while zooming (Like Instagram).
  • Autoplay Video: If checked the video will be autoplayed on loading completion. (Default: TRUE)
  • Start Video Muted: If checked the video will start muted on loading completion. (Default: TRUE)
Events:
  • PageChanged(CurrentIndex As Int)
  • VideoPlaying(CurrentIndex As Int)
  • VideoPaused(CurrentIndex As Int)
  • SingleTap(CurrentIndex As Int, Data As Object)
    NOTE: Only for image items. For video items single tap will toggle the video playback state.
  • DoubleTap(CurrentIndex As Int, Data As Object)

Usage:
  1. Add this view from the Designer.
  2. Copy the attached ximageslider_video layout file to your project if you want to show video items.
  3. Create a list having each item as a map. The map will contain a bitmap or URL and data (any object) (See Example). (For Image item)
  4. Create a list having each item as a map. The map will contain a video URL or Path, video type, and data (any object) (See Example). (For Video item)
  5. Set that list to the image slider.
  6. Set zoom panel (optional)
  7. On activity pause / page disappear event pause the current video playback (See Example)
  8. On activity resume / page appear event update the current video playback (See Example)
  9. If you are using multiple sliders in a clv, then call UpdatePlayback on ScrollChange event.
For multiple sliders with CLV check the attached examples.

Update 1.1:

  1. Added support for lazy loading
  2. Added support for remote image loading (Example attached)
  3. New loading indicator and tap to retry option for failed remote image
Update 1.20:
  1. Fixed crash issue in Android 28.
  2. Added support for updating the image list (Check updated example).
Update 1.21: Added new PageChanged event.
Update 1.22: Fixed a crashing issue while using as a single slider with no ActionBar.
Update 2.00:
  1. Fix item scaling issue
  2. Support video playback both local and remote (uri/dash/hls/smoothstream)
  3. Auto-pause while going offscreen
  4. Auto-resume while visible 85%
  5. Autoplay and start muted video
  6. Hide/Show (inside/outside) dot indicators
  7. Support image and video scaling (Fill / Fit / Stretch)
  8. Added new events VideoPlaying and VideoPaused
  9. Changed SetImages method name to SetItems
  10. More accurate blurry cover image.
  11. Now it depends upon ExoPlayer for B4A and iUI8 for B4i
Update 2.01:
  1. Fixed an issue of ShowNext function (B4A)
  2. Fixed double tap issue (B4i)
Update 2.02: Fixed a crashing issue with a slow network connection while playing video.
 

Attachments

  • ximageslider_video_layout.zip
    2.3 KB · Views: 1,055
  • xImageSliderIG_sample_b4a.zip
    169.6 KB · Views: 1,311
  • xImageSliderIG_sample_b4i.zip
    221.4 KB · Views: 1,035
  • xImageSliderIG.b4xlib
    9 KB · Views: 879
Last edited:

alfaiz678

Active Member
Licensed User
I have this problem
 

Attachments

  • بدون عنوان.jpg
    بدون عنوان.jpg
    54.9 KB · Views: 271

Biswajit

Active Member
Licensed User
Longtime User

MarcoRome

Expert
Licensed User
Longtime User
Hi @Biswajit
if you try with same movie example this:
sc.items.Add(CreateMap("data":"Item 1-"&j, "video":"https://www.xxxxx.com/caricafile/bosa/bosa.mp4", "type":"uri"))
The App crash with this error: "
ximagesliderig_vplayer_ready (java line: 1675)
java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to android.widget.ProgressBar"
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
ximagesliderig_vplayer_ready (java line: 1675)
java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to android.widget.ProgressBar
at b4a.example.ximagesliderig._vplayer_ready(ximagesliderig.java:1675)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.SimpleExoPlayerWrapper$1.onPlayerStateChanged(SimpleExoPlayerWrapper.java:96)
at com.google.android.exoplayer2.ExoPlayerImpl$PlaybackInfoUpdate.lambda$run$5$ExoPlayerImpl$PlaybackInfoUpdate(ExoPlayerImpl.java:828)
at com.google.android.exoplayer2.ExoPlayerImpl$PlaybackInfoUpdate$$Lambda$5.invokeListener(Unknown Source:2)
at com.google.android.exoplayer2.BasePlayer$ListenerHolder.invoke(BasePlayer.java:182)
at com.google.android.exoplayer2.ExoPlayerImpl.invokeAll(ExoPlayerImpl.java:845)
at com.google.android.exoplayer2.ExoPlayerImpl.access$000(ExoPlayerImpl.java:43)
at com.google.android.exoplayer2.ExoPlayerImpl$PlaybackInfoUpdate.run(ExoPlayerImpl.java:826)
at com.google.android.exoplayer2.ExoPlayerImpl.notifyListeners(ExoPlayerImpl.java:736)
at com.google.android.exoplayer2.ExoPlayerImpl.updatePlaybackInfo(ExoPlayerImpl.java:710)
at com.google.android.exoplayer2.ExoPlayerImpl.handlePlaybackInfo(ExoPlayerImpl.java:652)
at com.google.android.exoplayer2.ExoPlayerImpl.handleEvent(ExoPlayerImpl.java:595)
at com.google.android.exoplayer2.ExoPlayerImpl$1.handleMessage(ExoPlayerImpl.java:127)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Any idea ??
Thank you and have a nice Merry Christmas
Marco
 
Last edited:

Biswajit

Active Member
Licensed User
Longtime User
Hi @Biswajit
if you try with same movie example this:

The App crash with this error: "
ximagesliderig_vplayer_ready (java line: 1675)
java.lang.ClassCastException: anywheresoftware.b4a.BALayout cannot be cast to android.widget.ProgressBar"


Any idea ??
Thank you and have a nice Merry Christmas
Marco
Working fine here. Did you change any library code?
 

Biswajit

Active Member
Licensed User
Longtime User

lucasheer

Active Member
Licensed User
Longtime User
Hello!

For some reason, the videos are super crisp, but the images are getting blurred after being displayed. I'm thinking this is from resize?

Any suggestions on going about fixing this?

Thank you!
 

lucasheer

Active Member
Licensed User
Longtime User
I fixed the blurry issue on iOS for my case.

Changed
B4X:
iv.Bitmap = img.Resize(ImgCont.Width,ImgCont.Height,True)

to
B4X:
iv.Bitmap = img

Was going to convert ImageView to B4XImageView, but this worked for me. I compress/crop images to be relatively close to the view size on the backend.

thank you!
 

Biswajit

Active Member
Licensed User
Longtime User

MarcoRome

Expert
Licensed User
Longtime User
Hi @Biswajit
I noticed a problem that maybe you can give me the solution.
If the library is used on a second activity, the first time the activity is opened everything works fine, but when you switch to another activity and try to reopen the activity with the videos, they are not more loaded and give the following error.


The project test is here:
 

Biswajit

Active Member
Licensed User
Longtime User
Hi @Biswajit
I noticed a problem that maybe you can give me the solution.
If the library is used on a second activity, the first time the activity is opened everything works fine, but when you switch to another activity and try to reopen the activity with the videos, they are not more loaded and give the following error.


The project test is here:
If you check the log you will see this is happening due to limited numbers of hardware accelerated video decoders. Some devices have a limited number of hardware accelerated video decoders available. In my device its 12. If I create more than 12 exoplayers then its throwing the decoder initialization failed issue. The video players needs to be recycled when its off-screen. But for that a lots of changes needs to be done and I am busy until next week. After that I will try to fix it.

For now use the attached library and change the Activity_pause event to,
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    For Each sc As SliderContainer In sliders
        sc.slider.PauseCurrentVideo
        If UserClosed Then sc.slider.ReleaseVideoPlayers ''<- add this line
    Next
End Sub

Also when you are using too many video players its better to add the following code to your manifest to avoid OOM issue.
B4X:
SetApplicationAttribute(android:largeHeap, "true")
 

Attachments

  • xImageSliderIG.b4xlib
    9 KB · Views: 227

MarcoRome

Expert
Licensed User
Longtime User
Also when you are using too many video players its better to add the following code to your manifest to avoid OOM issue.
B4X:
SetApplicationAttribute(android:largeHeap, "true")
This i had already added

If you check the log you will see this is happening due to limited numbers of hardware accelerated video decoders. Some devices have a limited number of hardware accelerated video decoders available. In my device its 12. If I create more than 12 exoplayers then its throwing the decoder initialization failed issue. The video players needs to be recycled when its off-screen. But for that a lots of changes needs to be done and I am busy until next week. After that I will try to fix it.
Yes, that's exactly the problem.
With the method you added, the problem was solved 👍 👍 👍


Thanks i sent you a donation for your valuable support !!
 

MarcoRome

Expert
Licensed User
Longtime User
It would be very useful, in the new version to also add the Complete event (when the movie ends)
Thank you again
 
Top