B4A Library [Lib] MediaBrowser

Hello,

This library lists the media content (audio, image and video) of the MediaStore (a database containing meta data for all available media on both internal and external storage devices). It includes an audio player and a media scanner (to scan the new media and add them to the MediaStore).

I added new functions to the library esAudioMediaBrowser provided by Lagore, improved a bit the existing ones, and extended the stuff to images and videos.
The demo (a media gallery) needs the libs Reflection & StringUtils. It displays only files stored on your external SD card (but the lib can handle internal files).

This lib does not work with Android versions < Gingerbread (2.3).

List of properties and methods:
  • GetAudioFieldByID (External As Boolean, ID As Long, Field As String) As String
    Returns a field from the MediaStore about the specified audio file.
    Examples of field: FIELD_COMPOSER, FIELD_DATE_MODIFIED, FIELD_MIME_TYPE, FIELD_SIZE...
  • GetAudioFileInfo (Path As String, File As String) As Map
    Returns a Map containing info from the MediaStore about an audio file.
    The fields that are returned are:
    "ID"
    "Title"
    "Album"
    "Artist"
    "Track"
    "Year"
    "Location"
    "DisplayName"
    "Duration" (in ms)
  • GetAudioFileInfoByID (External As Boolean, ID As Long) As Map
    Returns a Map containing info from the MediaStore about an audio file.
    The fields that are returned are:
    "ID"
    "Title"
    "Album"
    "Artist"
    "Track"
    "Year"
    "Location"
    "DisplayName"
    "Duration" (in ms)
  • GetExifAttribute (Location As String, Attribute As String) As String
    Returns the requested attribute from the Exif data of the specified image.
    Location = complete path name for the file.
    The complete list of attributes can be found here: https://developer.android.com/reference/android/media/ExifInterface
  • GetExifLatLong (Location As String, LatLong As Float()) As Boolean
    Stores the latitude and longitude value of the specified image in a float array. Returns False if these Exif data are not available.
    Location = complete path name for the file.
  • GetExtImageFileInfo (Location As String) As Map
    Returns a Map containing info from the MediaStore about an external image file.
    Location = complete path name for the file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Height" (with Android 3.0+)
    "Width" (with Android 3.0+)
    "Size" (in bytes)
  • GetExtVideoFileInfo (Location As String) As Map
    Returns a Map containing info from the MediaStore about an external video file.
    Location = complete path name for the file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Resolution" (can be Null for some files)
    "Size" (in bytes)
  • GetImageDimensions (Location As String) As Map
    Returns a map containing width and height of the specified file.
    Location = complete path name for the file.
  • GetImageFieldByID (External As Boolean, ID As Long, Field As String) As String
    Returns a field from the MediaStore about the specified image file.
    Examples of field: FIELD_DATE_MODIFIED, FIELD_MIME_TYPE, FIELD_ORIENTATION...
  • GetImageFileInfoByID (External As Boolean, ID As Long) As Map
    Returns a Map containing info from the MediaStore about an image file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Height" (with Android 3.0+)
    "Width" (with Android 3.0+)
    "Size" (in bytes)
  • GetImgThumbnailByID (ID As Long, Mini As Boolean) As android.graphics.Bitmap
    Returns the micro thumbnail (96x96) or mini thumbnail (512x384) of an image file (it is generated if it doesn't exist).
  • GetMediaAudioList (External As Boolean, SortCol As String) As Map
    Returns a Map containing a list of all Audio files in the MediaStore which can be sorted.
    The allowed fields for sorting are:
    Null (= FIELD_ID)
    FIELD_ALBUM
    FIELD_ARTIST
    FIELD_COMPOSER
    FIELD_DATA (location)
    FIELD_DATE_ADDED
    FIELD_DATE_MODIFIED
    FIELD_DISPLAY_NAME
    FIELD_DURATION
    FIELD_MIME_TYPE
    FIELD_SIZE
    FIELD_TITLE
    FIELD_TRACK
    FIELD_YEAR
    The fields that are returned are:
    "ID"
    "Title"
    "Album"
    "Artist"
    "Track"
    "Year"
    "Location"
    "DisplayName"
    "Duration" (in ms)
  • GetMediaImageList (External As Boolean, SortCol As String) As Map
    Returns a Map containing a list of all Image files in the MediaStore which can be sorted.
    The allowed fields for sorting are:
    Null (= FIELD_ID)
    FIELD_DATA (location)
    FIELD_DATE_ADDED
    FIELD_DATE_MODIFIED
    FIELD_DATE_TAKEN
    FIELD_DISPLAY_NAME
    FIELD_HEIGHT
    FIELD_MIME_TYPE
    FIELD_ORIENTATION
    FIELD_SIZE
    FIELD_TITLE
    FIELD_WIDTH
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Height" (with Android 3.0+)
    "Width" (with Android 3.0+)
    "Size" (in bytes)
  • GetMediaVideoList (External As Boolean, SortCol As String) As Map
    Returns a Map containing a list of all Video files in the MediaStore which can be sorted.
    The allowed fields for sorting are:
    Null (= FIELD_ID)
    FIELD_DATA (location)
    FIELD_DATE_ADDED
    FIELD_DATE_MODIFIED
    FIELD_DATE_TAKEN
    FIELD_DISPLAY_NAME
    FIELD_MIME_TYPE
    FIELD_RESOLUTION
    FIELD_SIZE
    FIELD_TITLE
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Resolution" (can be Null for some files)
    "Size" (in bytes)
  • GetVideoFieldByID (External As Boolean, ID As Long, Field As String) As String
    Returns a field from the MediaStore about the specified video file.
    Examples of field: FIELD_DATE_ADDED, FIELD_DATE_MODIFIED, FIELD_MIME_TYPE...
  • GetVideoFileInfoByID (External As Boolean, ID As Long) As Map
    Returns a Map containing info from the MediaStore about a video file.
    The fields that are returned are:
    "ID"
    "Location"
    "DisplayName"
    "DateTaken" (in ticks from 1970)
    "Resolution" (can be Null for some files)
    "Size" (in bytes)
  • GetVideoThumbnailByID (ID As Long, Mini As Boolean) As android.graphics.Bitmap
    Returns the micro thumbnail (96x96) or mini thumbnail (512x384) of a video file (it is generated if it doesn't exist).
    There's a known bug on some Android versions with micro thumbnails:
    If the micro thumbnail can't be generated (e.g. video file format not recognized), the result is either null or a random thumbnail. Thus, for video files, it is strongly recommended to check if the mini thumbnail is initialized before trying to get the micro thumbnail.
  • Initialize (EventName As String)
  • MediaAudioPlay (External As Boolean, ID As Int)
    Plays an audio file.
    Raises the "MediaCompleted" event when the file end is reached.
  • MediaIsLooping As Boolean
    Checks whether the player is looping or non-looping.
  • MediaIsPlaying As Boolean
  • MediaLength As Int
    Returns the duration in ms.
  • MediaPause
  • MediaPosition As Int
    Gets the current playback position.
  • MediaResume
  • MediaSeek (position As Int)
    Seeks to specified time position (in ms).
    Raises the event "SeekCompleted" when the position change has been completed.
  • MediaSetLooping (Looping As Boolean)
    Sets the player to be looping or non-looping.
  • MediaSetVolume (leftVolume As Float, rightVolume As Float)
    Sets the volume on this player (and this player only).
    Note that the passed volume values are raw scalars. UI controls should be scaled logarithmically.
    This function must be called AFTER MediaAudioPlay.
  • MediaStop
    Stops the playback and releases the allocated resources.
  • ScanNewMedia (Paths As String())
    Requests the media scanner to scan the given files and add them to the MediaStore.
    Raises the "ScanCompleted" event after each file is scanned.
Warning:
There's a known bug on some Android versions with micro thumbnails:
If the micro thumbnail can't be generated (e.g. video file format not recognized), the result is either null or a random thumbnail. Thus, for video files, it is strongly recommended to check if the mini thumbnail is initialized before trying to get the micro thumbnail.


v1.2:
I fixed two bugs.

v1.21:
I removed the excessive log entries.
I removed all the Try/Catch that silently trapped errors.

v1.3:
This version requires Java 7 and B4A v3.82+.
I added 3 functions:
- GetImageFileInfoByID(External As Boolean, ID As Long) As Map;
- GetVideoFileInfoByID(External As Boolean, ID As Long) As Map;
- ScanNewMedia(Paths() As String).
I updated the demo so it works with recent Android versions.

v1.31:
I fixed a mistake in two functions introduced in v1.3.

v1.4:
I fixed an issue with API 30;
I added five functions: GetAudioFieldByID, GetImageFieldByID, GetVideoFieldByID, GetExifAttribute and GetExifLatLong;
I added a few FIELD constants;
I updated the demo to display the following fields: composer, orientation, latitude and longitude.

Enjoy,
Fred
 

Attachments

  • MediaBrowser v1.4.zip
    80.7 KB · Views: 1,044
Last edited:

Informatix

Expert
Licensed User
Longtime User
Version 1.1

Added functions:
- GetImageDimensions
- MediaIsLooping
- MediaSetLooping
- MediaSetVolume

Added event:
- SeekCompleted

I removed a few bugs in the demo app.

Warning:
There's a known bug on some Android versions with micro thumbnails:
If the micro thumbnail can't be generated (e.g. video file format not recognized), the result is either null or a random thumbnail. Thus, for video files, it is strongly recommended to check if the mini thumbnail is initialized before trying to get the micro thumbnail.

See post #1 for the files
 

mcmanu

Active Member
Licensed User
Longtime User
Hi

I get a Compiler error (objekt wurde nicht auf eine Objektinstanz festgelegt) i don´t know the english pronouncation of this error
 

mcmanu

Active Member
Licensed User
Longtime User
sorry

It was a stupid Fault of me xD i used basic4android 1.9
so the error message is not showing in b4a 2.0 :)
Thank you for the lib its fantastic :)
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
Using MediaBrowser with Internal Camera

Hi folks,

I have written a demo app using Fred's great mediabrowser library to solve a problem that has long vexed me. The attached app will launch Android's internal camera app, and then use the mediabrowser library to get the location of the image that was taken and load it to a panel on return.

One bug it has is if you open the camera to take a pic and click the back button, it will return whatever the last pic was. I'll post an update if I figure out a way to fix that.

EDIT: I came up with a solution for the back button being clicked. But if the user takes a picture and clicks cancel in the cam app, the pic taken still shows up in the panel.

Another issue is that the cam app launches in 1MP mode. Is that because of how the camera is launched?

Jack
 

Attachments

  • camtest.zip
    6 KB · Views: 652
Last edited:

Informatix

Expert
Licensed User
Longtime User
Hi folks,

I have written a demo app using Fred's great mediabrowser library to solve a problem that has long vexed me. The attached app will launch Android's internal camera app, and then use the mediabrowser library to get the location of the image that was taken and load it to a panel on return.

One bug it has is if you open the camera to take a pic and click the back button, it will return whatever the last pic was. I'll post an update if I figure out a way to fix that.

EDIT: I came up with a solution for the back button being clicked. But if the user takes a picture and clicks cancel in the cam app, the pic taken still shows up in the panel.

Another issue is that the cam app launches in 1MP mode. Is that because of how the camera is launched?

Jack

Thanks for this example.

If writing a lib does not scare you, you should write a little library to get the result of your intent. See the tutorial here.
 

PaulR

Active Member
Licensed User
Longtime User
Hi, thanks for this library.

I am encountering an issue with Activity_Resume when not calling MediaStop in Activity_Pause when playing audio. It appears that it is not possible to access the same instance of MediaBrowser so I cannot stop the audio that was playing.

This worked with the MediaPlayer library, so I'm at least 4% sure it's this library. :)
 

Informatix

Expert
Licensed User
Longtime User
Hi, thanks for this library.

I am encountering an issue with Activity_Resume when not calling MediaStop in Activity_Pause when playing audio. It appears that it is not possible to access the same instance of MediaBrowser so I cannot stop the audio that was playing.

This worked with the MediaPlayer library, so I'm at least 4% sure it's this library. :)

It's a problem with your code, in fact.

Try the following:
Dim MB as MediaBrowser (in Process_Globals)
if FirstTime then MB.Initialize("") (in Activity_Create)
Create a button to stop the music
Play a song with MB.MediaAudioPlay and change the device orientation. The music doesn't stop. Click on the button and it should stop.

Don't take the MB demo as a basis because it isn't designed for such an use.
 

PaulR

Active Member
Licensed User
Longtime User
Thanks, my issue was declaring it in Globals instead of Process_Globals.

:sign0161:
 

Flosch

Member
Licensed User
Longtime User
Volume Example

Hi I'm a little bit confused by the MediaSetVolume option.

Is the Maximum Value = 1 ?
And what is the best way to scale a seekbar logarithmically?

Thanks for your help.
 

hvkaniti

Member
Licensed User
Longtime User
Hi, Thanks for the awesome library. I am brand new to android app development and B4A. How can I get the album cover (image) of an audio file? Could you guide me please?
 

Informatix

Expert
Licensed User
Longtime User
Hi, Thanks for the awesome library. I am brand new to android app development and B4A. How can I get the album cover (image) of an audio file? Could you guide me please?

Is the cover image separated from the audio file (e.g. a jpeg in the folder) or embedded ? If separated, then you can retrieve it as an image file with the library. If not, you have to read the MP3 and decode the image inside. This is not natively made by Android.
 

hvkaniti

Member
Licensed User
Longtime User
Thank you very much for the reply. Actually I am trying to develop an application to make/modify mp3 albums. As you said, the existing mp3 file might have embedded artwork as well. So..I am still trying to figure out how to implement this with B4A. Please correct me if I am thinking something not feasible here.. Thanks in advance.
 

Informatix

Expert
Licensed User
Longtime User
So..I am still trying to figure out how to implement this with B4A. Please correct me if I am thinking something not feasible here.. Thanks in advance.
That shouldn't be too difficult (the MP3 and ID3 formats are well documented and there are plenty of samples on the web in various languages). I'm sure that can be written in plain Basic. Look at InputStream, ReadBytes, etc.
 
Top