Playing movies and sound
Playing movies in desktop apps
To play a movie in your desktop app, drag a MoviePlayer control onto a window. You can then set the Movie property to the movie that you want to play. This can be done in the Inspector for movies that have been added to the project or it can be done at runtime by assigning a Movie to the Movie property.
On macOS, MoviePlayer uses AVFoundation to play the movie. On Windows, Windows Media Player is used to play the movie. MoviePlayer is not supported on Linux.
This code prompts the user to select a movie and then plays it in a MoviePlayer:
Var f As FolderItem f = FolderItem.ShowOpenFileDialog("") If f <> Nil Then MoviePlayer1.Movie = Movie.Open(f) MoviePlayer1.Play End If
WebMoviePlayer can play movies from a variety of sources using the browser's built-in HTML5 video capabilities.
To play a movie, you assign a URL to the MovieURL property.
Playing movies in mobile apps
To play movies in iOS apps, you can use the MobileMoviePlayer control. To do this, add a Copy Files Build Step to your project to add the movie file. Refer to the Copy Files to iOS Device topic for information on how to set up a Copy Files Step.
In your app you point a FolderItem to the movie file and then tell the MobileMoviePlayer to display it like this:
Var f As FolderItem = SpecialFolder.Resource("MyMovie.mp4") If f.Exists Then MoviePlayer1.LoadFile(f.URLPath) End If
Playing sounds and audio in desktop apps
With desktop apps, you have a several options for playing sounds.
Sound files that have been added to your project can be played simply by referencing their name and calling the Play method:
This works for most sound files such as WAV, AIFF, MP3, AAC, etc. You can also load sounds at runtime using the FolderItem.ShowOpenFileDialog method and the Sound class:
Var f As FolderItem f = FolderItem.ShowOpenFileDialog("") If f <> Nil Then Var s As Sound = Sound.Open(f) If s <> Nil Then s.Play End If End If
The Sound class has properties to adjust the volume and left/right panning of the sound. It also has methods to play, loop, stop, clone and check if a sound is playing.
The NotePlayer class is used to play musical notes.
NotePlayer is not supported on Linux.
This code plays “do-re-mi-fa-so-la-ti-do”:
NotePlayer1.Instrument = 1 // Notes for Do Re Mi Fa So La Ti Do // (C, D, E, F, G, A, B, C) Var DoReMi(7) As Integer DoReMi = Array(60, 62, 64, 65, 67, 69, 71, 60) For Each note As Integer In DoReMi NotePlayer1.PlayNote(note, 100) // Pause to let note play App.SleepCurrentThread(500) Next
You can also use a MoviePlayer to play sounds. Open your sound file as if it were a Movie and assign it to the Movie Property. Using the MoviePlayer to play sounds allows you to use the Movie controller to play and stop the sound or you can make the MoviePlayer invisible if necessary.
Playing sounds in web apps
To play sounds in web apps, use the WebAudioPlayer control. Just assign a URL that points to an MP3 file to the URL property then call the Play method to play it.
Loading sounds at runtime in mobile apps
Use a Copy Files Build Step to copy the sound files to your resources and then you can load a sound file like this:
Var soundFile As FolderItem soundFile = SpecialFolder.Resource("MySound.mp3") Var mySound As Sound = Sound.Open(soundFile) mySound.Play