Created
April 17, 2025 18:06
-
-
Save thinkphp/f7712248a3effbad0ebcd76c328d3793 to your computer and use it in GitHub Desktop.
PlayList Task va08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class PlayList { | |
private LinkedList<AudioFile> audioFiles; | |
private int currentPosition; | |
/* | |
Constructorul implicit care initializeaza o PlayList goala | |
*/ | |
public PlayList() { | |
audioFiles = new LinkedList<>(); | |
currentPosition = -1; //pozitie invalida pentru inceput | |
} | |
/* | |
constructor care initializeaza | |
*/ | |
public PlayList(String pathname) { | |
this();//initializeaza obiectul folosind contructorul implicit | |
loadFromM3U(pathname); | |
} | |
/** | |
Adauga un fisier audio in lista | |
@param file fisierul audio de adaugat | |
*/ | |
public void add(AudioFile file) { | |
if(file != null) { | |
audioFiles.add( file ); | |
} | |
} | |
public void remove(AudioFile file) { | |
audioFiles.remove(file); | |
//actualizam currentPosition daca este necesar | |
if(audioFiles.isEmpty()) currentPosition = -1; | |
else if(currentPosition >= audioFiles.size()) { | |
currentPosition = 0; | |
} | |
} | |
public int size() { | |
return audioFiles.size(); | |
} | |
public void setCurrent(int position) { | |
currentPosition = position; | |
} | |
public int getPosition() { | |
return currentPosition; | |
} | |
public void nextSong() { | |
if(audioFiles.isEmpty()) { | |
currentPosition = -1; | |
return; | |
} | |
//incrementeaza pozitia si gestioneaza ciclicitatea | |
currentPosition = ( currentPosition + 1 ) % audioFiles.size(); | |
} | |
public List<AudioFile> getList() { | |
return audioFiles; | |
} | |
public AudioFile currentAudioFile() { | |
if(currentPosition >= 0 && currentPosition < audioFiles.size()) return audioFiles.get(currentPosition); | |
return null; | |
} | |
public void loadFromM3U( String pathname ) { | |
//reset the PlayList | |
audioFiles.clear(); | |
currentPosition = -1; | |
try { | |
String line; | |
while((line=reader.readLine()) != null) { | |
line = line.trim(); | |
//skip comments | |
if(line.isEmpty() || line.startsWith("#")) { | |
continue; | |
} | |
try { | |
AudioFile audioFile = AudioFileFactory.createAudioFile( line ); | |
add( audioFile ); | |
} catch(RuntimeException e) { | |
System.err.println("Skipping file: " + e.getMessage()); | |
} | |
} | |
} catch(IOException e) { | |
//System.err.println("Error loading playlist from " + pathname + ": " + e.getMessage()) | |
} | |
} | |
/** | |
Salveaza lista de redare in format M3U | |
@param pathname calea catre fisierul de iesire | |
*/ | |
public void saveAsM3U( String pathname ) { | |
try { | |
PrintWriter writer = new PrintWriter(new FileWriter(filename)); | |
for( AudioFile file: audioFile ) { | |
writer.println( file.getPathname() ); | |
} | |
} catch(IOEception e) { | |
System.err.println("Error saving playlist to: " + pathname + ": " + e.getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment