SkillAgentSearch skills...

AlexaAndroid

A library and sample app to abstract access to the Amazon Alexa service for Android applications.

Install / Use

/learn @willblaschko/AlexaAndroid
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Alexa Voice Library

=======

A library and sample app to abstract access to the Amazon Alexa service for Android applications.

First and foremost, my goal with this project is to help others who have less of an understanding of Java, Android, or both, to be able to quickly and easily integrate the Amazon Alexa platform into their own applications.

For getting started with the Amazon Alexa platform, take a look over here: https://developer.amazon.com/appsandservices/solutions/alexa/alexa-voice-service/getting-started-with-the-alexa-voice-service

Library updated with functionality for the Alexa v20160207 API version.

Applications

Sample Application

Curious about what the library can do? A quick example of the three main functions (live recorded audio events, text-to-speech intents, prerecorded audio intents), plus sample code can be found in the sample app

Compiling and running the sample application

  • Follow the process for creating a connected device detailed at the Amazon link at the top of the Readme.
  • Add your api_key.txt file (part of the Amazon process) to the app/src/main/assets folder
  • Change PRODUCT_ID to the value for my Alexa application that you configured for "Application Type Id" above.
  • Build and run the sample app using Gradle from the command line or Android Studio!

Production Application

Or see what the library can do when converted into a full package, complete with optional always-on listener: Alexa Listens

Using the Library

Most of the library can be accessed through the AlexaManager and AlexaAudioPlayer classes, both of which are singletons.

Installation

  • Ensure you're pulling from jcenter() for your project (project-level build.gradle):
buildscript {
    repositories {
        jcenter()
    }
    ...
}

allprojects {
    repositories {
        jcenter()
    }
}
  • Add the library to your imports (application-level build.gradle):
compile 'com.willblaschko.android.alexa:AlexaAndroid:2.4.2'
  • Follow the process for creating a connected device detailed in the Amazon link at the top of the Readme.
  • Follow the instructions for adding your key and preparing the Login with Amazon activity from the 'Login with Amazon' Android Project guide
  • Add your api_key.txt file (part of the Login with Amazon process detailed in the link above) to the app/src/main/assets folder.
  • Start integration and testing!

Library Instantiation and Basic Return Parsing

private AlexaManager alexaManager;
private AlexaAudioPlayer audioPlayer;
private List<AvsItem> avsQueue = new ArrayList<>();

private void initAlexaAndroid(){
	//get our AlexaManager instance for convenience
	alexaManager = AlexaManager.getInstance(this, PRODUCT_ID);

	//instantiate our audio player
	audioPlayer = AlexaAudioPlayer.getInstance(this);

	//Callback to be able to remove the current item and check queue once we've finished playing an item
	audioPlayer.addCallback(alexaAudioPlayerCallback);
}

//Our callback that deals with removing played items in our media player and then checking to see if more items exist
private AlexaAudioPlayer.Callback alexaAudioPlayerCallback = new AlexaAudioPlayer.Callback() {
	@Override
	public void playerPrepared(AvsItem pendingItem) {

	}

	@Override
	public void itemComplete(AvsItem completedItem) {
		avsQueue.remove(completedItem);
		checkQueue();
	}

	@Override
	public boolean playerError(int what, int extra) {
		return false;
	}

	@Override
	public void dataError(Exception e) {

	}
};

//async callback for commands sent to Alexa Voice
private AsyncCallback<AvsResponse, Exception> requestCallback = new AsyncCallback<AvsResponse, Exception>() {
	@Override
	public void start() {
		//your on start code
	}

	@Override
	public void success(AvsResponse result) {
		Log.i(TAG, "Voice Success");
		handleResponse(result);
	}

	@Override
	public void failure(Exception error) {
		//your on error code
	}

	@Override
	public void complete() {
		 //your on complete code
	}
};

/**
 * Handle the response sent back from Alexa's parsing of the Intent, these can be any of the AvsItem types (play, speak, stop, clear, listen)
 * @param response a List<AvsItem> returned from the mAlexaManager.sendTextRequest() call in sendVoiceToAlexa()
 */
private void handleResponse(AvsResponse response){
	if(response != null){
		//if we have a clear queue item in the list, we need to clear the current queue before proceeding
		//iterate backwards to avoid changing our array positions and getting all the nasty errors that come
		//from doing that
		for(int i = response.size() - 1; i >= 0; i--){
			if(response.get(i) instanceof AvsReplaceAllItem || response.get(i) instanceof AvsReplaceEnqueuedItem){
				//clear our queue
				avsQueue.clear();
				//remove item
				response.remove(i);
			}
		}
		avsQueue.addAll(response);
	}
	checkQueue();
}


/**
 * Check our current queue of items, and if we have more to parse (once we've reached a play or listen callback) then proceed to the
 * next item in our list.
 *
 * We're handling the AvsReplaceAllItem in handleResponse() because it needs to clear everything currently in the queue, before
 * the new items are added to the list, it should have no function here.
 */
private void checkQueue() {

	//if we're out of things, hang up the phone and move on
	if (avsQueue.size() == 0) {
		return;
	}

	AvsItem current = avsQueue.get(0);

	if (current instanceof AvsPlayRemoteItem) {
		//play a URL
		if (!audioPlayer.isPlaying()) {
			audioPlayer.playItem((AvsPlayRemoteItem) current);
		}
	} else if (current instanceof AvsPlayContentItem) {
		//play a URL
		if (!audioPlayer.isPlaying()) {
			audioPlayer.playItem((AvsPlayContentItem) current);
		}
	} else if (current instanceof AvsSpeakItem) {
		//play a sound file
		if (!audioPlayer.isPlaying()) {
			audioPlayer.playItem((AvsSpeakItem) current);
		}
	} else if (current instanceof AvsStopItem) {
		//stop our play
		audioPlayer.stop();
		avsQueue.remove(current);
	} else if (current instanceof AvsReplaceAllItem) {
		audioPlayer.stop();
		avsQueue.remove(current);
	} else if (current instanceof AvsReplaceEnqueuedItem) {
		avsQueue.remove(current);
	} else if (current instanceof AvsExpectSpeechItem) {
		//listen for user input
		audioPlayer.stop();
		startListening();
	} else if (current instanceof AvsSetVolumeItem) {
		setVolume(((AvsSetVolumeItem) current).getVolume());
		avsQueue.remove(current);
	} else if(current instanceof AvsAdjustVolumeItem){
		adjustVolume(((AvsAdjustVolumeItem) current).getAdjustment());
		avsQueue.remove(current);
	} else if(current instanceof AvsSetMuteItem){
		setMute(((AvsSetMuteItem) current).isMute());
		avsQueue.remove(current);
	}else if(current instanceof AvsMediaPlayCommandItem){
		//fake a hardware "play" press
		sendMediaButton(this, KeyEvent.KEYCODE_MEDIA_PLAY);
	}else if(current instanceof AvsMediaPauseCommandItem){
		//fake a hardware "pause" press
		sendMediaButton(this, KeyEvent.KEYCODE_MEDIA_PAUSE);
	}else if(current instanceof AvsMediaNextCommandItem){
		//fake a hardware "next" press
		sendMediaButton(this, KeyEvent.KEYCODE_MEDIA_NEXT);
	}else if(current instanceof AvsMediaPreviousCommandItem){
		//fake a hardware "previous" press
		sendMediaButton(this, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
	}

}

//our call to start listening when we get a AvsExpectSpeechItem
protected abstract void startListening();

//adjust our device volume
private void adjustVolume(long adjust){
	setVolume(adjust, true);
}

//set our device volume
private void setVolume(long volume){
	setVolume(volume, false);
}

//set our device volume, handles both adjust and set volume to avoid repeating code
private void setVolume(final long volume, final boolean adjust){
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	final int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	long vol= am.getStreamVolume(AudioManager.STREAM_MUSIC);
	if(adjust){
		vol += volume * max / 100;
	}else{
		vol = volume * max / 100;
	}
	am.setStreamVolume(AudioManager.STREAM_MUSIC, (int) vol, AudioManager.FLAG_VIBRATE);
	//confirm volume change
	alexaManager.sendVolumeChangedEvent(volume, vol == 0, requestCallback);
}

//set device to mute
private void setMute(final boolean isMute){
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	am.setStreamMute(AudioManager.STREAM_MUSIC, isMute);
	//confirm device mute
	alexaManager.sendMutedEvent(isMute, requestCallback);
}

 private static void sendMediaButton(Context context, int keyCode) {
	KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
	Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
	intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
	context.sendOrderedBroadcast(intent, null);

	keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
	intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
	intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
	context.sendOrderedBroadcast(intent, null);
}

Recipes

Here's a quick overview of the code that will likely be required to create a solid application, check the JavaDoc for more details.

Logging In To Amazon

//Run an async check on whether we're logged in or not
alexaManager.checkLoggedIn(mLo

Related Skills

View on GitHub
GitHub Stars278
CategoryDevelopment
Updated6mo ago
Forks130

Languages

Java

Security Score

87/100

Audited on Oct 4, 2025

No findings