I am building a music streaming application using Cloudinary to store all uploaded audio files. I have checked around but have not come across articles with any clear discussion about the topic: How to retrieve audio information like length of audio file from cloudinary file
Currently, Cloudinary does not have an audio tag so I am using the video helper like this:
<% if track.track.attached? %>
<%= cl_video_tag track.track.key,:source_types => [:mp3],:controls => true,:poster => false, class: "track-play play-track", id: "play-track"%>
<%end%>
It works well but I am stumped on how I would retrieve file information that might help to do various actions like:
- collect track duration of file
- switch to next track when current track duration complete
- display a custom media player based off of info from the cloudinary media player
Any help, thoughts or links would be appreciated!
Thanks again!
--- Additional info ---
The cloudinary video tag renders like this: Cloudinary Video Tag image
I use javascript and css to hide the cloudinary media player and trigger audio files when a play button is clicked. It looks like this Track Card Properly Displayed
UPDATE
Support answered my question to and this is what they provided. Hope that helps!
Hi Ilia,
When you upload a new asset and specify the 'image_metadata' parameter in the API call or Upload Preset, we'll include details of the newly-uploaded file in our Upload API response, including the duration of audio and video assets.
For example, for a random m4a file that I uploaded to my own account, the response with image_metadata=true, contains:
...
audio:
{ codec: 'aac',
bit_rate: '134511',
frequency: 48000,
channels: 2,
channel_layout: 'stereo' },
video: {},
is_audio: true,
bit_rate: 144577,
duration: 3.540146,
original_filename: 'test' }
You can also obtain that information for existing assets via the 'get resources' method of our Admin API, and use that to synchronise your own database.
If you'll need to refer to this information on the client-side, and can't store it in your own database, another option is to store the duration in the asset's 'context' metadata, the values of which are returned if you use our client-side asset list feature: https://cloudinary.com/documentation/advanced_url_delivery_options#client_side_asset_lists
In your case, you could add tag to all assets that are on the same playlist, then use the .json list for that playlist to get the asset details? Regarding how to move from one file to the next, I don't think there are helper methods for this in Rails specifically, but our video player does support an API for adding multiple files to a playlist, and sends events you can use to detect when a track completes so you change the contents of the playlist: https://cloudinary.com/documentation/video_player_playlists_recommendations https://cloudinary.com/documentation/video_player_events_analytics