ASX Version4.20.14

play/main.c

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

/* $Header: /home/eliot/asi/repo/cvsrepo/Repository/apps/asx/examples/play/main.c,v 1.14 2011/05/04 19:13:02 as-dxb Exp $ */
#include "stdio.h"
#include "stdlib.h"
#ifdef __APPLE__
#include <unistd.h>
#include <asx/asx.h>
#include <asx/asxstring.h>
#define Sleep(t) sleep(t)
#else
#include "windows.h"
#include "asx.h"
#include "asxstring.h"
#endif

ASX_HANDLE hSystem=0;

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

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

    if(argc<2)
    {
        printf("Filename to play is a required parameter.\n");
        return -1;
    }

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

    ASX_System_SetMessageLogging(hSystem,asxMSG_LOGGING_VERBOSE);
    CheckError(hSystem, __LINE__);

    // get the adapter
    ASX_System_GetAdapter(hSystem,nAdapterToUse,&hAdapter);
    CheckError(hAdapter, __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,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
    ASX_Player_Open( hPlayer, argv[1]);
    CheckError(hPlayer, __LINE__);

    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);

#define TEST 1

#if TEST==0
    printf("Waiting for playback to complete\n");
    ASX_Player_Wait(hPlayer);
    CheckError(hPlayer, __LINE__);
#elif TEST==1
    printf("Looping while player state is asxPLAYER_RUNNING\n");
    while(1){
        enum asxPLAYER_STATE s;
        ASX_Player_GetState(hPlayer, &s);
        CheckError(hPlayer, __LINE__);
        if(s!=asxPLAYER_RUNNING) {
            printf("Player state %d\n",(int)s);
            break;
        }
        Sleep(10);
    }
#elif TEST==2
    printf("Pausing 1 second before issuing ASX_Player_Stop()\n");
    Sleep(1);
    printf("Call ASX_Player_Stop()\n");
    ASX_Player_Stop(hPlayer);
#else
#error Bad TEST define value.
#endif

    ASX_Player_GetState(hPlayer, &state);
    ASXSTRING_EnumToString(state,0,0,&nLen);
    pState = (char *)malloc(nLen);
    ASXSTRING_EnumToString(state,pState,nLen,&nLen);
    printf("Playback complete. Final state:%s\n",pState);

    // 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 *pstate);
ASX32_API ASX_ERROR ASX_Player_SetTimeScale( ASX_HANDLE hPlayer, float fScaleFactor);
*/

    printf("Press ENTER to exit\n");
    getchar();
    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: #%d, %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;
}