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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include "raylib.h"
#include <string.h>
#include <ctype.h>
typedef struct {
Music music;
bool loaded;
bool playing;
float duration;
char filename[256];
} PlayerState;
static int strcasecmp_ext(const char *a, const char *b)
{
while (*a && *b) {
if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) return 1;
a++; b++;
}
return *a != *b;
}
static const char *basename_from_path(const char *path)
{
const char *last = path;
for (const char *p = path; *p; p++) {
if (*p == '/' || *p == '\\') last = p + 1;
}
return last;
}
static void seek_to(PlayerState *s, float target)
{
if (target < 0.0f) target = 0.0f;
if (target > s->duration) target = s->duration;
SeekMusicStream(s->music, target);
}
int main(void)
{
InitWindow(1920, 1080, "Study Player");
InitAudioDevice();
SetTargetFPS(60);
PlayerState state = { 0 };
while (!WindowShouldClose())
{
/* --- 1.1 Drag & drop file loading --- */
if (IsFileDropped())
{
FilePathList files = LoadDroppedFiles();
if (files.count > 0)
{
const char *path = files.paths[0];
const char *ext = GetFileExtension(path);
if (ext != NULL && strcasecmp_ext(ext, ".mp3") == 0)
{
/* Unload previous */
if (state.loaded)
{
StopMusicStream(state.music);
UnloadMusicStream(state.music);
state.loaded = false;
state.playing = false;
}
state.music = LoadMusicStream(path);
state.duration = GetMusicTimeLength(state.music);
state.loaded = true;
state.playing = true;
const char *base = basename_from_path(path);
strncpy(state.filename, base, sizeof(state.filename) - 1);
state.filename[sizeof(state.filename) - 1] = '\0';
PlayMusicStream(state.music);
}
}
UnloadDroppedFiles(files);
}
/* --- 1.4 Music stream update --- */
if (state.loaded)
{
UpdateMusicStream(state.music);
}
/* --- 1.2 Play/pause with rewind --- */
if (state.loaded && IsKeyPressed(KEY_SPACE))
{
if (state.playing)
{
float pos = GetMusicTimePlayed(state.music);
PauseMusicStream(state.music);
float rewind = pos - 1.0f;
if (rewind < 0.0f) rewind = 0.0f;
SeekMusicStream(state.music, rewind);
state.playing = false;
}
else
{
ResumeMusicStream(state.music);
state.playing = true;
}
}
/* --- 1.3 Arrow key seeking --- */
if (state.loaded && IsKeyPressed(KEY_LEFT))
{
float pos = GetMusicTimePlayed(state.music);
seek_to(&state, pos - 5.0f);
}
if (state.loaded && IsKeyPressed(KEY_RIGHT))
{
float pos = GetMusicTimePlayed(state.music);
seek_to(&state, pos + 5.0f);
}
/* --- Drawing --- */
BeginDrawing();
ClearBackground((Color){ 26, 26, 46, 255 });
if (state.loaded)
{
DrawText(state.filename, 40, 40, 40, RAYWHITE);
DrawText(state.playing ? "Playing" : "Paused", 40, 90, 30, GRAY);
}
else
{
DrawText("Drop an MP3 file here to play", 540, 340, 40, RAYWHITE);
}
EndDrawing();
}
if (state.loaded)
{
StopMusicStream(state.music);
UnloadMusicStream(state.music);
}
CloseAudioDevice();
CloseWindow();
return 0;
}
|