BackgroundMusic

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

Import Statement: import VPlay 2.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 SoundEffectVPlay. For videos use the Video component.

Note: The Qt 5 Audio element currently (as of Qt release 5.3.0) can not play sound files from qrc on iOS, Mac and some Android devices. However, bundling your assets into a qrc file is the suggested deployment option, because you can then compile your assets into the binary and they are protected. Due to this Qt issue, we recommend using SoundEffectVPlay for your background sounds, or deploy your audio assets with the DEPLOYMENTFOLDERS option in your pro file instead of bundling them with qrc. If you need help with this topic, please use our support forums.

V-Play Additions to Audio

BackgroundMusic enhances the Qt 5 component Audio in the QtMultimedia 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 SoundEffectVPlay 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 Audio component would continue playing if the app is moved to the background. On Desktop platforms, the SoundEffect 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 SoundEffectVPlay, 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: "../assets/snd/backgroundMusic.mp3"
  // autoPlay: true - this is set by default
}

Property Documentation

autoLoad : bool

This property indicates if loading of media should begin immediately.

Defaults to true, if false media will not be loaded until playback is started.

Note that only the loading of the media is set here, the music will not start to play when it is loaded automatically, but only after calling play().


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.


availability : enumeration

Returns the availability state of the media player.

This is one of:

Value Description
Available The media player is available to use.
Busy The media player is usually available, but some other process is utilizing the hardware necessary to play media.
Unavailable There are no supported media playback facilities.
ResourceMissing There is one or more resources missing, so the media player cannot be used. It may be possible to try again at a later time.

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


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.
AccessDenied The audio cannot be played due to insufficient permissions.
ServiceMissing The audio cannot be played because the media service could not be instantiated.

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


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 Audio.Infinite and therefore plays the sound infinitely until it gets stopped by calling stop().

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


metaData : alias

See the Audio 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 Audio.PlayingState, add this import to your QML file: import QtMultimedia 5.0.


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
Audio.Null No source has been set or the source is null.
Audio.Loading The Audio is trying to load the source.
Audio.Ready The source is loaded and ready for play.
Audio.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 Audio.Available, add this import to your QML file: import QtMultimedia 5.0.


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.

See also playing, started, and stopped.


started()

This handler is called when playback is started or resumed.

See also playing and paused.


stopped()

This handler is called when playback is stopped.

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 Audio.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 Audio.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 Audio.StoppedState.

See also play().


Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support
Sign up for Free and start developing right away!
I want to make Games
I want to make Apps
Game Development
Cross-Platform, Any Resolution

Use one IDE to deploy to all supported platforms, including iOS and Android, from a single code base.

Support all screen resolutions, aspect ratios and sizes with auto-adapting UI elements, smart scene scaling and memory efficient asset selection.

Learn More

V-Play ranked #1 at research2guidance cross-platform tool benchmarking 2014 by 2188 developers comparing 40 leading tools.

Multiplayer

V-Play Multiplayer supports both real-time and turn-based gameplay, so you can use it to make many different types of cross-platform multiplayer games. It's perfect for making player-vs-player games like 'Words with Friends' or games for a large amount of players, such as 'Clash of Clans'.

Learn More Video

Highscores & Achievements

The V-Play Game Network is a cross-platform gaming service that allows players around the world to compare game highscores and achievements. Players can also challenge each other across multiple platforms and share their progress on Facebook.

Learn More

QML & JavaScript

Qt Meta Language is a highly intuitive reactive language, which is super easy to learn, yet it's extremely powerful and flexible. Mix it with JavaScript to create awesome stuff, with just a few lines of code.

import QtQuick 2.0
import VPlay 2.0

GameWindow {
  Scene {
    
    SimpleButton {
      anchors.centerIn: parent
      text: "Press Me"
      onClicked: {
        console.debug("Wow you pressed me!")
      }
    }
  }
}

Learn More

Monetize & Analyze

With V-Play, you can use many 3rd party services for ads, in-app purchases, analytics and more, with just a few lines of code. All of these plugins work cross-platform, which allows you to maintain a single code base.

Learn More

Level Editor

The LevelEditor can be used during development to create and modify levels for your game, which you can then bundle in your final publishing build. Additionally, you can also integrate the in-game level editor to your published game and let your gamers create new levels.

Learn More Video

Card Game like UNO, Hearthstone or Poker

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/OneCard/OneCard.pro

Match-3 like Candy Crush Saga

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/JuicySquash/JuicySquash.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/JuicySquashAdvanced/JuicySquashAdvanced.pro

Puzzle like 2048 or Threes!

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/2048/2048.pro

Casino like Big Win Slots

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/FlaskOfRum/FlaskOfRum.pro

Side Scroller like Jetpack Joyride or Flappy Bird

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/FlappyBird/FlappyBird.pro

Tower Defense like Castle Defense or Bloons TD

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/Squaby/Squaby.pro

Falldown & Jump like Doodle Jump or Mega Jump

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/ChickenOutbreak/ChickenOutbreak.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/ChickenOutbreak2/ChickenOutbreak2.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/DoodleJump/DoodleJump.pro

Platformer like Super Mario or Lep's World

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/Platformer/Platformer.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/PlatformerWithLevelEditor/PlatformerWithLevelEditor.pro

Action like Angry Birds, Fruit Ninja, Cut the Rope

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/StackTheBoxWithCommunityEditor/StackTheBoxWithCommunityEditor.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/BalloonPop/BalloonPop.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/CarChallenge/CarChallenge.pro

Arcade like Arkanoid or Space Invaders

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/demos/ZombieBreak/ZombieBreak.pro

Community like Super Mario Maker or Minecraft

We got a demo game for you!

The V-Play SDK includes an open-source demo for this game genre. You can use its source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/examples/Squaby/Squaby.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/StackTheBoxWithCommunityEditor/StackTheBoxWithCommunityEditor.pro

<Path to V-PlaySDK>/Examples/V-Play/demos/PlatformerWithLevelEditor/PlatformerWithLevelEditor.pro

Any other Idea? let us know how we can help you

You are looking for another demo?

The V-Play SDK includes many open-source demos for different game genres. You can use their source code and build your game in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

If you do not find your game genre in this list and wonder if V-Play is the right choice, just contact us, we are happy to help!

Contact Us

App Development
Better Apps, Less Effort

Develop feature-rich, cross-platform mobile apps from a single code base.

V-Play apps look, feel and perform exactly like native iOS, Android and Desktop apps. With less code & much faster development time.

Learn More Highlights Video

V-Play ranked #1 at research2guidance cross-platform tool benchmarking 2014 by 2188 developers comparing 40 leading tools.

Save Time, Code & Money

Save up to 90% source code with V-Play, compared to frameworks like Xamarin, Titanium, React Native and others.

Learn More

QML & JavaScript

Qt Meta Language is a highly intuitive reactive language, which is super easy to learn, yet it's extremely powerful and flexible. Mix it with JavaScript to create awesome stuff, with just a few lines of code. QML also allows you to easily create smooth and complex animations.

import QtQuick 2.0
import VPlayApps 2.0

App {
  Page {
    
    AppButton {
      anchors.centerIn: parent
      text: "Press Me"
      onClicked: {
        console.debug("Wow you pressed me!")
      }
    }
  }
}

Learn More

Monetize & Analyze

With V-Play, you can use many 3rd party services for ads, in-app purchases, analytics and more, with just a few lines of code. All of these plugins work cross-platform, which allows you to maintain a single code base.

Learn More

Native Sensors & More

V-Play Apps harness the power of Qt, the leading cross-platform development framework used by over 1,000,000 developers.

This gives your access to native device features like sensors, camera, file system as well as multimedia, networking, localization and much more.

Learn More

Responsive Design

V-Play has built in responsive design capabilities, you can target phones and tablets with the same source code.

Learn More

Highly Extensible

You have existing JavaScript, C++ or native code? You want to add any 3rd party SDK that we do not offer already?

No worries, you can add any JS, C++, Java or Objective-C code to your project.

Learn More

Component Showcase App shows the most important V-Play features and components

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/showcase/Showcase.pro

Qt World Summit Conference App a full-featured conference management app made by V-Play

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/qtws2016/QtWS2016.pro

Twitter App how to build layouts like in the official Twitter app for iOS and Android

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/twitter/Twitter.pro

Maps App displays free bikes or boxes at bike stations for Vienna's bike sharing service Citybike Wien

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/maps/Maps.pro

Messaging App demonstrates how to create the UI of a Facebook Messenger like app

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/messaging/Messaging.pro

Weather App how to include powerful animations into your user interface

We got a demo app for you!

The V-Play SDK includes an open-source demo for this app type. You can use its source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play.

<Path to V-PlaySDK>/Examples/V-Play/appdemos/waether/Weather.pro

Any other Idea? let us know how we can help you

You are looking for another app demo?

The V-Play SDK includes many open-source demos for different app types. You can use their source code and build your app in record time. After installing V-Play, you can simply open the .pro file with Qt Creator, the development environment used for V-Play..

If you do not find your app type in this list and wonder if V-Play is the right choice, just contact us, we are happy to help!

Contact Us