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.

1633072366145.png


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 for images, B4XGifView for animated gifs and ExoPlayer / MediaView / VideoPlayer for videos.

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.
It supports images by default.

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_PUBLICNot mandatory but you should always add it
WebP imagesSMM_WEBPWebPimage/webpPay attention to the B4J instructions
Proper handling of Jpeg orientation attributeSMM_META (B4J only)B4J - see post #3image/jpeg
Debug messagesSMM_DEBUG
WebViewtext/*
Ico imagesSMM_IMAGE4J (B4J only)B4J - see post #8image/vnd.microsoft.icon
image/x-icon
Natively supported in B4A and B4i


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.

Start with the two examples. More information next week.

Updates

- 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: 104
  • SMM_Example2.zip
    186.3 KB · Views: 57
  • SimpleMediaManager.b4xlib
    19.3 KB · Views: 34
Last edited:

Erel

Administrator
Staff member
Licensed 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

Administrator
Staff member
Licensed 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: 30

Erel

Administrator
Staff member
Licensed 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

Administrator
Staff member
Licensed 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

Administrator
Staff member
Licensed 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

Administrator
Staff member
Licensed 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.
 
Status
Not open for further replies.
Top