I have a Background Audio Task in a WP8.1 project which uses the BackgroundMediaPlayer to play audio.

In my foreground app I have articles (online articles) which can be listened to. This is done via TTS ( SpeechSynthesizer ).

I have tried two things to implement this feature:

Creating a SpeechSynthesisStream in the task and use it with BackgroundMediaPlayer.Current.SetStreamSource(IRandomAccessStream stream) .

Always hitting memory exceptions with this method when the text is longer than a few hundred chars. Creating the stream in the foreground app and save it to a .wav file. This works with longer texts as in the first method but creates really large files and takes inacceptable long to generate and increases memory by a few hundred MB.

Code for the 2nd implementation:

string content = "......."; // create stream from synthesizer SpeechSynthesizer synth = new SpeechSynthesizer(); SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(content); // get inputstream and size of stream ulong size = stream.Size; IInputStream inputStream = stream.GetInputStreamAt(0); stream.Dispose(); DataReader dataReader = new DataReader(inputStream); await dataReader.LoadAsync((uint)size); byte[] buffer = new byte[(int)size]; dataReader.ReadBytes(buffer); inputStream.Dispose(); dataReader.Dispose(); // open folder and file IStorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Audio", CreationCollisionOption.OpenIfExists); IStorageFile file = await folder.CreateFileAsync("audio.wav", Windows.Storage.CreationCollisionOption.ReplaceExisting); // write file await Windows.Storage.FileIO.WriteBytesAsync(file, buffer);

Any ideas how to implement this feature in a "memory-friendly" and fast way (without using online services)?