This blog post was authored by Joao Lucas Guberman Raza, a program manager on the Windows Phone team.
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.
- First create the XAudio2 object, which handles the XAudio2 sound APIs.
- 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.
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.