001package org.intellimate.izou.sdk.frameworks.music.player.template; 002 003import org.intellimate.izou.identification.Identification; 004import org.intellimate.izou.identification.IdentificationManager; 005import org.intellimate.izou.sdk.Context; 006import org.intellimate.izou.sdk.activator.Activator; 007import org.intellimate.izou.sdk.frameworks.music.events.PlayerCommand; 008import org.intellimate.izou.sdk.frameworks.music.events.StartMusicRequest; 009import org.intellimate.izou.sdk.frameworks.music.events.StopMusic; 010import org.intellimate.izou.sdk.frameworks.music.player.Playlist; 011import org.intellimate.izou.sdk.frameworks.music.player.Progress; 012import org.intellimate.izou.sdk.frameworks.music.player.TrackInfo; 013import org.intellimate.izou.sdk.frameworks.music.player.Volume; 014import org.intellimate.izou.sdk.frameworks.music.resources.PlaylistResource; 015import org.intellimate.izou.sdk.frameworks.music.resources.ProgressResource; 016import org.intellimate.izou.sdk.frameworks.music.resources.TrackInfoResource; 017import org.intellimate.izou.sdk.frameworks.music.resources.VolumeResource; 018 019import java.util.Optional; 020 021/** 022 * you should use this class to control your Player. 023 * Use this class even if you don't start/stop in this class. For example if you want to use a 3rd. party library which 024 * starts/stops without your interference communicate this with this class. The purpose of this class is not only to 025 * stop the Player, but communicate this behaviour to other classes! 026 * @author LeanderKfini 027 * @version 1.0 028 */ 029public abstract class PlayerController extends Activator { 030 private Player player; 031 032 public PlayerController(Context context, String ID, Player player) { 033 super(context, ID); 034 this.player = player; 035 } 036 037 public PlayerController(Context context, String ID) { 038 super(context, ID); 039 this.player = null; 040 } 041 042 public void setPlayer(Player player) { 043 this.player = player; 044 } 045 046 public Player getPlayer() { 047 return player; 048 } 049 050 /** 051 * starts the playing command 052 */ 053 public void startPlaying() { 054 startPlaying((TrackInfo)null); 055 } 056 057 /** 058 * starts the playing command 059 * @param trackInfo the track to play 060 */ 061 public void startPlaying(TrackInfo trackInfo) { 062 Optional<Identification> ownIdentification = IdentificationManager.getInstance() 063 .getIdentification(this); 064 Optional<Identification> playerIdentification = IdentificationManager.getInstance() 065 .getIdentification(player); 066 if (!ownIdentification.isPresent() || !playerIdentification.isPresent()) { 067 error("unable to obtain identification"); 068 return; 069 } 070 StartMusicRequest.createStartMusicRequest(ownIdentification.get(), playerIdentification.get(), trackInfo, player.isUsingJava) 071 .ifPresent(event -> fire(event, 5)); 072 073 } 074 075 /** 076 * starts the playing command 077 * @param playlist the playlist to play 078 */ 079 public void startPlaying(Playlist playlist) { 080 Optional<Identification> ownIdentification = IdentificationManager.getInstance() 081 .getIdentification(this); 082 Optional<Identification> playerIdentification = IdentificationManager.getInstance() 083 .getIdentification(player); 084 if (!ownIdentification.isPresent() || !playerIdentification.isPresent()) { 085 error("unable to obtain identification"); 086 return; 087 } 088 StartMusicRequest.createStartMusicRequest(ownIdentification.get(), playerIdentification.get(), playlist, player.isUsingJava) 089 .ifPresent(event -> fire(event, 5)); 090 091 } 092 093 /** 094 * stops the playing of the music 095 */ 096 public void stopPlaying() { 097 Optional<Identification> ownIdentification = IdentificationManager.getInstance() 098 .getIdentification(this); 099 Optional<Identification> playerIdentification = IdentificationManager.getInstance() 100 .getIdentification(player); 101 if (!ownIdentification.isPresent()|| !playerIdentification.isPresent()) { 102 error("unable to obtain id"); 103 return; 104 } 105 StopMusic.createStopMusic(ownIdentification.get(), playerIdentification.get()) 106 .ifPresent(event -> fire(event, 5)); 107 } 108 109 /** 110 * commands the player to fulfill the command 111 * @param command the command 112 * @param playlist the playlist, or null if not needed 113 * @param progress the progress, or null if not needed 114 * @param trackInfo the trackInfo, or null if not needed 115 * @param volume the volume, or null if not needed 116 */ 117 public void command(String command, Playlist playlist, Progress progress, TrackInfo trackInfo, Volume volume) { 118 Optional<Identification> ownIdentification = IdentificationManager.getInstance() 119 .getIdentification(this); 120 Optional<Identification> playerIdentification = IdentificationManager.getInstance() 121 .getIdentification(player); 122 if (!ownIdentification.isPresent()|| !playerIdentification.isPresent()) { 123 error("unable to obtain id"); 124 return; 125 } 126 Optional<PlayerCommand> playerCommand = PlayerCommand.createPlayerCommand(ownIdentification.get(), 127 playerIdentification.get(), command, player.getCapabilities(), getContext()); 128 if (playlist != null) { 129 playerCommand.get().addResource(new PlaylistResource(ownIdentification.get(), playlist)); 130 } 131 if (progress != null) { 132 playerCommand.get().addResource(new ProgressResource(ownIdentification.get(), progress)); 133 } 134 if (trackInfo != null) { 135 playerCommand.get().addResource(new TrackInfoResource(ownIdentification.get(), trackInfo)); 136 } 137 if (volume != null) { 138 playerCommand.get().addResource(new VolumeResource(ownIdentification.get(), volume)); 139 } 140 fire(playerCommand.get(), 5); 141 } 142}