Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

BackgroundMusic

The BackgroundMusic element allows playing long-lasting and looping background sound in wav, mp3 or ogg file format. More...

Import Statement: import Felgo 4.0
Inherits:

Item

Properties

Signals

Methods

Detailed Description

This component allows to play looping audio files, mostly used as background music in games. Background musics are usually longer-lasting tracks in encoded and compressed audio formats like ogg or MP3. However, the BackgroundMusic component can also play wav files. See Supported Audio Formats for more details.

Note: If you have multiple BackgroundMusic objects in your game, set the autoPlay property to false except for the first one you would like to play, to avoid automatically playing all of them when they get loaded.

For short notifications sounds with low latency, use GameSoundEffect. For videos use the Video component.

Felgo Additions to MediaPlayer

BackgroundMusic enhances the Qt component MediaPlayer in the Qt Multimedia 5.0 module with the following additions:

  • Set infinite looping by deafult. To add more variation with different music tracks, set the loops property to a finite number and play another BackgroundMusic in the onStopped handler.
  • Allow to pause and resume all BackgroundMusic objects by setting settings.musicEnbaled to true or false.
  • Proper application handling: if the app is moved to the background all instances of GameSoundEffect and BackgroundMusic are paused on mobile platforms. All instances are then resumed when the app gets focus again and comes into foreground. This is especially useful on mobile devices, where the default MediaPlayer component would continue playing if the app is moved to the background. On Desktop platforms, the GameSoundEffect and BackgroundMusic instances keep playing. You can modify this behavior with the autoPauseInBackground property.
  • Use the playing property to make it easier to detect if the BackgroundMusic is currently playing.

Audio Details

This type allows you to play compressed audio files (typically ogg or MP3 files but also others like AAC or WAV are supported). Compared to GameSoundEffect, it takes less resources as it does not provide such a low latency to play a sound after calling play(). Thus use it to play longer-lasting tracks.

Finding Sounds

For free sounds, we recommend the following websites:

These sites have a great search feature, as you can search by categories or tags. Most of the sounds are available for free if you add the license to your game. There also are low-price premium version of the sounds, which allows you to use them without any license note.

Supported Audio Formats

The different supported platforms use different APIs for playing background music, therefore not all file formats are supported over all platforms. It's recommended to use the following file formats:

Platforms Supported Audio Formats
Windows wav, mp3
All other platforms wav, mp3, ogg

The music is preloaded (the autoLoad property is true by default) and is started automatically by default after loading it. You can change that auto-play behavior by setting autoPlay to false.

For manual playback control, start the music with play().

Performance Hints

Try setting the bitrate and the sampling rate as low as possible, so it still sounds reasonable. That helps in saving precious memory and runtime performance. Also mono channel sounds are preferred for performance reasons. The background musics used in the demo projects have a bitrate of 48kb/s and a 22kHz sampling rate.

Example Usage

Here is an example of a background music that is played when the qml file is loaded:

 BackgroundMusic {
   id: backgroundMusic
   source: Qt.resolvedUrl("../assets/snd/backgroundMusic.mp3")
   // autoPlay: true - this is set by default
 }

Property Documentation

autoPauseInBackground : bool

Set this property to automatically pause this BackgroundMusic when the app is moved to background.

On mobile that happens if the user presses the home button and leaves the app. On desktop that happens when the app loses its focus or gets minimized.

By default, this value is true on mobile platforms and false on desktop platforms.


autoPlay : bool

Set this property to play the background music automatically when this item is loaded, e.g. at application start.

By default, it is set to true. So when the settings.musicEnabled property is also set to true, the music is played after initialization of the Settings object.

See also GameWindowApplicationWindow::settings.


bufferProgress : real

This property holds how much of the data buffer is currently filled, from 0.0 (empty) to 1.0 (full).


duration : int

This property holds the duration of the media in milliseconds.

If the media doesn't have a fixed duration (a live stream for example) this will be 0.


error : enumeration

This property holds the error state of the audio. It can be one of:

Value Description
NoError There is no current error.
ResourceError The audio cannot be played due to a problem allocating resources.
FormatError The audio format is not supported.
NetworkError The audio cannot be played due to network issues.
AccessDeniedError The audio cannot be played due to insufficient permissions.

Note: For accessing the enum values like MediaPlayer.NoError, add this import to your QML file: import QtMultimedia.


errorString : string

This property holds a string describing the current error condition in more detail.


hasAudio : bool

This property holds whether the media contains audio.


hasVideo : bool

This property holds whether the media contains video.


loops : alias

This property provides a way to control the number of times to repeat the sound on each play().

By default, loops is set to MediaPlayer.Infinite and therefore plays the sound infinitely until it gets stopped by calling stop().

Note: To have access to the MediaPlayer.Infinite enum, make sure to call import QtMultimedia 6 in the header of your QML file.


metaData : alias

See the MediaPlayer documentation which metaData properties are available.

See also QMediaMetaData.


muted : bool

This property holds whether the background music is muted.

Defaults to false, and can be set to true or by setting the volume to 0.


playbackRate : real

This property holds the rate at which audio is played at as a multiple of the normal rate.

Defaults to 1.0.


playbackState : enumeration

This property holds the state of media playback. It can be one of:

  • PlayingState - the media is currently playing.
  • PausedState - playback of the media has been suspended.
  • StoppedState - playback of the media is yet to begin.

Note: For accessing the enum values like MediaPlayer.PlayingState, add this import to your QML file: import QtMultimedia.


playing : bool

This property holds whether the background music is currently playing.

This property is read-only.

See also play().


position : int

This property holds the current playback position in milliseconds.

To change this position, use the seek() method.

See also seek().


seekable : bool

This property holds whether position of the audio can be changed.

If true, calling the seek() method will cause playback to seek to the new position.


source : url

This property holds the source URL of the media.

The default is an empty url, so this is a required property.


status : int

This property indicates the current status of the BackgroundMusic. Possible statuses are listed below.

Value Description
MediaPlayer.Null No source has been set or the source is null.
MediaPlayer.Loading The Audio is trying to load the source.
MediaPlayer.Ready The source is loaded and ready for play.
MediaPlayer.Error An error occurred during operation, such as failure of loading the source.

This property is read-only.

Note: For accessing one of the enum values like MediaPlayer.LoadedMedia, add this import to your QML file: import QtMultimedia.


volume : real

This property holds the volume of the music output, from 0.0 (silent) to 1.0 (maximum volume).

Note: The volume property affects ALL Sounds and the BackgroundMusic instances on Windows, but not on mobile platforms. So the last volume setting that is defined is used for all sounds in the application.

The default value is 1.

See also muted.


Signal Documentation

paused()

This signal is emitted when playback is paused.

The corresponding handler is onPaused.

Note: The corresponding handler is onPaused.

See also playing, started, and stopped.


started()

This handler is called when playback is started or resumed.

Note: The corresponding handler is onStarted.

See also playing and paused.


stopped()

This handler is called when playback is stopped.

Note: The corresponding handler is onStopped.

See also playing and started.


Method Documentation

pause()

Pauses playback of the media.

Sets the playing property to false. It also sets the playbackState property to MediaPlayer.PausedState.

See also play().


play()

Starts playback of the media if the settings.musicEnabled property is true (which is the default).

Sets the playing property to true. It also sets the playbackState property to MediaPlayer.PlayingState.

See also pause() and stop().


seek(offset)

If the seekable property is true, seeks the current playback position to offset.

Seeking may be asynchronous, so the position property may not be updated immediately.

See also seekable and position.


stop()

Stops playback of the media.

Sets the playing property to false. It also sets the playbackState property to MediaPlayer.StoppedState.

See also play().


Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded