UserGuide

Movies and Sound

From Xojo Documentation

Your apps can display and play movies using several available controls: MoviePlayer (for desktop apps), WebYouTubeMovie 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:

Dim f As FolderItem
f = GetOpenFolderItem("")

If f <> Nil Then
MoviePlayer1.Movie = f.OpenAsMovie
MoviePlayer1.Play
End If

Playing Movies in Web Apps

There are two controls that can be used to play movies in web apps: WebYouTubeMovie and WebMoviePlayer.

WebYouTubeMovie

WebYouTubeMovie is a simple control that can only play movies from YouTube. You specify the URL to the YouTube movie in the Inspector for the WebYouTubeMovie control. You must use the long URL to the YouTube movie, not the shorter sharing link.

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 one of the URL properties: DesktopURL, MobileCellularURL and MobileWIFIURL. WebMoviePlayer will use the appropriate property depending on the device being used and its Internet connection speed.

There are several methods to control the movie, including: FastForward, FastForwardStop, FastRewind, FastRewindStop, GoToBeginning, GoToEnding, Mute, Play and Reset. You should always call Reset after changing a movie URL at run-time.

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:

Dim f As FolderItem = SpecialFolder.GetResource("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:

Dim f As FolderItem
f = GetOpenFolderItem("")
If f <> Nil Then
Dim s As Sound = f.OpenAsSound
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)
Dim 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 you'll want to use the HTML5 Audio tag in a WebHTMLViewer. This is an example of how you can set up the HTML5 Audio tag:

<audio controls="controls" %autoplay%>
  <source src="http://www.archive.org/download//Stockfinster.-DeadLinesutemos025/01_Push_Push.mp3" type="audio/mp3" />
  Your browser does not support the audio tag.
</audio>

Assign the above HTML to a variable or constant and then you can then send that HTML to the WebHTMLViewer to play the sound. Here a constant with the above HTML (kHTMLAudio) is sent to the HTMLViewer:

HTMLViewer1.LoadPage(kHTMLAudio)

This displays audio player controls that you can use to play the sound.

Refer to the example included with Xojo for a control you can use in your own apps: Examples/Web/Controls/AudioPlayer

Playing Sounds and Audio in iOS Apps

You can use the iOSSound class to play sounds in your iOS apps. Just drag the sound files you want to use into your iOS project. You can play them in code by calling the Play method:

SoundName.Play

You can also load sound files at run-time. You'll need to use a Copy Files Build Step to copy the sound files to your resources and then you can load a sound file like this:

Dim soundFile As FolderItem
soundFile = SpecialFolder.GetResource("MySound.mp3")
Dim 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/AudioPlayer
  • Examples/Web/Controls/WebMoviePlayer

See Also

Movie, MoviePlayer, NotePlayer, Sound, iOSSound, WebMoviePlayer, WebYouTubeMovie classes; UserGuide:Framework topic