Using Amazon Polly from .net / c#, Get MP3 File

knight-rider-car-kittIf you haven’t checked out Amazon’s new Polly (Text-to-Speech (TTS) cloud service) it does produce some pretty great, life-like audio. Below is a quick sample on how you can feed some text to Polly, and get an MP3 file with it. I don’t cover all of the install options of the AWS Toolkit for Visual Studio / .Net, but it’s pretty simple. (listen to this text here)

Below is some code:

AmazonPollyClient pc = new AmazonPollyClient();

SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();
sreq.Text = "Your Sample Text Here";
sreq.OutputFormat = OutputFormat.Mp3;
sreq.VoiceId = VoiceId.Amy;
SynthesizeSpeechResponse sres = pc.SynthesizeSpeech(sreq);

using (var fileStream = File.Create(@"c:\yourfile.mp3"))
{
sres.AudioStream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();
}

 

Also make sure you have the below included:

using Amazon.Polly;
using Amazon.Polly.Model;

And these 2 NuGet packages added to your project:

aws-polly

This only scratches the surface of what Polly can do. Between streaming, SSML, Lexicons and more at a great price, I think we’ll be seeing more applications use this.

Using Amazon Polly from .net / c#, Get MP3 File

Cordova or NPM Update Errors – MODULE_NOT_FOUND / EACCES

If you’re taking a break and updating node, npm, cordova or other various packages – you may run into some permission issues causing errors like MODULE_NOT_FOUND or EACCES. Below are some errors I experienced (in OSX 10.11).

These errors were fixed pretty easily by running the below:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This command was originally found at:
https://docs.npmjs.com/getting-started/fixing-npm-permissions

npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "update" "-g" "cordova"
npm ERR! node v5.7.0
npm ERR! npm v3.6.0
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module 'github-url-from-git'

OR:

npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/.staging'

Hope this info helps out someone having the same issues.

Cordova or NPM Update Errors – MODULE_NOT_FOUND / EACCES

Creating a .bash_profile file in OS X and adding PATH directories

If you’re starting out with a fresh install of OS X (10.9 in my example) and are using any development tools, at some point I’m sure you’ll want to add some directories to your system PATH. In short: this allows you to use an application in a specific directory from any other directory – commonly when you’re running commands in Terminal.

To start, we’ll utilize a text editor – in my case I’m using TextMate – but any plain text editor should do. Let’s get to it:

  1. bash_1Let’s first make sure you don’t already have a .bash_profile. In TextMate, go to File > Open. Browse to your home folder (with the house icon) and click “Show Hidden Files”. In your home folder you shouldn’t already see a .bash_profile file. (If you do, then you don’t need to create a new file and can open your file, make changes and skip to step 5.)
  2. bash_2So cancel the open dialog and enter some text into the untitled file currently open. You’re usually entering something like: export PATH=${PATH}:/somedirectory/asubdirectory:/anotherdirectory
  3. bash_3Now let’s save our new .bash_profile. Go to File > Save As. Browse to your home folder (with the little house icon again). Enter the filename as “.bash_profile” (without quotes).
  4. bash_4If you get a message saying “names that begin with a dot are reserved for the system” chose “Use ‘.’
  5. bash_5That’s it. Now if you already have a terminal open run source ~/.bash_profile (this just give you access to the updated PATH).
Creating a .bash_profile file in OS X and adding PATH directories

Upgrading Apache Cordova to latest version

Updating Apache Cordova to the latest version only takes a few minutes. Below are the steps (taken from Cordova) needed to update your > 3.0 projects to the latest version (3.5 as of this post).

In a command / terminal run:

  1. npm install -g cordova
  2. Now go to into your project (ie: c:\projects\test)  and run:
  3. cordova platform update android (or ios)

 

You can update your plugins by removing / adding them again like:

cordova plugin rm org.apache.cordova.file
cordova plugin add org.apache.cordova.file

That’s it. Now you should have an up to date environment.

Upgrading Apache Cordova to latest version

New App Version IOS / iTunes Connect CFBundleVersion Error

App_Store_Logo

When adding a new version of your app to iTunes Connect, you may run across the below error:

ERROR ITMS-9000: “This bundle is invalid. The value for key CFBundleVersion [1.0] in the Info.plist file must contain a higher version than that of the previously uploaded version [1.0].” at SoftwareAssets/SoftwareAsset (MZItmspSoftwareAssetPackage)

This version (CFBundleVersion) is the build version of your bundle and can be different (more frequent) than your release version number (aka CFBundleShortVersionString).

You can edit this version in the info.plist file

bundleversion

From Apple:

The build version number should be a string comprised of three non-negative, period-separated integers with the first integer being greater than zero. The string should only contain numeric (0-9) and period (.) characters. Leading zeros are truncated from each integer and will be ignored (that is, 1.02.3 is equivalent to 1.2.3).

After updating your info.plist you should be able to rebuild and upload your new / updated version without issue. -Now you just need to wait for Apple to approve…

New App Version IOS / iTunes Connect CFBundleVersion Error

Renaming a local file in Cordova / PhoneGap

A few folks have asked “how do I simply rename a local file in Cordova?” Well, to start, you’ll need the file plugin from Apache Cordova. If you don’t already have this, run the below to add the file plugin.

cordova plugin add org.apache.cordova.core.file

Then, you can use the “moveTo” function to rename the file. Below is a reusable function I utilize to pass the current file name, directory, the new file name and a function that happens when the rename is successful.

You can utilize the below function like:

//to call the function. 'renameSuccess' is an function that is used as the success event if the rename is okay
renameFile('somefile.xml','myapp/xml/','new.xml', renameSuccess);

//the function
function renameFile(currentName, currentDir, newName, successFunction) {

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {

        fileSystem.root.getFile(currentDir + currentName, null, function (fileEntry) {
            fileSystem.root.getDirectory(currentDir, {create: true}, function (dirEntry) {
                parentEntry = new DirectoryEntry(currentName, currentDir + currentName);

                fileEntry.moveTo(dirEntry, newName, function () {

                    successFunction();

                }, renameFail);
            }, renameFail);
        }, renameFail);

    }, renameFail);
}

//and the sample success function
function renameSuccess() {
    alert('renamed!');
}

//and the sample fail function
function renameFail() {
    alert('failed');
}

I’m not saying this is the ‘best’ way to do this, so feel free to comment on improvements!

Renaming a local file in Cordova / PhoneGap

Allow Local File Access in Chrome (Windows)

chrome-128Sometimes it’s cool to debug and test javascript applications in Chrome but you want to read / write to local files. Example: you’re looking to use ajax and do a $.getJSON(‘json/somefile.json’). Chrome by default won’t allow this and will throw an error similar to:

Failed to load resource: No 'Access-Control-Allow-Origin' 
header is present on the requested resource. 
Origin 'null' is therefore not allowed access.

Or
XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' 
header is present on the requested resource. 
Origin 'null' is therefore not allowed access.

Chrome does have a switch to enable this, it’s quite easy to turn on. You’ll need to make sure Chrome is closed completely, and run chrome with the ‘–allow-file-access-from-files’ flag. Ie:

C:\Users\<user>\AppData\Local\Google\Chrome\Application>
chrome --allow-file-access-from-files

Or you should be able to run:

%localappdata%\google\chrome\application\chrome --allow-file-access-from-files

I’ve made the below into a .bat file I use, if you find it helps.

start "chrome" %localappdata%\google\chrome\application\chrome --allow-file-access-from-files
exit

To see if the flag is set, you can visit:  chrome://version/ and look at the Command Line section and you should see –allow-file-access-from-files

You’ll most likely need to run this with at least admin access, and I would caution visiting unknown sites with this setting on, as they could capitalize on your setting and potentially read local files.

Update, see my newer post on using node and http-server to create a local web server to get around these issues:
Local web server for testing / development using Node.js and http-server

Allow Local File Access in Chrome (Windows)