Sound.Play/Stop Not working

If I drag an mp3 into my project and .playlooping the music in the App.Opening event and then try and .stop the music anywhere in the App I don’t get a result. Are there some exceptions to what you can play? R2.1

As far as I know the built in Sound class is broken. You can use the AVAudioPlayer class in iOSKit. Example here: https://github.com/kingj5/iOSKit/blob/master/ExampleViews/AVFoundationViews/RecordPlayAudioView.xojo_code

Thanks Jason, is ios kit suppose to work in Xojo 2020R2.1?

Some commits were pushed today that should eliminate the compiler errors. Please pull the latest version for 2020r2.1 support

What would be the minimum that I have to drag in the project or doesn’t it matter? What I mean is if you don’t use it, it doesn’t compile.

Just copy the entire modules folder. Anything you don’t use will be removed by the linker when compiling your app

1 Like

How do you use this to play an .mp3 that is dragged into your project

if not recorder.recording then
  dim err as Foundation.NSError
  player = new AVFoundation.AVAudioPlayer(recorder.url,err)
  call player.Play
end if

This is untested but should get you most of the way there. I thought it was fairly clear from the example but let me know if something still doesn’t make sense

Dim f as folderitem //file to play
//player is an AVFoundation.AVAudioPlayer instance on your view
Dim url as new Foundation.NSURL(f)

dim err as Foundation.NSError

player = new AVFoundation.AVAudioPlayer(url,err)
call player.Play

When I try this, it doesn’t play giving an error operation can’t be completed

Dim f as folderitem //file to play
//player is an AVFoundation.AVAudioPlayer instance on your view
f = SpecialFolder.Resource("QMSoundtrack2.mp3")
Dim url as new Foundation.NSURL(f.URLPath,false)

dim err as Foundation.NSError

player = new AVFoundation.AVAudioPlayer(url,err)
call player.Play

Please try adding the following before that code:

dim session as AVFoundation.AVAudioSession = AVFoundation.AVAudioSession.SharedInstance
dim err as Foundation.NSError
call session.SetCategory(AVFoundation.AVStringConstant("AVAudioSessionCategoryPlayback"),err)
if not session.SetActive(True,err) then
    MsgBox "failed to make session active"
end if

Err is nil now but doesn’t play anything. The file is ok if I just use Xojo .play but that is back to the initial problem of you can’t stop it

Add an iOSSound property in App class or a module. Let’s name it theSound

Play like this:

if theSound is nil then
    theSound = new iossound(SpecialFolder.GetResource("name of file.mp3"))
End if
theSound.Play

Stop like this:

theSound.Stop
1 Like

Please try passing only the folderitem, like in my example. The URL Path might not be what we want.

Also please try changing the AVAudioPlayer constructor to

player = new AVFoundation.AVAudioPlayer(url, “mp3”, err)

This should work per stackoverflow, the second suggestion I translated from there

1 Like

So for those of you that are using iOSSound or MobileSound, may I suggest that you subclass it and add a Destructor:

Sub Destructor()
    Self.Stop
End Sub

This should stop the memory leak that’s been reported.

In terms of the original question, the problem you are running into is that a sound dragged into a project is a method at runtime which returns an instance of the object it wraps… and a new instance is created every time it is called.

The way around this is to make a property somewhere, lets say

App.MySound as Sound

In the App.Opening event:

MySound = Name_of_your_sound
MySound.Play

And later:

App.MySound.Stop
2 Likes

Thanks Greg :beers: