0
0

I’m trying to use FSOUND_Stream_SetTime() to fastforward through a song. I’m using FSOUND_Stream_GetTime() to get the current time in the song and add X to it when i call FSOUND_Stream_SetTime()… whenever i do this the stream returns to the beginning of the song. If i call FSOUND_Stream_GetTime() right after completing the set time call i get the correct time position but a few seconds later it returns to time position 0. Does anyone have any ideas about his?

Here is the ugly mess know as my code:
[code:3rinqnp9]

ifndef __CSD_PLAYBACK_H

define __CSD_PLAYBACK_H

include "PlayList.h"

include <fmod.h>

define FMOD_ERROR -1

class PlayBack
{
private:
FSOUND_STREAM* m_pStream; // This is the stream used to play our sounds.
FSOUND_SAMPLE* m_pSample; // This is a sample for the stream used to play our sounds.
PlayList m_PlayList; // This is the play list we are playing from.
// bool m_bPaused; // This is true if we are paused.
const int m_knPrimaryChannel; // This is the main channel we play on.
public:

PlayBack(): m_pStream(NULL), m_knPrimaryChannel(0), m_pSample(NULL)
// This is the default construction.
// The function inits the sound system.
{
    // Init Sound System.
    if(!FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND))
    {
        throw Exception(&quot;PlayBack::PlayBack() Unable to set output deivce&quot;);
    }
    if(!FSOUND_SetDriver(0))
    {
        throw Exception(&quot;PlayBack::PlayBack() Unable to set sound driver&quot;);
    }
    if(!FSOUND_SetMixer(FSOUND_MIXER_QUALITY_AUTODETECT))
    {
        throw Exception(&quot;PlayBack::PlayBack() Unable to set mixer&quot;);
    }
    if(!FSOUND_Init(44100, 1, FSOUND_INIT_GLOBALFOCUS))
    {
        // This should have error handling...(TRUE|FALSE returns).  
        throw Exception(&quot;PlayBack::PlayBack() Unable to init Sound System&quot;);
    }
}// PlayBack()

~PlayBack() { CloseSong(); FSOUND_Close(); }
// Unloads the Sound System.

public:
bool GetPause() { return (0 != FSOUND_GetPaused(m_knPrimaryChannel)); }
// Returns the current paused status.

void    TogglePause() { FSOUND_SetPaused(m_knPrimaryChannel, (GetPause()? false : true)); }
// Toggles the pause status on and off.

void    CloseSong()
// This function close the current stream and sets its pointer to NULL.
// The function takes no action if the stream is already closed.
{ 
    if(m_pStream)
    {
        Stop();
        FSOUND_Stream_Close(m_pStream); // i'm not sure what happens if we close a strean we are currently playing.
    }
//  if(m_pSample)
//  {
//      FSOUND_Sample_Free(m_pSample);
//  }
    m_pSample   = NULL;
    m_pStream   = NULL;
}// CloseSong()

void    OpenSong(String strFileName)
// This function loads a song into the stream.
// strFileName is the path and filename of the song to be opened.
// If the stream is already open it is closed.
{
    CloseSong();
    if(strFileName != &quot;&quot;)
    {
        // Opening the play stream and setting the call back function for when it is finished playing.
        m_pStream   = FSOUND_Stream_Open(strFileName.c_str(),FSOUND_MPEGACCURATE,0,0);      // We are going to need to make sure the file was loaded.
        if(m_pStream)
        {
            if(!FSOUND_Stream_SetEndCallback(m_pStream, *(FSOUND_STREAMCALLBACK)Callback_StreamFinished, (void*)this))  // This should have error handling...(TRUE|FALSE returns).
            {
                throw Exception(&quot;PlayBack::OpenSong() Could not set EndCallback functoin&quot;);
            }
            m_pSample   = FSOUND_Stream_GetSample(m_pStream);
        }
        else
        {
            throw Exception(&quot;PlayBack::OpenSong() Could not open file stream&quot;);
        }
    }
}//    OpenSong()

void    Play()
// This function plays the current song on the play list.
{
    int     nFreq;
    if(GetPause())
    {
        TogglePause();
    }
    else
    {
        OpenSong(m_PlayList.GetCurSong());
        if(m_pStream)
        {
            if(FSOUND_Sample_GetDefaults(m_pSample, &amp;nFreq, NULL, NULL, NULL))
            {
                FSOUND_SetFrequency(m_knPrimaryChannel, nFreq);
                cout &lt;&lt; nFreq &lt;&lt;endl;
            }
            PlayStream();
        }
    }
}// Play()

void    Stop()  { if(m_pStream) FSOUND_Stream_Stop(m_pStream); }
// Stops the current stream from playing.

void    PlayStream()
// This funciton starts the stream playing.
{
    if(FMOD_ERROR == FSOUND_Stream_Play(m_knPrimaryChannel, m_pStream)) // we may want to and some error handling here.
    {
        throw Exception(&quot;PlayBack::PlaySystem() Could not start playing stream&quot;);
    }
}

void    SetTime(int nMillisec)
// This function set the time position in the current song.
// nMillisec is the position in millisec in the stream.
{
    if(!FSOUND_Stream_SetTime(m_pStream, nMillisec))  // we may want to add some error handling here.
    {
        throw Exception(&quot;PlayBack::SetTime() Could not set the time position&quot;);
    }
}// SetTime()

int     GetTime() { return (m_pStream)? FSOUND_Stream_GetTime(m_pStream) : 0; }
// Returns the current time position in the stream.
// If there is no current stream the function returns 0.

int     GetLength() { return (m_pStream)? FSOUND_Stream_GetLengthMs(m_pStream) : 0; }
// This function return the length of the current stream in milliseconds.
// If there is no current stream the function returns 0.

void    FastForward(int nMillisec)
// This function moves ahead in the stream x milliseconds.
// nMillisec is the number of milliseconds to move ahead.
{
    int     nPosition;

    // If we are moving to before the beginning of the stream open the previous song.
    nPosition   = GetTime()+nMillisec;
    cout &lt;&lt; &quot;Time: &quot; &lt;&lt; GetTime() &lt;&lt; &quot;\tNewTime: &quot; &lt;&lt; nPosition &lt;&lt;endl;
    if(GetLength() &lt; nPosition)
    {
        cout &lt;&lt; &quot;End of Song Reached&quot; &lt;&lt; endl;
        OpenSong(m_PlayList.MoveNextSong());
    }
    else
    {
        SetTime(nPosition); // Again with the error handling.
        int i = 0;
        while(i++ &lt; 100000){}
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
    }
}//    FastForward()

void    Rewind(int nMillisec)
// This function moves back in the stream x milliseconds.
// nMillisec is the number of milliseconds to move back.
{
    int     nPosition;

    // If we are moving to before the beginning of the stream open the previous song.
    nPosition   = GetTime()-nMillisec;
    if(0 &gt; nPosition)
    {
        OpenSong(m_PlayList.MovePreSong());
        cout &lt;&lt; &quot;Beginning of Song Reached&quot; &lt;&lt; endl;
    }
    else
    {
        TogglePause();
        SetTime(nPosition); // Again with the error handling.
        TogglePause();
        cout &lt;&lt; &quot;New Time: &quot; &lt;&lt; GetTime() &lt;&lt; endl;
    }
}//    Rewind()

PlayList&amp;   GetPlayListInterface() { return m_PlayList; }
// Returns a ref to the PlayList interface.
// Do not copy!

// Add Call back function here ;-)
static signed char Callback_StreamFinished(FSOUND_STREAM *stream, void *buff, int len, void *userdata)
// When a song finishes this function will be called and should start the next song playing.
{
    PlayBack*   pPlayBack;

    pPlayBack   = (PlayBack*) userdata;

    pPlayBack-&gt;GetPlayListInterface().MoveNextSong();
    pPlayBack-&gt;Play();
    return 0;

}//    Callback_StreamFinished()

};

endif

[/code:3rinqnp9]

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.