Playing MP3 Over the Web

I’m trying to build a small control that will allow playing of an MP3 from a remote server.
Setting the URL works great, but there are a couple challenges.

  1. it auto-plays and I don’t want it to.
  2. The background of the control is black. Is there a way to override that?
  3. When I navigate off the page by tapping the back “button”, it keeps playing. I’d like it to stop, but I can’t see a way to unload or stop the HTML control.

Any recommendations?

Is this an iOS project or a Web project?

IOS

Ah, so you’ve got this audio player in an HTMLViewer.

In that case, the audio control is the safari native control. You can control whether or not it auto-plays by setting the autoplay html attribute to “false”. In terms of the style, usually what people do is hide the control and make their own interface.

Greg, I’m missing something (probably obvious). Where does one set the autoplay html attribute?
Is there a better control that I have access to to play this?

[quote=430221:@Chris Halford]Greg, I’m missing something (probably obvious). Where does one set the autoplay html attribute?
Is there a better control that I have access to to play this?[/quote]
Oh, I get it now… you are just pointing the htmlviewer at the mp3 and getting the automatic behavior.

Ok, so what’s happening is it’s presenting you with the default control. If you want any amount of control, you’ll need to create an html audio control in an html file and load that.

This is untested, but should be a way to play an mp3 file over the internet using iOSKit (stolen from https://stackoverflow.com/questions/34563329/how-to-play-mp3-audio-from-url-in-ios-swift):
Add two properties, “player” as AVFoundation.AVAudioPlayer and “mBlock” as iOSBlock.

Add the following to kick off the download for playing:

[code]dim url_string as Text = “https://mywebsite.com/greatsong.mp3
dim url as Foundation.NSURL = Foundation.NSURL.URLWithString(url_string)

declare Function sharedSession lib FoundationLib selector “sharedSession” (cls as Ptr) as ptr
Declare Function downloadTask lib FoundationLib selector “downloadTaskWithURL:completionHandler:” _
(obj_id as ptr, url as ptr, compHandler as ptr) as ptr
Declare sub resume lib FoundationLib selector “resume” (obj_id as ptr)

mBlock = new iOSBlock(AddressOf DownloadComplete)

dim downloadTask_obj as ptr = downloadTask(sharedSession(NSClassFromString(“NSURLSession”)), url, mBlock.Handle)

resume(downloadTask_obj)[/code]

Also add a function to your view thats called when the download is done.

[code]Sub DownloadComplete(location as ptr, response as ptr, err as ptr)
dim err_ as Foundation.NSError
self.player = new AVFoundation.AVAudioPlayer(new Foundation.NSURL(location), err_)
call self.player.PrepareToPlay()
self.player.volume = 1.0
call self.player.Play()

End Sub
[/code]

@Jason King I’m going to try it this weekend. Thank you. Now can you find some code examples that free up more hours in the day so I can do all this stuff before the weekend? You figure that out and you’ll be a billionaire.