package uk.ac.ucl.excites.sapelli.collector.util;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;

/**
 * Class that uses the Media Player to speak a given audio file (i.e. mp3)
 * 
 * @author Michalis Vitos
 *
 */
public class AudioToVoice
{
	private Context context;
	private MediaPlayer mediaPlayer;

	public AudioToVoice(Context context)
	{
		this.context = context;
	}

	/**
	 * Use Media Player to speak a given audio file
	 * 
	 * @param soundFilePath
	 */
	public void speak(String soundFilePath)
	{
		// Stop any previous playbacks
		stop();

		// Create the Media Player
		if(soundFilePath != null)
			mediaPlayer = MediaPlayer.create(context, Uri.parse(soundFilePath));

		// Make sure that the Media Player was successfully created.
		if(mediaPlayer != null)
			mediaPlayer.start();
	}

	/**
	 * Stop the Media Player
	 */
	public void stop()
	{
		if(mediaPlayer != null && mediaPlayer.isPlaying())
			mediaPlayer.stop();
	}

	/**
	 * Destroy the media player and release any resources
	 */
	public void destroy()
	{
		if(mediaPlayer != null)
		{
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}
}

@@ -44,16 +44,45 @@
 .util;%0A%0A
+import java.io.IOException;%0A%0A
 import a
@@ -126,25 +126,115 @@
 d.media.
-MediaPlay
+AudioManager;%0Aimport android.media.MediaPlayer;%0Aimport android.media.MediaPlayer.OnCompletionListen
 er;%0Aimpo
@@ -614,16 +614,74 @@
 ilePath%0A
+%09 * @throws IOException%0A%09 * @throws IllegalStateException%0A
 %09 */%0A%09pu
@@ -761,24 +761,421 @@
 %0A%09%09stop();%0A%0A
+%09%09// Get the volume%0A%09%09final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);%0A%09%09final int originalVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);%0A%09%09final int streamMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);%0A%0A%09%09// Set the volume%0A%09%09audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, streamMaxVolume, 0);%0A%0A
 %09%09// Create 
@@ -1211,32 +1211,36 @@
 lePath != null)%0A
+%09%09%7B%0A
 %09%09%09mediaPlayer =
@@ -1294,16 +1294,82 @@
 ePath));
+%0A%09%09%09mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);%0A%09%09%7D
 %0A%0A%09%09// M
@@ -1451,16 +1451,20 @@
 = null)%0A
+%09%09%7B%0A
 %09%09%09media
@@ -1478,16 +1478,299 @@
 start();
+%0A%0A%09%09%09// Set the volume back to its original value%0A%09%09%09mediaPlayer.setOnCompletionListener(new OnCompletionListener()%0A%09%09%09%7B%0A%09%09%09%09@Override%0A%09%09%09%09public void onCompletion(MediaPlayer mp)%0A%09%09%09%09%7B%0A%09%09%09%09%09audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%09%09%7D
 %0A%09%7D%0A%0A%09/*

Set the volume to its maximum value while using the Audio Feedback and restore it on completion