×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Question Wave Player using CMdaAudioPlayerUtility

    Hi all,
    I'm trying to make a console application for playing wave file using CMdaAudioPlayerUtility class. I tried many ways but I'm not getting any audio.
    I guessed intialization might be taking sometime so I used a variable to store the state (of the player) and created a function Play in my player class (class has CMdaAudioPlayerUtility* iPlayer as private member) in which I waited for the state variable to be turned to EReadyToPlay from ENotReady(that was intially set in constructor) and then called Play of the CMdaAudioPlayerUtility class through my private variable(pointer).

    I had earlier tried to play the file in the MapcInitComplete (the code has been left commented)

    I'll present the code for clarity

    Header file:

    Code:
    /*
    * ============================================================================
    *  Name     : Playerapp.h
    *  Part of  : Playerapp
    *  Created  : 09.04.2007 by 
    *  Description:
    *     Exe header file
    *  Version  :
    *  Copyright: 
    * ============================================================================
    */
    
    #ifndef __PLAYERAPP_H__
    #define __PLAYERAPP_H__
    
    
    //  Include Files
    
    #include <e32base.h>
    #include <MdaAudioSamplePlayer.h>
    
    //Class Declarations
    class CPlayer	:	public CBase,
    					public MMdaAudioPlayerCallback
    {
    public:
    	static CPlayer* NewL(const TDesC& aFileName);
    	CPlayer();
    	~CPlayer();
    	
        /**
        * PlayL
        * Begin playback of the audio sample. 
        */      
        void Play();
    
        /**
        * StopL
        * Stop playback of the audio sample.
        * Note that this implementation of the virtual function does not leave.
        */
        void Stop();
    
    //From Class MMdaAudioPlayerCallback
    public:
        /**
        * MapcInitComplete
        * Handle the event when initialisation of 
        * the audio player utility is complete.
        * @param aError The status of the audio sample after initialisation
        * @param aDuration The duration of the sample
        */
        void MapcInitComplete( TInt aError, 
                               const TTimeIntervalMicroSeconds& aDuration );
    
        /**
        * MapcPlayComplete
        * Handle the event when when the audio player utility
        * completes asynchronous playing.
        * @param aError The status of playback
        */
        void MapcPlayComplete( TInt aError );
    
    private:
    
    	//private functions
    	void ConstructL(const TDesC& aFileName);
    
    	//data
    
        /**
        * TState
        * The application state
        * - ENotReady Not ready to play
        * - EReadyToPlay Ready to play
        * - EPlaying Playing
        */
    	enum TState
        {
            ENotReady,
            EReadyToPlay,
            EPlaying
        };
    
        /** iState The current state of the audio player utility. **/    
        TState iState;
    
        /**
        * iMdaAudioPlayerUtility. The audio player utility object.
        * owned by CPlayer object.
        */
    
    	CMdaAudioPlayerUtility* iPlayer;
    };
    
    //  Function Prototypes
    
    GLDEF_C TInt E32Main();
    
    
    #endif  // __PLAYERAPP_H__
    
    // End of file
    and implementation file is

    Code:
    /*
    * ============================================================================
    *  Name     : Playerapp.cpp
    *  Part of  : Playerapp
    *  Created  : 09.04.2007 by 
    *  Description:
    *     Exe source file
    *  Version  :
    *  Copyright: 
    * ============================================================================
    */
    
    
    //  Include Files  
    
    #include "Playerapp.h"
    #include <f32file.h>
    #include <e32base.h>
    #include <e32std.h>
    #include <e32cons.h>            // Console
    
    
    //  Constants
    
    _LIT(KTextConsoleTitle, "Console");
    _LIT(KTextFailed, " failed, leave code = %d");
    _LIT(KTextPressAnyKey, " [press any key]\n");
    
    
    //  Global Variables
    
    LOCAL_D CConsoleBase* console;  // write all messages to this
    
    //Class Definition
    
    CPlayer::CPlayer():iState(ENotReady)	//Intially NotReady
    {
    	//No Implementation required
    }
    
    CPlayer::~CPlayer()
    {
        iPlayer->Close();
        delete iPlayer;    
    }
    
    CPlayer* CPlayer::NewL(const TDesC& aFileName)
    {
        CPlayer* self = new ( ELeave ) CPlayer();
        CleanupStack::PushL( self );
        self->ConstructL( aFileName );
        CleanupStack::Pop( self );  
        return self;
    }
    
    void CPlayer::ConstructL(const TDesC& aFileName)
    {
        // on emulator, the sound files used by application are 
        // deployed onto c: drive
        _LIT( tFullPath,"c:\\system\\apps\\sound\\" );
        TParse parse;
    
        // use tparse to get the full path to sound file
        parse.Set( tFullPath, &aFileName,  NULL );  
        TFileName tFullFileName = parse.FullName();
    
        // Create an audio player utility instance for playing 
        // sample data from a file,
        // causes MMdaAudioPlayerCallback::MapcInitComplete to be called
        iPlayer = CMdaAudioPlayerUtility::NewFilePlayerL( tFullFileName, *this );
    }
    
    void CPlayer::Play()
    {
        while(EReadyToPlay != iState);
    
        iState = EPlaying;
        iPlayer->Play();
    }
    
    void CPlayer::Stop()
    {
        iPlayer->Stop();
        iState = EReadyToPlay;
    }
    
    void CPlayer::MapcInitComplete( TInt aError,
                                     const TTimeIntervalMicroSeconds& /*aDuration*/ )
    {
    	iState = aError ? ENotReady : EReadyToPlay;
    
    /*	if (!aError)
                    iPlayer->Play();*/
    
    /*	_LIT(KInitFailed, " Initialization failed, error code = %d");
    	if(aError)
    	console->Printf(KInitFailed,aError);
    */
    }
    
    void CPlayer::MapcPlayComplete( TInt aError )
    {
    	iState = aError ? ENotReady : EReadyToPlay;
    
    /*
    	_LIT(KStopped, " Playback stopped, error code = %d");
    	console->Printf(KStopped,aError);
    */
    }
    
    
    
    
    //  Local Functions
    
    LOCAL_C void MainL(const TDesC& aArgs)
    {
        //
        // add your program code here, example code below
        //
    	_LIT(KFileName, "check.wav");
    
    	CPlayer *player = CPlayer::NewL(KFileName);
        CleanupStack::PushL(player);
    	player->Play();
        CleanupStack::PopAndDestroy();
    }
    
    
    LOCAL_C void DoStartL()
    {
        // Create active scheduler (to run active objects)
        CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
        CleanupStack::PushL(scheduler);
        CActiveScheduler::Install(scheduler);
    
        // Call main function with command line
        TBuf<256> cmdLine;
        RProcess().CommandLine(cmdLine);
        MainL(cmdLine);
    
        // Delete active scheduler
        CleanupStack::PopAndDestroy(scheduler);
    }
    
    
    //  Global Functions
    
    GLDEF_C TInt E32Main()
    {
        // Create cleanup stack
        __UHEAP_MARK;
        CTrapCleanup* cleanup = CTrapCleanup::New();
    
        // Create output console
        TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
        if (createError)
            return createError;
    
        // Run application code inside TRAP harness, wait keypress when terminated
        TRAPD(mainError, DoStartL());
        if (mainError)
            console->Printf(KTextFailed, mainError);
        console->Printf(KTextPressAnyKey);
        console->Getch();
        
        delete console;
        delete cleanup;
        __UHEAP_MARKEND;
        return KErrNone;
    }
    
    // End of file
    I'm running this application on S60 SDK 2nd Ed, FP2 emulator (on win2000 SP4) and my audiofile "check.wav" is placed in c:\system\apps\sound under symbian directory (i.e. absolutepath according to windows for sound file will be "E:\symbian\8.0a\S60_2nd_FP2\epoc3\wins\c\System\Apps\sound\check.wav") as I guess the player running on emulator looks for file in this directory only.
    But I'm not getting any audio, the player just hangs at a point in the Play function where I'm waiting for the state to change to EReadyToPlay.

    I hope I'm clear. Help from anyone will be highly appreciated.

    Thanks,
    deCoder

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Thumbs up Re: Wave Player using CMdaAudioPlayerUtility

    Thanks to any one who gave any thought on this but I've found the problem as well as solution for the same. I will mention these so that anyone hitting the same problem may get some help.

    Problem:
    As mentioned in my problem the intialization may take up some time hence the provision for callback function MapcInitComplete has been given. Now bearing into mind that MapcInitComplete is asynchronously called with Play we find that Play is stuck in while loop while callback function never has the CPU and hence the solution lies in the use of clever use of activescheduler so that the play could be delayed till the intialization is done while letting the CPU to MapcInitComplete

    Solution:
    Just start the scheduler after intialization in ConstructL i.e. after call of NewFilePlayerL
    Now we know that by this time MapcInitComplete has been asynchronously called. Here after setting the state to Ready we stop the scheduler so that it proceeds.
    Now Play of our class is called. Here it finds state as Ready and hence tries to play the file. Here again we start activescheduler because it might take sometime (maybe for intializing internal buffers).
    Again we know as the play is complete the callback function MapcPlayComplete returns so if no error occurs we initialize the state back to ready (else indicate error) and stop the scheduler finally.

    This is followed by the clean up code from the caller of Play and application exits normally.


    This worked for me hope this helps others.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    6

    Re: Wave Player using CMdaAudioPlayerUtility

    This post is very useful for me to develop my first S60 Application.
    Decoder,
    Could you please update the code with what you explain in solution(Text above).

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Re: Wave Player using CMdaAudioPlayerUtility

    Hi jaganpes,
    I'll post the code (implementation file since the header file remains the same) for you.

    Code:
    /*
    * ============================================================================
    *  Name     : Playerapp.cpp
    *  Part of  : Playerapp
    *  Created  : 09.04.2007
    *  Description:
    *     Exe source file
    *  Version  :
    *  Copyright: 
    * ============================================================================
    */
    
    
    //  Include Files  
    
    #include "Playerapp.h"
    #include <f32file.h>
    #include <e32base.h>
    #include <e32std.h>
    #include <e32cons.h>            // Console
    
    
    //  Constants
    
    _LIT(KTextConsoleTitle, "Console");
    _LIT(KTextFailed, " failed, leave code = %d");
    _LIT(KTextPressAnyKey, " [press any key]\n");
    
    
    //  Global Variables
    
    LOCAL_D CConsoleBase* console;  // write all messages to this
    
    //Class Definition
    
    CPlayer::CPlayer():iState(ENotReady)	//Intially NotReady
    {
    	//No Implementation required
    }
    
    CPlayer::~CPlayer()
    {
        iPlayer->Close();
       delete iPlayer;    
    }
    
    CPlayer* CPlayer::NewL(const TDesC& aFileName)
    {
        CPlayer* self = new ( ELeave ) CPlayer();
        CleanupStack::PushL( self );
        self->ConstructL( aFileName );
        CleanupStack::Pop( self );  
        return self;
    }
    
    void CPlayer::ConstructL(const TDesC& aFileName)
    {
        // on emulator, the sound files used by application are 
        // deployed onto c: drive
        _LIT( tFullPath,"c:\\system\\apps\\sound\\" );
        TParse parse;
    
        // use tparse to get the full path to sound file
        parse.Set( tFullPath, &aFileName,  NULL );  
        TFileName tFullFileName = parse.FullName();
    
        // Create an audio player utility instance for playing 
        // sample data from a file,
        // causes MMdaAudioPlayerCallback::MapcInitComplete to be called
        iPlayer = CMdaAudioPlayerUtility::NewFilePlayerL( tFullFileName, *this );
        CActiveScheduler::Start();
    }
    
    
    void CPlayer::Play()
    {
        if(EReadyToPlay == iState)
    	{
    		iState = EPlaying;
    		iPlayer->Play();
    		CActiveScheduler::Start();
    	}
    }
    
    void CPlayer::MapcInitComplete( TInt aError,
                                     const TTimeIntervalMicroSeconds& /*aDuration*/ )
    {
    	iState = aError ? ENotReady : EReadyToPlay;
    	CActiveScheduler::Stop();
    
    }
    
    void CPlayer::MapcPlayComplete( TInt aError )
    {
    	iState = aError ? ENotReady : EReadyToPlay;
    	CActiveScheduler::Stop();
    }
    
    
    
    
    //  Local Functions
    
    LOCAL_C void MainL(const TDesC& aArgs)
    {
        //
        // add your program code here, example code below
        //
    	_LIT(KFileName, "check.wav");
    
        CPlayer *player = CPlayer::NewL(KFileName);
        CleanupStack::PushL(player);
        player->Play();
        CleanupStack::PopAndDestroy();
    }
    
    
    LOCAL_C void DoStartL()
    {
        // Create active scheduler (to run active objects)
        CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
        CleanupStack::PushL(scheduler);
        CActiveScheduler::Install(scheduler);
    
        // Call main function with command line
        TBuf<256> cmdLine;
        RProcess().CommandLine(cmdLine);
        MainL(cmdLine);
    
        // Delete active scheduler
        CleanupStack::PopAndDestroy(scheduler);
    }
    
    
    //  Global Functions
    
    GLDEF_C TInt E32Main()
    {
        // Create cleanup stack
        __UHEAP_MARK;
        CTrapCleanup* cleanup = CTrapCleanup::New();
    
        // Create output console
        TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
        if (createError)
            return createError;
    
        // Run application code inside TRAP harness, wait keypress when terminated
        TRAPD(mainError, DoStartL());
        if (mainError)
            console->Printf(KTextFailed, mainError);
        console->Printf(KTextPressAnyKey);
        console->Getch();
        
        delete console;
        delete cleanup;
        __UHEAP_MARKEND;
        return KErrNone;
    }
    
    // End of file

    HTH,
    deCoder

  5. #5
    Registered User
    Join Date
    Mar 2007
    Location
    INDIA
    Posts
    43

    Re: Wave Player using CMdaAudioPlayerUtility

    Hi,

    Check this link:
    http://wiki.forum.nokia.com/index.ph...ng_audio_files

    Best Regards,
    Kin

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Re: Wave Player using CMdaAudioPlayerUtility

    Hi Kin,
    I think if the requirement is to develop a console application for playing audio (using CMdaAudioPlayerUtility) then the code in the link wouldn't work. That was the problem in first place. If you look carefully at the code in the first post of this thread you would barely find any significant change in that code and the code in link (PlayUtility.h, PlayUtility.cpp) and these will be enough if you are having a GUI using these functions as core-engine functions but these code will fall short if you are just intializing and calling play in a console application, for reasons listed in my subsequent post.

    And a possible solution and the corresponding modified code is presented in my last post [There can be other ways to tackle this problem though]

    jaganpes, please note that if you are having a GUI you need not do the modifications suggested [because of the GUI event-loop].

    Regards,
    Rakesh

  7. #7
    Regular Contributor
    Join Date
    May 2008
    Posts
    149

    Unhappy Re: Wave Player using CMdaAudioPlayerUtility

    Hello, friend.

    I used your code to play WAV on S60 but I can't hear anything.
    I use the same emulator as you.
    Can u help me ?

    Thanx,

    Raffael
    Juiz de Fora - Brazil

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Re: Wave Player using CMdaAudioPlayerUtility

    hello friends,
    i tried this code in UIQ emulator , and its working fine. thank's to mr deCoder

  9. #9
    Regular Contributor
    Join Date
    May 2008
    Posts
    149

    Re: Wave Player using CMdaAudioPlayerUtility

    I've already solved the problem. I was passing a wrong filename to wave player utility. Thank you.

Similar Threads

  1. Create 3GP player with J2ME for Mobile
    By dexxa05 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 57
    Last Post: 2012-01-10, 11:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×