How to play music from a URL on Android?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Wed Apr 06, 2022 1:37 pm

This is my code:

Code: Select all

   if the environment = "mobile" then
      ## Create a native player:
      mobilecontrolcreate "player", "playSing"
      
      ## set more properies here...
      
      ## Set filename or URL for this player:
      mobilecontrolset "playSing","filename",_playMusic
      
      ## Finally start that thing:
      mobileControlDo "playSing", "play"
   else
      set the filename of player "sing" of cd "setting"to _playMusic
      start player "sing" of cd "setting" 
   end if
   

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to play music from a URL on Android?

Post by Klaus » Wed Apr 06, 2022 1:52 pm

OK, looks correct now.
Is _playMusis a httpS url?

Maybe you can check the result?

Code: Select all

...
mobilecontrolset "playSing","filename",_playMusic
if the result <> EMPTY then
  answer "Filename:" && the result
end if

## Finally start that thing:
mobileControlDo "playSing", "play"
if the result <> EMPTY then
  answer "Play:" && the result
end if
...
Not sure if that works on mobile.

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Wed Apr 06, 2022 5:29 pm

That's what he gave me
Filename:1
Play:13

what am I doing now?
This is my full code:
In this button the code:

Code: Select all

   PlayMusic "https://tcrvo.ml/12.mp3"
And stack:

Code: Select all

on PlayMusic _playMusic
   
   show widget "waitSing"
   show field "waitSing"
   if the environment = "mobile" then
      ## Create a native player:
      mobilecontrolcreate "player", "playSing"
      
      ## set more properies here...
      
      ## Set filename or URL for this player:
      mobilecontrolset "playSing","filename",_playMusic
      if the result <> EMPTY then
         answer "Filename:" && the result
      end if
      
      ## Finally start that thing:
      mobileControlDo "playSing", "play"
      if the result <> EMPTY then
         answer "Play:" && the result
      end if
   else
      set the filename of player "sing" of cd "setting"to _playMusic
      start player "sing" of cd "setting" 
   end if
   
   
   put url(_playMusic) into tVar
   if the result <> EMPTY then
      then
         answer "We're unable to play the song"
      end if
      hide widget "waitSing"
      hide field "waitSing"
      
   end PlayMusic

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to play music from a URL on Android?

Post by Klaus » Wed Apr 06, 2022 5:35 pm

Sorry, no idea, I was expecting something like "can't set filename" as the result or something more meaningful.
And no mention of this in the dcitionary. :cry:

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Wed Apr 06, 2022 5:40 pm

Maybe there are more ways?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7237
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to play music from a URL on Android?

Post by jacque » Wed Apr 06, 2022 9:24 pm

Looks like you need to add a few more properties. This works for me on Android:

Code: Select all

if the environment = "mobile" then
    ## Create a native player:
    mobilecontrolcreate "player", "playSing"
    
    ## set more properies here...
    mobilecontrolset "playSing","rect","10,10,200,50" --  adjust this to fit your app layout
    mobilecontrolset "playSing","visible",true
    mobilecontrolset "playSing","showController",true
    
    ## Set filename or URL for this player:
    mobilecontrolset "playSing","filename",_playMusic
    
    ## Finally start that thing:
    mobileControlDo "playSing", "play"
  else
    set the filename of player "sing" of cd "setting"to _playMusic
    start player "sing" of cd "setting" 
  end if
You can set the visible and showController to false if you don't want to see it. It appears that at least a rect is required. The default is to show the controller and the player. If you don't want the player to show, the actual rect isn't very important.

Also, make sure you have ticked the Internet checkbox in Android standalone settings. Also be sure you have Internet and (if available) TSNet selected in standalone inclusions.

When you're done with it, use mobileControlDelete to remove it from memory. That's required, since scripted mobile controls never go away until you quit the app or specifically remove them, and can interfere with other cards or controls.

I like the music. :)

Edit: You can delete the widget since you aren't using it. Also, the final "if" clause at the end of your handler is missing an "end if" and it has two "then"s. And typically there's a space between "url" and the url itself, though apparently the way you have it doesn't error. I do this:

Code: Select all

put url _playMusic into tVar
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Thu Apr 07, 2022 7:59 pm

This works the problem is that you have to wait 5 seconds for it to work

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to play music from a URL on Android?

Post by Klaus » Fri Apr 08, 2022 9:34 am

You could download the file when the app starts and then play it via "play file...".

Code: Select all

...
put "https://tcrvo.ml/12.mp3" into tURL
put specialfolderpath("documents") & "/12.mp3" into tFile
libURLDownloadToFile tURL,tFile
...
Check the dictionary for more options of that command.

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Fri Apr 08, 2022 9:56 am

Thank you.

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Fri Apr 08, 2022 10:56 am

Does it work on Android?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to play music from a URL on Android?

Post by Klaus » Fri Apr 08, 2022 1:33 pm

Why don't you just try?
You cell-phone will not explode, promised! 8)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7237
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to play music from a URL on Android?

Post by jacque » Fri Apr 08, 2022 5:59 pm

liveCode wrote:
Fri Apr 08, 2022 10:56 am
Does it work on Android?
It's supposed to, I've never used it. You could also set the URL of the native player when your app launches, and then only issue the play command when it's time to hear the music.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Sat Apr 09, 2022 6:47 pm

It works but there is a problem ...
I want this command to run the first time the user launches the app
The problem is that it takes a long time because I am loading 18 files
Then the user has to wait a long time
How can I make it happen in the background?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to play music from a URL on Android?

Post by Klaus » Sat Apr 09, 2022 7:02 pm

A possible solution:
1. when the app start check if (one of) the files are already in the user documents folder
2. If not start to download all files in a repeat loop, except one, that you
3. added to your standalone (Add files), so the first song can be played immediately when the app start, if that is neccessary.

libURLDownloadToFile tURL,tFile is asynchronous, means it will work in the background.

liveCode
Posts: 119
Joined: Sun Oct 17, 2021 5:53 pm

Re: How to play music from a URL on Android?

Post by liveCode » Sat Apr 09, 2022 7:05 pm

How do I check if one of the files is already found?

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”