-3

I have simply code here:

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Diagnostics;  
namespace Zad3
{

public sealed partial class MainPage : Page
{

    public MainPage()
    {

        this.InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {

        String text = System.IO.File.ReadAllText(@"C:\Users\source\repos\Lista3\zad3.txt");
        if (text != null)
        {
            MediaElement mediaElement = new MediaElement();
            var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
            Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(text);
            mediaElement.SetSource(stream, stream.ContentType);
            mediaElement.Play();
        }
    }

}

}

And i create one button in xaml. When i click button my program turn off... When i change text for normal string (np."Hello") my program work perfectly.

How can i repair this ? Please help. I tried to do it in a million ways . I'm begginer in this language.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
toja6755
  • 23
  • 7
  • 1
    Possible duplicate of [How to convert text string to speech sound](https://stackoverflow.com/questions/15387212/how-to-convert-text-string-to-speech-sound) – codebender Oct 22 '17 at 19:07
  • your link is for string. i know how can i do it. I need information about how to add file.txt to SpeechSynthesisStream – toja6755 Oct 22 '17 at 19:29
  • 1
    This has nothing to do with speech -- the problem is that [you don't have permission to access that file](https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions) – Peter Torr - MSFT Oct 22 '17 at 19:29
  • can u give me code how can i do this in this example ? i don't understand this... Please ;( – toja6755 Oct 22 '17 at 20:13

2 Answers2

1

You are not allowed to access this file from UWP @"C:\Users\source\repos\Lista3\zad3.txt". It is not in the application's local storage.

To get access to that file, you must request permission from the user by using the FileOpenPicker. Here's a link to get your started:

https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-using-file-and-folder-pickers

Laith
  • 6,071
  • 1
  • 33
  • 60
0

Try putting the file somewhere besides the User folder.

String text = System.IO.File.ReadAllText(@"C:\NewFolder\zad3.txt");
codebender
  • 469
  • 1
  • 6
  • 23