SFML audio streaming issues

I am trying to use SFML to load sounds. I got basic sounds to work, but longer audio files don't work. The tutorial told me to use audio streams. This is the code they gave me:
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
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
#include <iostream>


////////////////////////////////////////////////////////////
/// Customized sound stream for acquiring audio data
/// from memory (completely useless, just to illustrate the
/// streaming techniques)
////////////////////////////////////////////////////////////
class MyCustomStream : public sf::SoundStream
{
public :

    ////////////////////////////////////////////////////////////
    /// Default constructor
    ///
    /// \param BufferSize : Size of the internal audio buffer
    ///
    ////////////////////////////////////////////////////////////
    MyCustomStream(std::size_t BufferSize) :
    myOffset    (0),
    myBufferSize(BufferSize)
    {

    }

    ////////////////////////////////////////////////////////////
    /// Load sound data from a file
    ///
    /// \param Filename : File to load
    ///
    ////////////////////////////////////////////////////////////
    bool Open(const std::string& Filename)
    {
        // Load the sound data into a sound buffer
        sf::SoundBuffer SoundData;
        if (SoundData.LoadFromFile(Filename))
        {
            // Initialize the stream with the sound parameters
            Initialize(SoundData.GetChannelsCount(), SoundData.GetSampleRate());

            // Copy the audio samples into our internal array
            const sf::Int16* Data = SoundData.GetSamples();
            myBuffer.assign(Data, Data + SoundData.GetSamplesCount());

            return true;
        }

        return false;
    }

private :

    ////////////////////////////////////////////////////////////
    /// /see sfSoundStream::OnStart
    ///
    ////////////////////////////////////////////////////////////
    virtual bool OnStart()
    {
        // Reset the read offset
        myOffset = 0;

        return true;
    }

    ////////////////////////////////////////////////////////////
    /// /see sfSoundStream::OnGetData
    ///
    ////////////////////////////////////////////////////////////
    virtual bool OnGetData(sf::SoundStream::Chunk& Data)
    {
        // Check if there is enough data to stream
        if (myOffset + myBufferSize >= myBuffer.size())
        {
            // Returning false means that we want to stop playing the stream
            return false;
        }

        // Fill the stream chunk with a pointer to the audio data and the number of samples to stream
        Data.Samples   = &myBuffer[myOffset];
        Data.NbSamples = myBufferSize;

        // Update the read offset
        myOffset += myBufferSize;

        return true;
    }

    ////////////////////////////////////////////////////////////
    // Member data
    ////////////////////////////////////////////////////////////
    std::vector<sf::Int16> myBuffer;     ///< Internal buffer that holds audio samples
    std::size_t            myOffset;     ///< Read offset in the sample array
    std::size_t            myBufferSize; ///< Size of the audio data to stream
};


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create an instance of our custom audio stream
    // Try different (lower) values to hear the effect
    MyCustomStream Stream(40000);

    // Open a sound file
    if (!Stream.Open("music.ogg"))
        return EXIT_FAILURE;

    // Play it
    Stream.Play();
    std::cout << "Playing 5 seconds of audio data..." << std::endl;

    // Stop it after 5 seconds
    sf::Sleep(5.f);
    Stream.Stop();

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;
}

The code compiles without errors, but when it runs it says
Failed to load sound buffer from file

It works right when I use a small wav file, but I have tried larger files (which this is intended for) and it does not work. I have tried multiple file types and got the same issue.
What is wrong and how do I fix it?
I just tried this:
1
2
3
4
5
6
7
8
9
10
11
12
sf::Music Music1;
    if (!Music1.OpenFromFile("Music.ogg"))
    {
        // Error...
        std::cout<<"Error...";
    }
    else
    {
        std::cout<<"Opened.";
        Music1.Play();
        while (true){}
    }
inside int main() and it compiles and plays the file. When would the first(streaming) code be needed?
Topic archived. No new replies allowed.