This repository was archived by the owner on Jul 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
130 lines (118 loc) · 3.59 KB
/
Player.java
File metadata and controls
130 lines (118 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.ArrayList;
/**
* This class handles play, stop, loading of tracks and displays information about current tracks.
*
* @author (Slagnes, Kjell-Olaf)
* @version (1.0)
*/
public class Player
{
private PlayList playList;
private Track track; // Create an object track of the class Track.
private String trackName = ""; // Sets an empty string trackName.
private int tracksPlayed, totalPlayLength = 0;
private double totalTracks, totalDuration, avgTrackLength = 0;
/**
* Create a playlist with tracks from a folder named audio.
*/
public Player()
{
playList = new PlayList("audio");
}
/**
* Return the track collection currently loaded in this player.
*/
public PlayList getPlayList()
{
return playList;
}
/**
* Play the track selected.
*/
public void play()
{
// Check if a track is loaded, then play if a track is loaded.
if(track != null){
track.rewind(); //Reset the track so it can be played multiple times.
track.play(); // Play the track.
tracksPlayed++; // When a track is played tracksPlayed incremented by 1.
totalPlayLength += track.getDuration(); // When a track is played add the duration of the track to totalPlayLength.
averageTrackLength(); // Call the method averageTrackLength.
}
}
/**
* Stop the track selected.
*/
public void stop()
{
// If a track is playing stop it. If no track is loaded notify in console.
if(track != null){
track.stop();
}
}
/**
* Stop the track currently playing, then load the track selected.
*/
public void setTrack(int trackNumber)
{
stop(); // If a track is playing stop it. Then load track selected.
track = playList.getTrack(trackNumber);
}
/**
* Return track name. If no track is selected an empty string is returned.
*/
public String getTrackName()
{
// Set the string trackName to the track loaded.
if(track != null){
trackName = track.getName();
}
return trackName;
}
/**
* Return information about the currently selected track. The information
* contains the track name and playing time, in the format
* track-name (playing time)
* Return an empty string if no track is selected.
*/
public String getTrackInfo()
{
// Sets an empty string trackInfo, if a track is loaded set trackInfo to "trackName (track-duration)".
String trackInfo = "";
if(track != null){
trackInfo = getTrackName() + " (" + track.getDuration() + ") ";
}
return trackInfo;
}
/**
* Return the number of tracks played since the program started.
*/
public int getNumberOfTracksPlayed(){
return tracksPlayed;
}
/**
* Return the total play time of songs played.
*/
public int getTotalPlayedTrackLength(){
return totalPlayLength;
}
/**
* Return the average track length. If no tracks have been played return 0.
*/
public double averageTrackLength(){
totalTracks = getTotalPlayedTrackLength();
totalDuration = getNumberOfTracksPlayed();
if(totalTracks != 0){
avgTrackLength = totalTracks / totalDuration;
}
return avgTrackLength;
}
/**
* Set statistics values to zero.
*/
public void resetStatistics(){
tracksPlayed = 0;
totalPlayLength = 0;
avgTrackLength = 0;
}
}