ASX Version4.20.14

playlist/main.c

This is an example of how to use the ASX Player to play filelists.

/* $Header: /home/eliot/asi/repo/cvsrepo/Repository/apps/asx/examples/playlist/main.c,v 1.6 2010/01/19 14:49:46 as-tfe Exp $ */
#include "stdio.h"
#include "stdlib.h"
#include "asx.h"
#include "asxstring.h"

ASX_HANDLE hSystem=0;
int list_done = 0;

void PrintControlName(ASX_HANDLE hControl);
int CheckError(ASX_HANDLE hObj, int nLine);

void PlayerCallback
(
    ASX_HANDLE hASX_Player_Object,
    const enum asxPLAYER_FLAGS flags,
    void *pUser1
)   
{
    if(flags & asxPLAYER_FILE_COMPLETE){
        printf("File done callback\n");
    }
    if(flags & asxPLAYER_FILELIST_COMPLETE){
        printf("File list done callback\n");
        list_done = 1;
    }
    if(flags & asxPLAYER_FILE_START){
        char *szCurrentFilename = NULL;
        ASX_Player_PlaylistStatus( hASX_Player_Object,
                    NULL,
                    NULL,
                    &szCurrentFilename,
                    NULL,
                    NULL );
        printf("File %s started\n",szCurrentFilename);
    }
}

void ShowStatus
(
    ASX_HANDLE hASX_Player_Object
)
{
    unsigned int nTotalFileCount = 0;
    int nCurrentFile = 0;
    char *szCurrentFilename = NULL;
    unsigned int nTotalTime_ms = 0;
    unsigned int nCurrentTime_ms = 0;
    
    static unsigned int nLastTime_ms = 0;

    ASX_Player_PlaylistStatus( hASX_Player_Object,
                &nTotalFileCount,
                &nCurrentFile,
                &szCurrentFilename,
                &nTotalTime_ms,
                &nCurrentTime_ms );

    if(nCurrentTime_ms - nLastTime_ms > 50){
        printf("Playing \"%s\" (file %d of %d) pos %f of %f\n",
            szCurrentFilename,nCurrentFile+1,nTotalFileCount,
            (float)nCurrentTime_ms/1000.0,(float)nTotalTime_ms/1000.0);
        nLastTime_ms = nCurrentTime_ms;
    }
}

char *m_buf = NULL;
char **m_list = NULL;
int m_count = 0;

ASX_ERROR OpenPlaylist
(
    ASX_HANDLE hPlayer,
    const char* pFilename
)
{
    ASX_ERROR result = asxERROR_PLAYER_NOFILE;
    int i,j;
    fpos_t len;
    FILE *fp;

    fp = fopen(pFilename,"r");
    if(!fp)
        return result;
    fseek(fp,0,SEEK_END);
    fgetpos(fp,&len);
    fseek(fp,0,SEEK_SET);

    if(len){
        m_buf = (char *)malloc(len+1);
        fread(m_buf,1,len,fp);
        m_count=1;
        for(i=0;i<len;i++){
            if(m_buf[i]=='\n')
                m_count++;
        }
        m_list = (char **)malloc(sizeof(char *)*m_count);
        j = 0;
        m_list[0] = m_buf;
        for(i=0;i<len;i++){
            if(m_buf[i]=='\n'){
                j++;
                m_buf[i]=0;
                if(j<m_count)
                    m_list[j] = m_buf + i + 1;
            }
        }
        m_buf[len] = 0;
        // Skip leading whitespace and remove empty strings
        for(j=0;j<m_count;j++){
            while(m_list[j][0]==' '||m_list[j][0]=='\t')
                m_list[j]++;
            if(m_list[j][0]==0){
                for(i=j;i<m_count-1;i++)
                    m_list[i] = m_list[i+1];
                m_count--;
                j--;
            }
        }
        result = ASX_Player_OpenPlaylist(hPlayer,m_list,m_count);
    }
    fclose(fp);

    return result;
}

void FreePlaylist(void)
{
    if(m_buf)
        free(m_buf);
    if(m_list)
        free(m_list);
}

int main(int argc, char* argv[])
{
    char *pszName;
    char *pszFormat;
    char *pDummy;
    char *pExt;
    ASX_HANDLE hAdapter;
    ASX_HANDLE hMixer;
    ASX_HANDLE hPlayer;
    ASX_ERROR asxError;
    int nAdapterToUse=0;
    int nLen;

    if(argc<2)
    {
        printf( "Filename to play is a required parameter.\n"
            "Filename should be either an audio file (i.e. .WAV or .MP3) or a plain text\n"
            "file (with a .TXT extension) containing a list of filenames, one per line.\n");
        return -1;
    }

    // create the system
    ASX_System_Create(ASX_SYSTEM_TYPE_HPI,&hSystem);
    CheckError(hSystem, __LINE__);

    // get the adapter
    ASX_System_GetAdapter(hSystem,nAdapterToUse,&hAdapter);
    CheckError(hSystem, __LINE__);

    ASX_Adapter_GetName(hAdapter,0,0,&nLen);
    CheckError(hAdapter, __LINE__);
    pszName = (char *)malloc(nLen);
    ASX_Adapter_GetName(hAdapter,pszName,nLen,&nLen);
    CheckError(hAdapter, __LINE__);
    printf("Adapter [%d] is %s \n", nAdapterToUse,pszName);

    // get the mixer handle
    ASX_Adapter_GetMixer( hAdapter, &hMixer );
    CheckError(hAdapter, __LINE__);

    // get a player object
    asxError = ASX_Mixer_GetControlByNodeTypeAndIndex(
        hMixer,
        asxNODE_PLAYER,(argc>3)?strtol(argv[3],&pDummy,10):0,
        0,0,
        asxCONTROL_PLAYER,
        &hPlayer);
    CheckError(hMixer, __LINE__);

    // print out some control details
    PrintControlName(hPlayer);

    // open the player and pass in the file to be played
    // check for '.txt' extension
    pExt = strrchr(argv[1],'.');
    if(_stricmp(pExt,".txt")==0){
        OpenPlaylist( hPlayer, argv[1]);
    }else{
        ASX_Player_Open( hPlayer, argv[1]);
    }
    CheckError(hPlayer, __LINE__);

    ASX_Player_RegisterCallback(hPlayer,PlayerCallback,
    asxPLAYER_FILE_COMPLETE | asxPLAYER_FILELIST_COMPLETE | asxPLAYER_FILE_START,NULL); 

    if(argc>2)
    {
        printf("Start at offset %s milliseconds\n",argv[2]);
        ASX_Player_PreLoad(hPlayer,asxTIMESCALE_MILLISECONDS,strtol(argv[2],&pDummy,10));
    }

    // start playing the file at offset 0 seconds.
    ASX_Player_Start( hPlayer );
    CheckError(hPlayer, __LINE__);

    ASX_Player_Format_GetString(hPlayer, &pszFormat);
    printf("Playing %s Format %s on Device %s\n",argv[1],pszFormat,pszName);
    free(pszName);

    // wait for playback completion
    printf("Waiting for playback to complete\n");
    //while(!list_done)
    //  ShowStatus(hPlayer);
    ASX_Player_PlaylistWait(hPlayer);
    CheckError(hPlayer, __LINE__);

    printf("Playback complete.\n");

    // close the file being played
    ASX_Player_Close(hPlayer);
    CheckError(hPlayer, __LINE__);

/*

Not tested....

// start position other than 0
ASX32_API ASX_ERROR ASX_Player_Start( ASX_HANDLE hPlayer, float fPosition);
ASX32_API ASX_ERROR ASX_Player_Pause( ASX_HANDLE hPlayer);
ASX32_API ASX_ERROR ASX_Player_Stop( ASX_HANDLE hPlayer);
ASX32_API ASX_ERROR ASX_Player_GetPosition( ASX_HANDLE hPlayer, float *pfPosition);
ASX32_API ASX_ERROR ASX_Player_GetState( ASX_HANDLE hPlayer, int *pnState);
ASX32_API ASX_ERROR ASX_Player_SetTimeScale( ASX_HANDLE hPlayer, float fScaleFactor);
*/

    printf("Press ENTER to exit\n");
    getchar();
    FreePlaylist();
    ASX_System_Delete(hSystem);
    return 0;
}

void PrintControlName(ASX_HANDLE hControl)
{
    char *pszName;
    int nLen;
    enum asxCONTROL eControl;

    ASX_Control_GetType(hControl, &eControl);
    ASXSTRING_EnumToString(eControl,0,0,&nLen);
    pszName=(char *)malloc(nLen);
    ASXSTRING_EnumToString(eControl,pszName,nLen,&nLen);
    printf("Control : %s\n",pszName);

    free(pszName);
}
int CheckError(ASX_HANDLE hObj, int nLine)
{
    int nError;
    int asxSubSystemErrorCode=0;
    char *pszAsxErrorString;
    char *pszAsxSubSystemErrorString;
    int nLen1,nLen2;

    ASX_Error_GetLast( hObj, (ASX_ERROR*)&nError, &asxSubSystemErrorCode);
    if(!nError)
        return 0;
    ASX_Error_GetLastString( hObj, 0,0,&nLen1,0,0,&nLen2);
    pszAsxErrorString = (char *)malloc(nLen1);
    pszAsxSubSystemErrorString = (char *)malloc(nLen2);
    ASX_Error_GetLastString( hObj, pszAsxErrorString,nLen1,&nLen1,pszAsxSubSystemErrorString,nLen2,&nLen2);
    printf("Error: #%d, %s - Subsystem Error: #%ld, %s \n",
        nError,
        pszAsxErrorString,
        asxSubSystemErrorCode,
        pszAsxSubSystemErrorString );
    printf("When called from source %s line %d\n",__FILE__,nLine);

    printf("Press ENTER to exit\n");
    getchar();
    free(pszAsxErrorString);
    free(pszAsxSubSystemErrorString);
    ASX_System_Delete(hSystem);
    exit(1);
    return 1;
}