Skip to main content
May 17, 2013
Windows Phone Developer Blog

XAudio2 Performance and Battery Considerations for Windows Phone 8

This blog post was authored by Joao Lucas Guberman Raza, a program manager on the Windows Phone team.

– Adam

In this post we cover important information for Windows Phone 8 developers who use the XAudio2 APIs, including best practices for battery performance. XAudio2 is a high-performance audio API available in Windows 8, Xbox 360, and Windows Phone 8. An app developer can use XAudio2 to create audio graphs in which they can treat each audio source in the graph as a distinct “voice.” The developer can apply different effects to each of the “voices.”

In Windows Phone 8, the XAudio2 engine must be aligned with the life cycle of the app. This means that if an app is suspended, the app that’s using XAudio2 must force the XAudio2 engine to stop. When the app resumes/rehydrates, if it is designed to resume sounds using XAudio2, it must restart the XAudio2 engine. Significant battery drain can occur if an app doesn’t stop the XAudio2 engine when the app is suspended, and the engine continues to run.

To avoid this scenario, an app must call IXAudio2::StopEngine when the app is suspended as described in the Native audio APIs for Windows Phone 8. When the app resumes, it should call IXAudio2::StartEngine. For Silverlight and Direct3D with XAML apps, the suspend/resume APIs are the Suspended and Activated events. For Direct3D native-only apps, the suspend/resume APIs are the Suspending and CoreView Resuming events.

The following code examples show you how you can do this in a pure native Direct3D app.

  1. First create the XAudio2 object, which handles the XAudio2 sound APIs.
    1. IXAudio2* pXAudio = NULL ;
    3. if( FAILED(XAudio2Create(&pXAudio, 0, XAUDIO2_DEFAULT_PROCESSOR) ) )
    4.         return false ;
    6. if ( FAILED(pXAudio->CreateMasteringVoice( &pMasterVoice ) ) )
    7.         return false;

  2. Then, on the suspending/resume events, set up the XAudio2 object to call StopEngine and then StartEngine. In the following example, we use the default events in the Windows Phone 8 SDK template for Direct3D native-only apps.
    1. void WindowsApp::Initialize(CoreApplicationView^ applicationView)
    2.     {
    3.     applicationView->Activated +=
    4.         ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &WindowsApp::OnActivated);
    6.     CoreApplication::Suspending +=
    7.         ref new EventHandler<SuspendingEventArgs^>(this, &WindowsApp::OnSuspending);
    9.     CoreApplication::Resuming +=
    10.         ref new EventHandler<Platform::Object^>(this, &WindowsApp::OnResuming);    
    11. }
    13. void WindowsApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
    14. {
    15.     pXAudio->StopEngine() ;
    16. }
    18.    void WindowsApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
    19.    {
    20.     pXAudio->StartEngine() ;
    21. }

With these steps, you can design your app to follow best practices for battery consumption when you use the XAudio2 APIs. It’s important to note that this is one of many best practices you can use in your app. To learn more, see also local folder best practices for Windows Phone, localization best practices for Windows Phone, and background agent best practices for Windows Phone.