<file_sep>package com.philiproyer.audiobook.screens;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.philiproyer.audiobook.AudioBook;
import com.philiproyer.audiobook.tools.MetaData;


public class Player implements Screen {
	
	private Stage stagePlayer;
	private Table tablePlayer;
	private Skin skinPlayer;
	private Preferences prefs;
	private MetaData meta;
	private boolean pause;
	private float volume;
	private Music track;
	private String path;
	private String file;
	
	public Player(String path) {
		
		this.path = path; // Set the path that will be used for playing the requested file
		this.track = Gdx.audio.newMusic(Gdx.files.internal(path)); // LibGDX music instance
		this.file = this.path.substring(this.path.lastIndexOf("/") + 1).trim(); // Get the actual file name out of the path var
	
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		stagePlayer.act(delta);
		stagePlayer.draw();
	}

	@Override
	public void resize(int width, int height) {
		tablePlayer.invalidateHierarchy();
	}

	@Override
	public void show() {
		
		stagePlayer = new Stage();

		Gdx.input.setInputProcessor(stagePlayer);

		skinPlayer = new Skin(Gdx.files.internal("ui/menuSkin.json"), new TextureAtlas("ui/atlas.pack"));
		
		
		tablePlayer = new Table(skinPlayer);
		tablePlayer.setFillParent(true);
		
		
		prefs = Gdx.app.getPreferences(AudioBook.TITLE); // Get the preferences
		
		pause = true; // Default to pause
		
		if(prefs.contains("volume")) { // Check if volume has been set previously
			volume = prefs.getFloat("volume");
		} else { // If not set the volume to something nice
			volume = 0.7f;
			prefs.putFloat("Volume", 0.7f);
			prefs.flush();
		}
		
		meta = new MetaData(); // Instance of the TIKA metadata reader
		meta.loadFile(this.path); // Load the current track and read the data
		

		final TextButton play = new TextButton("Play", skinPlayer, "default");
		play.addListener(new ClickListener() {

			@Override
			public void clicked(InputEvent event, float x, float y) {
				play();
				
				if(pause) {
					play.setText("Play");
				} else {
					play.setText("Pause");
				}
			}
			
		});
		play.pad(5);
		
		TextButton volumeUp = new TextButton("Volume +", skinPlayer, "default");
		volumeUp.addListener(new ClickListener() {

			@Override
			public void clicked(InputEvent event, float x, float y) {
				volumeUp();
			}
			
		});
		volumeUp.pad(5);
		
		TextButton volumeDown = new TextButton("Volume -", skinPlayer, "default");
		volumeDown.addListener(new ClickListener() {

			@Override
			public void clicked(InputEvent event, float x, float y) {
				volumeDown();
			}
			
		});
		volumeDown.pad(5);

		tablePlayer.add(new Label("AudioBook", skinPlayer, "big")).colspan(9).expandX().spaceBottom(100).row();
		
		tablePlayer.add().uniformX().expandY().top().colspan(1);
		tablePlayer.add(play).left().expandY().top().colspan(1);
		tablePlayer.add().uniformX().expandY().top().colspan(7).row();

		tablePlayer.add().uniformX().expandY().top().colspan(1);
		tablePlayer.add(volumeUp).left().expandY().top().colspan(1);
		tablePlayer.add().uniformX().expandY().top().colspan(7).row();
		
		tablePlayer.add().uniformX().expandY().top().colspan(1);
		tablePlayer.add(volumeDown).left().expandY().top().colspan(1);
		tablePlayer.add().uniformX().expandY().top().colspan(7).row();
		
		stagePlayer.addActor(tablePlayer);
	}

	@Override
	public void hide() {
		saveProgress();
	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {
		
	}

	@Override
	public void dispose() {
		System.out.println("The destroying is happening!");
	}
	
	// Volume buttons
	private void volumeUp() {
		if(this.volume < 1.0) {
			this.volume += 0.05;
			this.track.setVolume(volume);
			System.out.println("Volume Increased " + volume);
		}
	}
	
	private void volumeDown() {
		if(this.volume > 0.0) {
			this.volume -= 0.05;
			this.track.setVolume(volume);
			System.out.println("Volume Decreased " + volume);
		}
	}
	
	private void play() {
		if(!track.isPlaying()) {
			this.pause = false;
			this.track.setVolume(volume);
			this.track.play();
			
			if (Gdx.app.getType() == ApplicationType.Android) {
				//this.track.setPosition(prefs.getFloat(this.file));
			}
			
			// Set location to last location

			System.out.println("Play: " + this.track.getPosition());
		} else {
			this.pause = true;
			this.track.pause();
			saveProgress();
			System.out.println("Pause: " + this.track.getPosition());
		}
	}
	
	private void saveProgress() {
		prefs.putFloat(this.file,this.track.getPosition()); // Store value of track in preferences
		System.out.println("Progress saved for " + this.file);
	}

}
<file_sep>app.version=1.0
app.id=com.philiproyer.audiobook.IOSLauncher
app.mainclass=com.philiproyer.audiobook.IOSLauncher
app.executable=IOSLauncher
app.build=1
app.name=Audiobook Player
<file_sep>An Open Source AudioBook Reader