Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:00:20+00:00 2026-06-16T10:00:20+00:00

I have to create an audio file from a stream generated by the SpeechSynthesizer.

  • 0

I have to create an audio file from a stream generated by the SpeechSynthesizer.

This is the code am using

var stream = await _SpeechSynth.GetSpeakStreamAsync(text, this.Language);
            var reader = new Windows.Storage.Streams.DataReader(stream.GetInputStreamAt(0));
            await reader.LoadAsync((uint)stream.Size);

            byte[] bytes = new byte[stream.Size];
            reader.ReadBytes(bytes);

             var audioFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(_AgendaAudioFilename, CreationCollisionOption.ReplaceExisting);

             using (var outputStream = await audioFile.OpenAsync(FileAccessMode.ReadWrite))
             {

                 Stream os = outputStream.GetOutputStreamAt(0).AsStreamForWrite();
                 await os.WriteAsync(bytes, 0, bytes.Length);
             }

I found out the problem was that at “os.WriteAsync” was not being awaited. When i put a breakpoint at that line and wait for a few secs then the file was being created properly. Shouldnt the await keyword wait till all the bytes are copied into the stream before going to the next line?

I dont mind if anyone can suggest any better way of copying an InMemoryRandomAccessStream to a file?

EDIT:

Mahantesh’s answer works like a charm. Thanks a lot. But am facing a new problem.

After the file is created am trying to open it again to play it but am getting this error.

“Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))”

This is my code.

if (!cacheHit)
        {
            var stream = await _SpeechSynth.GetSpeakStreamAsync(text, this.Language);

            //write the stream to disk (seems to be buggy when attempting to playback from the result of the speech synth request)
            var audioFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(_AgendaAudioFilename, CreationCollisionOption.ReplaceExisting);

             outputStream = await audioFile.OpenAsync(FileAccessMode.ReadWrite);

             await RandomAccessStream.CopyAsync(stream, outputStream);                 

        }

        //read agenda from disk and play back the audio
        var agendaStorageFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(_AgendaAudioFilename);
        var agendaStreamFromDisk = await agendaStorageFile.OpenAsync(FileAccessMode.Read);            

        _MediaPlayer.Url = text;
        _MediaPlayer.SetBytestream(agendaStreamFromDisk);
        _MediaPlayer.Play();    

Am getting the error at this line:

var agendaStreamFromDisk = await agendaStorageFile.OpenAsync(FileAccessMode.Read);

Any idea what might be going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:00:22+00:00Added an answer on June 16, 2026 at 10:00 am

    I think you can try with

    await RandomAccessStream.CopyAsync(inMemoryStream, outputStream);
    

    Solution to new Problem:

    if (!cacheHit)
        {
            var stream = await _SpeechSynth.GetSpeakStreamAsync(text, this.Language);
    
            //write the stream to disk (seems to be buggy when attempting to playback from the result of the speech synth request)
            var audioFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(_AgendaAudioFilename, CreationCollisionOption.ReplaceExisting);
    
             using(outputStream = await audioFile.OpenAsync(FileAccessMode.ReadWrite)){
             await RandomAccessStream.CopyAsync(stream, outputStream);}
    
        }
    
        //read agenda from disk and play back the audio
        var agendaStorageFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(_AgendaAudioFilename);
       using (var agendaStreamFromDisk = await agendaStorageFile.OpenAsync(FileAccessMode.Read)){
    
        _MediaPlayer.Url = text;
        _MediaPlayer.SetBytestream(agendaStreamFromDisk);
        _MediaPlayer.Play();  }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have create a vcf file that contains contacts by using this code ContentResolver
I have a file that is created using var recordIntent = new Intent(MediaStore.Audio.Media.RecordSoundAction); I
I am using the following code to play audio file. I have tested the
I have a stream of PCM audio captured from a cell phone, and I
Alright I have two problems. I'm using AVAudioPlayer to play a simple audio file
How to save the recorded audio file which is recorded using sound_record.js from Titanium
I need to create an audio file from an array of bytes(posted in by
I'm trying to stream 1.5-4 hour audio files from a URL. I have my
I am using AudioQueue to stream audio from an arbitrary source (the class basically
I have an svn dump file from our old server. This file contains multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.