flutteraudioaudio-streamingcloud-storagejust-audio

Cloud storage provider for music streaming


As an intro, I'm developing an app with Flutter that has an audio section.

I would like to address two subjects.

  1. For the moment the audio is stored in the cloud, more specific using Firebase. The main problem is that the pricing is not very supportive when the bandwidth threshold is exceeded. Also, as I discovered, each song is downloaded completely when trying to play it. Therefore, it doesn't matter that I want play 10 seconds or 1 minute of a song, the same traffic is generated. I'm using just_audio package as audio library and I'm wondering if there is a solution to integrate a stream base solution that implies buffering.

  2. As I've seen in the debug logs, a HTTP request is sent every time a song (from the cloud) is requested to play. Now, my concerns are that I can't use just_audio for streaming. Is there a cloud solution that fulfills a good compromise between price and bandwidth, even if the song is downloaded entirely each time the play action is required? I'm taking in consideration to develop an offline mode for the audio section, so that each song could be played from the local memory. Even so, it must be a user option, not a by default feature.


Solution

    1. I found two solutions for cloud storage that offer a good price for data transfer. The first option is starting from 5$/mo, 1TB included and 0.01$ / GB for extra traffic. https://www.digitalocean.com/. The second one starts from 9EUR/mo, 1TB included and the cost for any extra traffic is 0.5EUR/TB.

    2. just-audio has support for HLS and MPEG-DASH. Therefore, for the server-side, a good solution is nginx with the rtmp module.

    Credits to: https://docs.peer5.com/guides/setting-up-hls-live-streaming-server-using-nginx/

    The setup is pretty much straight forward:

    git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
    
    sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
    
    wget http://nginx.org/download/nginx-1.19.0.tar.gz
    tar -xf nginx-1.19.0.tar.gz
    cd nginx-1.19.0
    
    ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
    sudo make install
    

    Then, for the configuration file follow the instructions accesing the above link.

    In order to obtain the .ts chunks, I used ffmpeg with the following command.

    ffmpeg -i example.mp3 -c:a libmp3lame -b:a 128k -map 0:0 -f segment -segment_time 10 -segment_list outputlist.m3u8 -segment_format mpegts output%03d.ts
    

    Move the output files in the song's folder from the configured hls_path.

    Because only https is supported (http is supported only for testing purpose, not recommended in production), the final step is to register a SSL certificate and for this I used Let's encrypt. https://letsencrypt.org/getting-started/ If you have ssh access, cerbot is a good option https://certbot.eff.org/.

    Finally everything can be tested directly from the app or via a HLS client: http://players.akamai.com/players/hlsjs

    Paste the link of the song in the akami player and it should work. Example: https://your_domain.com/hls/song_name/outputlist.m3u8