UserGuide

Movies and Sound

From Xojo Documentation

Your apps can display and play movies using several available controls: MoviePlayer (for desktop apps) and WebMoviePlayer (for web apps).

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 run-time 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

Playing Movies in Web Apps

The web equivalent of the desktop MoviePlayer is the WebMoviePlayer.

WebMoviePlayer

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 iOS Apps

To play movies in iOS apps, you can use the iOSHTMLViewer 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 HTMLViewer to display it like this:

Var f As FolderItem = SpecialFolder.Resource("MyMovie.mp4")

If f.Exists Then
HTMLViewer1.LoadURL(f.URLPath)
End If

Playing Sounds and Audio in Desktop Apps

With desktop apps, you have a several options for playing sounds.

Sound Class

Sound files that have been added to your project can be played simply by referencing their name and calling the Play method:

SoundName.Play

This works for most sound files such as WAV, AIFF, MP3, AAC, etc. You can also load sounds at run-time using the FolderItem.OpenAsSound 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.

Note Player

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

MoviePlayer

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 iOS 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 New iOSSound(soundFile)
mySound.Play

Example Projects

  • Examples/Graphics and Multimedia/DoReMi
  • Examples/Graphics and Multimedia/FolderMoviePlayer
  • Examples/Graphics and Multimedia/Sound/Sound Player
  • Examples/iOS/Sound/GameBuzzer
  • Examples/iOS/Sound/NatureSounds
  • Examples/Web/Controls/WebMoviePlayer

See Also

Movie, MoviePlayer, NotePlayer, Sound, WebMoviePlayer classes