Search code examples
javastringtwitterprocessingtext-to-speech

Text to Speech live Tweets using TTS.lib


I'm trying to get a specific user's tweets into Processing and then have them spoken out using the TTS Library. So far I've managed to get the tweets into Processing, with them printed as I want them. BUT, adding the TTS stuff is where it's proving problematic, considering my novice-level-skills.

What happens at the moment is that I receive the error message:

The method speak(String) in the type TTS is not applicable for the arguments (String[])

Anyone have any ideas? Help would be greatly appreciated. Thanks.

import twitter4j.util.*;
import twitter4j.*;
import twitter4j.management.*;
import twitter4j.api.*;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.auth.*;

import guru.ttslib.*;


import java.util.*;

TTS tts;
tts = new TTS();

ConfigurationBuilder cb = new ConfigurationBuilder();

cb.setOAuthConsumerKey("XXXX");
cb.setOAuthConsumerSecret("XXXX");
cb.setOAuthAccessToken("XXXX");
cb.setOAuthAccessTokenSecret("XXXX");

java.util.List statuses = null;


Twitter twitter = new TwitterFactory(cb.build()).getInstance();

String userName ="@BBC";
int numTweets = 19;
String[] twArray = new String[numTweets];

  try {
    statuses = twitter.getUserTimeline(userName);
  }
  catch(TwitterException e) {
  }

  for (int i=0; i<statuses.size(); i++) {
    Status status = (Status)statuses.get(i);

    //println(status.getUser().getName() + ": " + status.getText());
    twArray[i] = status.getUser().getName() + ": " + status.getText();

  }

println(twArray);

tts.speak(twArray);

Solution

  • The error says it all: the tts.speak() function take a single String value, but you're giving it a String[] array.

    In other words, you should only be passing in a single tweet at a time. Instead, you're passing in every tweet at once. The function doesn't know how to handle that, so you get the error.

    You need to only pass in a single String value. How you do that depends on what exactly your goal is. You might just pass in the first tweet:

    tts.speak(twArray[0]);
    

    Or you might pass in each tweet one at a time:

    for(String tweet : twArray){
       tts.speak(tweet);
    }