1/*******************************************************************************************
2*
3* raylib [audio] example - Music playing (streaming)
4*
5* This example has been created using raylib 1.3 (www.raylib.com)
6* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7*
8* Copyright (c) 2015 Ramon Santamaria (@raysan5)
9*
10********************************************************************************************/
11
12#include "raylib.h"
13
14int main(void)
15{
16 // Initialization
17 //--------------------------------------------------------------------------------------
18 const int screenWidth = 800;
19 const int screenHeight = 450;
20
21 InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)");
22
23 InitAudioDevice(); // Initialize audio device
24
25 Music music = LoadMusicStream("resources/guitar_noodling.ogg");
26
27 PlayMusicStream(music);
28
29 float timePlayed = 0.0f;
30 bool pause = false;
31
32 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
33 //--------------------------------------------------------------------------------------
34
35 // Main game loop
36 while (!WindowShouldClose()) // Detect window close button or ESC key
37 {
38 // Update
39 //----------------------------------------------------------------------------------
40 UpdateMusicStream(music); // Update music buffer with new stream data
41
42 // Restart music playing (stop and play)
43 if (IsKeyPressed(KEY_SPACE))
44 {
45 StopMusicStream(music);
46 PlayMusicStream(music);
47 }
48
49 // Pause/Resume music playing
50 if (IsKeyPressed(KEY_P))
51 {
52 pause = !pause;
53
54 if (pause) PauseMusicStream(music);
55 else ResumeMusicStream(music);
56 }
57
58 // Get timePlayed scaled to bar dimensions (400 pixels)
59 timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*400;
60
61 if (timePlayed > 400) StopMusicStream(music);
62 //----------------------------------------------------------------------------------
63
64 // Draw
65 //----------------------------------------------------------------------------------
66 BeginDrawing();
67
68 ClearBackground(RAYWHITE);
69
70 DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, LIGHTGRAY);
71
72 DrawRectangle(200, 200, 400, 12, LIGHTGRAY);
73 DrawRectangle(200, 200, (int)timePlayed, 12, MAROON);
74 DrawRectangleLines(200, 200, 400, 12, GRAY);
75
76 DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, LIGHTGRAY);
77 DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, LIGHTGRAY);
78
79 EndDrawing();
80 //----------------------------------------------------------------------------------
81 }
82
83 // De-Initialization
84 //--------------------------------------------------------------------------------------
85 UnloadMusicStream(music); // Unload music stream buffers from RAM
86
87 CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
88
89 CloseWindow(); // Close window and OpenGL context
90 //--------------------------------------------------------------------------------------
91
92 return 0;
93}