How to retrieve audio information like length of audio file? Rails App
I am building a music streaming application using Cloudinary to store all uploaded audio files. 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%>
I want to be able 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!
-
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_analyticsPlease let me know if that helps and if there's anything else I can help with
Regards,
Stephen
-
Hi there,
Sorry for the late response.
You could use our upload method and others using Android SDK.
Please review the following guide:
https://cloudinary.com/documentation/android_image_and_video_uploadBest Regards,
Wissam
Please sign in to leave a comment.
Comments
5 comments