How to attach and play sound file?

Hi Everyone,

I am not able to find a way to attach (copy and paste) a sound file into a web project and play it. There are lots of great examples where a file can be loaded with the filesystem that work good. Does anyone have a simple method to paste a mp3 file into the Xojo IDE and play it?

Thanks for your help :slight_smile:

[quote=119019:@Eugene Dakin]Hi Everyone,

I am not able to find a way to attach (copy and paste) a sound file into a web project and play it. There are lots of great examples where a file can be loaded with the filesystem that work good. Does anyone have a simple method to paste a mp3 file into the Xojo IDE and play it?[/quote]

The IDE won’t let you drag mp3 files into the project. You probably would need to paste some base64 instead and decode it before using. Strangely enough, m4a files can be added.

What I do not know is how to find what is the path of a resource once it has been added to the project, which would allow to address it.

If you are willing to place the mp3 on a URL, this could help through Page Source of with a WebSDK ControlWrapper :
http://stackoverflow.com/questions/6807464/embed-mp3-in-html

I forgot to mention WebMoviePlayer, but it too uses an URL 


Hi Michel,

Thanks for the helpful comments. I appreciate the help.

I have noticed that I am able to play sounds on a Desktop application, but sound doesn’t seem to work on a web application.

Dim s as Sound

Sound is not recognized. The error There is no class with this name appears over the word Sound .

Are there any other helpful thoughts? It would be nice to use Xojo native code, and if thats not possible then its Ok. Just as long as I know what the options are. :slight_smile:

[quote=119120:@Eugene Dakin]Hi Michel,

Thanks for the helpful comments. I appreciate the help.

I have noticed that I am able to play sounds on a Desktop application, but sound doesn’t seem to work on a web application.

Dim s as Sound

Sound is not recognized. The error There is no class with this name appears over the word Sound .

Are there any other helpful thoughts?[/quote]

On a desktop app, you tap directly in the hardware of the machine the software runs on. Hence you have sound, speak and beep available. Maybe the LR should mention these are not available for Web Apps.

On a web app, your app executes on a Linux or Windows machine, and is viewed through a distant browser on an unknown hardware, which can be anything from an iPhone to a Windows or Linux machine. Therefore the only way to play a sound is either to use HTML5 or the WebMovie player that uses Flash to support all machines.

The WebMoviePlayer is the simplest way to go ; if all you want is play music when a page is displayed, here is what you do :

  • Place a 1 pixel wide and 1 pixel high WebMoviePlayer on your WebPage
  • Set AutoPlay on
  • Set it to invisible
  • Upload your mp3 on a web site and make it the URL of your WebMoviePlayer in the inspector. Or you can grab the link on any public mp3 download site such as http://mp3skull.com/

Now when you display your page, the music plays.

If you want the user to be able to stop the music, you want to set the control larger and visible.

on a Web app you can use HTML5 Audio tag to play it.

it may still work like in this blog article:
http://www.mbsplugins.de/archive/2010-12-24/A_webapp_to_play_a_christmas_s

There is an updated version of the project included in MBS Plugins download.

[quote=119133:@Christian Schmitz]on a Web app you can use HTML5 Audio tag to play it.

it may still work like in this blog article:
http://www.mbsplugins.de/archive/2010-12-24/A_webapp_to_play_a_christmas_s[/quote]

Very nice Christian. Thank you.

Thanks for all of the helpful comments. Getting closer to working, but not quite yet. After having the mp3 file in the same directory as the CGI, the file is loaded but doesn’t seem to play. Here is the code so far.

[code] Dim f as FolderItem
#If DebugBuild and Not TargetMacOS then
f = GetFolderItem("").Parent.Child(“LakeWaves.mp3”)
#Else
f = GetFolderItem(“LakeWaves.mp3”)
#Endif

If f <> Nil then
If f.Exists Then
MoviePlayer1.AutoPlay = True
MoviePlayer1.Visible = False
MoviePlayer1.MobileWifiURL = f.URLPath
'MoviePlayer1.DesktopURL = f.URLPath
MoviePlayer1.Play
Else
MsgBox “File does not exist”
End If
Else
MsgBox “File is Nil”
End If[/code]

I used a copyfiles after the gear in OS X Build settings to place the file next to the executable, but not all mp3 seem to play. For some reason, this one seems more reliable :

http://www.mcrfb.com/files/commericals//Tv%20Commercials%20-%20Meow%20Mix%20Cat%20Food%20(Meow%20Meow%20Meow%20Meow).mp3

f = GetFolderItem("Meow.mp3")

Hi Michel,

That mp3 brings back good memories.

Thanks for you help and it doesn’t seem to work. I will try a couple of other alternatives at a later time.

Have a great weekend!

[quote=119189:@Eugene Dakin]Hi Michel,

That mp3 brings back good memories.

Thanks for you help and it doesn’t seem to work. I will try a couple of other alternatives at a later time.[/quote]

I tested it before posting :frowning:

At my first experiment I used the URL I posted in the IDE. Worked fine.

Afterward, I modified slightly your code with the copyfile, and used MoviePlayer1.DesktopURL = f.URLPath .

Maybe you can try Christian’s method too. It seems pretty simple to use.

Last minute : when using an http: address, it works flawlessly. Using f.URLPath does not work. Seems MoviePlayer does not cope well with local files.

You should probably be using the URL from WebFile.

Bob’s right. Check f.URLPath. It probably starts with “file://” instead of “http://”. FolderItem is local-machine centric. WebFile is browser centric.

Actually, I tried Webfile.url, and when run in the IDE, it still resorts to local. It probably works as http:// only when installed on the server.

Run locally, file:// should work. Are you saying it doesn’t?

No it does not. I verified the validity of the file:// URL, and yet it does not play. Actually the player shows the knob going slowly from left to right as if it reads fine, but no sound is heard. When with the same mp3 file an http:// is used, it plays fine. I guess since a web application is destined to be uploaded to a server anyway, it should be no problem to set up a url to the mp3. This affects Xojo Cloud as well, and I guess it will be necessary to find a similar workaround for it.

The problem probably comes from the Flash player MoviePlayer is based on.

Christian method has a big issue : the Page Source Source property cannot be set the way he does. So, another method has to be used.

I believe HTML5 should work but have not had time to try yet.

<embed src="Meow.mp3" height="50" width="100">

Hi Eugene!

I’ve been working on 60 web controls for the community as “donation-ware” (just donate a new code snippet for 1 of 3 packs of web controls
3 snippets for all 60, or one tutorial for all 60 in one shot) at Xojo Developer’s Spot, when they’re ready (will be announced).

I uploaded pre-release set 1 (all controls not finished and debugged), which does have a control called “XDSAudioPlayer” (finished).

Download: http://www.xojodevspot.com/demos/xdscontrols-set-1.zip

I left the Audio control unencrypted since it is completed for you to edit as seen fit.

It features controls or none, auto play or not, loop or no loop, and play/pause/stop/volume by invoking its methods. Can even “transcend” pages, so that it can play music in all pages from the initial page it began playing on. Try it out :slight_smile:

See it’s “Open” event in the demo to see how it loads the local MP3 and begins playing it


Hi Michel, Bob, and Tim - thanks for looking into this a little more. I agree that the file:// prefix should work, but doesn’t seem to in this application. Not sure if this is ‘working as expected’ or is this a bug?

Christian - Thank you for the kind code. I gave it a try and I am missing something (positive its me) and it looks like some of the code may need updating such as the binary method read type. It looks like Xojo has added the more types of Read (ReadUint32, ReadInt16, etc.). I am sure that it works and will require more tweaking on my part. :slight_smile:

Matthew, This example works great! I attempted to copy and paste the XDSAudioPlayer in a sample application and there appears to be a few errors. As an example, one of the errors is This item does not exist “element.innerHTML = '” + JavaScriptEscape(tag)+ “’;” Attached is the link to download the file. Simple Sound The attached file uses the same mp3 file.

The purpose of accessing a local sound file without internet access is to serve data in the oilfield when the internet is down - which is frequent.