B4A Library [B4X] SimpleMediaManager (SMM) - framework for images, videos and more

Status
Not open for further replies.
This is a simple to use, cross platform library, that does many complex things under the hood.



The developer calls SetMedia with a placeholder panel and a url. The panel will show an image, video or animated gif (more formats will be added).
B4X:
'Show a remote image / video / animated gif in Panel1
MediaManager.SetMedia(Panel1, "https://...")
It uses B4XImageView (or ZoomImageView) for images, B4XGifView for animated gifs, ExoPlayer / MediaView / VideoPlayer for videos and WebView for html or text.

Under the hood features:
  • Maintains a cache of resources.
  • Automatically tracks and disposes unused resources.
  • Reuses media views and media resources.
  • Cancels ongoing http requests when they are no longer relevant.
  • Fade-in animation.
  • Loading and Error resources. For example, it is simple to show an animated gif while the main resource is loading.
  • Images are loaded asynchronously using BitmapsAsync.
  • External dependencies are optional.
  • And more...
It uses conditional symbols in order to avoid adding dependencies when a feature is not required.


Feature​
Conditional Symbol​
Dependencies​
Mime​
Comments​
ImagesNoneimage/*
VideosSMM_VIDEOB4A - ExoPlayer
B4J - MediaView
B4i - iUI8 (internal library)
video/*
Animated GifsSMM_GIFB4XGifViewimage/gifThis is also required for non-animated gifs
Cancellation of ongoing http requestsHU2_PUBLICNoneNot mandatory but you should always add it
WebP imagesSMM_WEBPWebPimage/webpPay attention to the B4J instructions.
Android 9+ supports animated WebP images.
Proper handling of Jpeg orientation attributeSMM_META (B4J only)B4J - see post #3image/jpegAlso works in B4A / B4i.
Debug messagesSMM_DEBUG
WebViewNonetext/*
Ico imagesSMM_IMAGE4J (B4J only)B4J - see post #8image/vnd.microsoft.icon
image/x-icon
Natively supported in B4A and B4i
ZoomImageViewSMM_ZOOMZoomImageViewSet with REQUEST_ZOOMIMAGEVIEW


Notes:
  • The placeholder panels are expected to be empty from other views. You can reuse a placeholder panel, don't remove the added views in that case.
  • SMM was built to work with B4XPages. It wasn't tested and not all features will work in non-B4XPages projects.
  • SMM is an internal library.

Start with the two examples. More information next week.

Updates

- v1.17 - New MaxImageFileSize property that sets the maximum size of image files that will be loaded. Default value is 8mb.
- v1.16 - Allows adding headers to the http requests. This is done with the new REQUEST_HEADERS extra field: https://www.b4x.com/android/forum/t...for-images-videos-and-more.134716/post-984079
- v1.15 - Adds support for HLS video streams (mime = application/vnd.apple.mpegurl). Supported on B4A and B4i.
- v1.12 - New DefaultRequestTimeout field. Default value is 30000 (=30 seconds).
- v1.11 - Fixes an issue where a corrupted image can crash the app.
- v1.10 - Adds support for animated WebP images on Android 9+.
- v1.09 - Fixes issue with missing layout file.
- v1.08 - Adds support for showing images with ZoomImageView instead of B4XImageView: https://www.b4x.com/android/forum/t...for-images-videos-and-more.134716/post-857972
- v1.07 - Adds a SMM_MediaReady event, that is raised after the media was loaded, the views created and the fade-in effect ended. It requires adding a special parameter to the request.
Many improvements to the cache management.
- v1.06 - Fixes an issue where the old WebView content was visible when a WebView was reused.
- v1.05 - Adds support for web sites and other text that can be displayed with WebView. Adds support for Ico images in B4J (it is natively supported in B4A and B4i).
- v1.04 - Fixes a bug introduced in v1.03. BitmapsAsync module was added to the library. BitmapsAsync library is no longer required.
- v1.03 - SetMediaFromFile - new method to load local resources. Don't confuse with AddLocalMedia, which is used to replace the default loading and error media.
- v1.02 - Adds support for WebP images.
- v1.01 - Adds support for handling JPEG orientation attributes. It requires some configuration in B4J. See post #3.
 

Attachments

  • SMM_Example1.zip
    180.8 KB · Views: 2,152
  • SMM_Example2.zip
    186.3 KB · Views: 2,080
  • SimpleMediaManager.b4xlib
    24.8 KB · Views: 421
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
v1.01 adds support for handling of JPEG orientations.

No additional instructions for B4A and B4i. Note that in B4A it will only be applied when running on Android 7+ (API 24+).
In B4J it requires another dependency.

B4J instructions:
1. Add to main module:
B4X:
#if SMM_META
#AdditionalJar: metadata-extractor-2.16.0
#AdditionalJar: xmpcore-6.1.11
#End If
2. Download the additional jars. (Projects: https://github.com/drewnoakes/metadata-extractor/ and https://github.com/adobe/XMP-Toolkit-SDK)
3. Add SMM_META to the build configuration.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
By default, SMM will show a white panel while the resource is loaded and a grey panel in cases of errors.
The error and loading medias can be configured:
B4X:
'set an "error" image.
MediaManager.AddLocalMedia(MediaManager.KEY_DEFAULT_ERROR, xui.LoadBitmap(File.DirAssets, "error.jpg"), "image/*")
'And change the background color:
MediaManager.DefaultErrorRequest.Extra.Put(MediaManager.REQUEST_BACKGROUND, xui.Color_White)

'set a loading gif. Note that is done with File.ReadBytes.
'Don't forget to add SMM_GIF to the build configuration, and add a reference to B4XGifView.
MediaManager.AddLocalMedia(MediaManager.KEY_DEFAULT_LOADING, File.ReadBytes(File.DirAssets, "loading.gif"), "image/gif")

The result is:

The attached example downloads jpeg images with different orientations. They should be displayed with the correct orientation (all upwards).
 

Attachments

  • Loading_EXIF.zip
    311.8 KB · Views: 812

Erel

B4X founder
Staff member
Licensed User
Longtime User
V1.02 released - adds support for WebP images.
I've added a table to the first post, which explains the various optional features and their dependencies.

If you haven't done it yet, download SMM_Example2 and run it in release mode (preferably in B4A or B4i). It is a good example of how simple it is to manage hundreds of images using SMM.

 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Version 1.03 adds support for loading local resources.
It is done with the new SetMediaFromFile method.
Note that the mime type must be set. You can use image/* for standard image (if you want to properly handle EXIF attributes with JPEG files then you need to set the mime type to image/jpeg for jpegs).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
- v1.05 - Adds support for web sites and other text that can be displayed with WebView. Adds support for Ico images in B4J (it is natively supported in B4A and B4i).

Support for Ico images in B4J depends on image4j: https://github.com/imcdonagh/image4j (LGPL license)
Instructions:
- Download jar and put in additional libraries folder: https://repo1.maven.org/maven2/org/jclarion/image4j/0.7/image4j-0.7.jar
- Add to main module:
B4X:
#if SMM_IMAGE4J
#AdditionalJar: image4j-0.7
#End If
- Add to build configuration: SMM_IMAGE4J

Example: https://www.b4x.com/android/forum/threads/smm-top-sites-example.134898
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
- v1.07 - Adds a SMM_MediaReady event, that is raised after the media was loaded, the views created and the fade-in effect ended.
This event is relevant when you need to do something with the media, other than displaying it.

It is not enabled by default. You should enable it with the MediaManager.REQUEST_CALLBACK parameter:
B4X:
MediaManager.SetMediaWithExtra(Panel1, "https://image.link.com", "", CreateMap(MediaManager.REQUEST_CALLBACK: Me))
Wait For (Panel1) SMM_MediaReady (Success As Boolean, Media As SMMedia)

Notes:
1. The callback (Me in this case) must be a class, such as a B4XPages page.
2. The target view is set as the Sender value. This allows using it as the "sender filter" parameter with Wait For.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
- v1.08 - Adds support for showing images with ZoomImageView instead of B4XImageView.

Instructions:
1. Add SMM_ZOOM to build configuration (Ctrl + B)
2. Add reference to ZoomImageView v1.05+: https://www.b4x.com/android/forum/t...-zoomable-pannable-image-view.119987/#content
3. Add REQUEST_ZOOMIMAGEVIEW to the relevant requests:

B4X:
MediaManager.SetMediaWithExtra(Panel1, "https://image.link", "", CreateMap(MediaManager.REQUEST_ZOOMIMAGEVIEW: True))
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
v1.10 - Adds support for animated WebP images on Android 9+.
Don't forget to add SMM_WEBP conditional symbol and reference to WebP library.
It will show a static image on Android 8- devices and B4J / B4i.

Example:
B4X:
MediaManager.SetMedia(Pane2, "https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp")
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
- v1.16 - Allows adding headers to the http requests. This is done with the new REQUEST_HEADERS extra field.
The value should be a Map with one or more headers.
B4X:
MediaManager.SetMediaWithExtra(Pane1, Url, "", CreateMap(MediaManager.REQUEST_HEADERS: CreateMap("User-Agent": "cool user")))

Note that SMM caches the metadata and media based on the url. Changing the headers will not have any effect if the url was already downloaded.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…