| 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <libgen.h> |
| 5 | #include "raylib.h" |
| 6 | |
| 7 | int |
| 8 | main (int argc, char **argv) |
| 9 | { |
| 10 | const int width = 1280; |
| 11 | const int height = 720; |
| 12 | |
| 13 | InitWindow (width, height, "moosic palyer"); |
| 14 | SetTargetFPS (60); |
| 15 | InitAudioDevice(); |
| 16 | |
| 17 | Music song; |
| 18 | if (argc > 1) |
| 19 | { |
| 20 | song = LoadMusicStream(argv[1]); |
| 21 | PlayMusicStream(song); |
| 22 | } |
| 23 | else { |
| 24 | |
| 25 | FILE *f = popen("zenity --file-selection", "r"); |
| 26 | if (f != NULL) |
| 27 | { |
| 28 | char song_path[1024]; |
| 29 | |
| 30 | if (fgets(song_path, sizeof(song_path), f) != NULL) |
| 31 | { |
| 32 | song_path[strcspn(song_path, "\n")] = 0; |
| 33 | song = LoadMusicStream(song_path); |
| 34 | PlayMusicStream(song); |
| 35 | } |
| 36 | pclose(f); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | while (!WindowShouldClose ()) |
| 41 | { |
| 42 | UpdateMusicStream (song); |
| 43 | float vol = GetMasterVolume (); |
| 44 | |
| 45 | if (IsKeyPressed (KEY_P) == true && IsMusicStreamPlaying (song)) |
| 46 | { |
| 47 | PauseMusicStream (song); |
| 48 | } |
| 49 | else if (IsKeyPressed (KEY_P) == true && !IsMusicStreamPlaying (song)) |
| 50 | { |
| 51 | ResumeMusicStream (song); |
| 52 | } |
| 53 | else if (IsKeyPressed (KEY_EQUAL) == true && IsMusicStreamPlaying (song)) |
| 54 | { |
| 55 | SetMasterVolume (vol + 0.1f); |
| 56 | } |
| 57 | else if (IsKeyPressed (KEY_MINUS) == true && IsMusicStreamPlaying (song)) |
| 58 | { |
| 59 | SetMasterVolume (vol - 0.1f); |
| 60 | } |
| 61 | |
| 62 | BeginDrawing (); |
| 63 | ClearBackground (BLACK); |
| 64 | |
| 65 | char path_copy[1024]; |
| 66 | char song_path[1024]; |
| 67 | strncpy(path_copy, song_path, sizeof(path_copy)); |
| 68 | char *filename = basename(path_copy); |
| 69 | |
| 70 | |
| 71 | |
| 72 | const char *text = !IsMusicStreamPlaying(song) |
| 73 | ? "paused :<" |
| 74 | : TextFormat("currently playing %s :D\n Volume: %.2f", filename, vol); |
| 75 | int font_size = 24; |
| 76 | int text_width = MeasureText(text, font_size); |
| 77 | |
| 78 | int text_x = (width - text_width) / 2; |
| 79 | int text_y = (height - font_size) / 2; |
| 80 | |
| 81 | |
| 82 | DrawText(text, text_x, text_y , font_size, RAYWHITE); |
| 83 | |
| 84 | EndDrawing (); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | UnloadMusicStream(song); |
| 89 | CloseAudioDevice (); |
| 90 | CloseWindow (); |
| 91 | return 0; |
| 92 | } |