⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soundmixercontainer.cpp

📁 手机操作系统Symbian支持混音功能的声音引擎代码
💻 CPP
字号:
/*
 * ============================================================================
 *  Name     : SoundMixerContainer.cpp
 *  Part of  : SoundMixer
 *  Created  : 03/30/2006 by Forum Nokia
 *  Description:
 *     This is the project specification file for SoundMixer.
 *  Version  : 2.0.0
 *  Copyright: Forum Nokia
 * ============================================================================
 */

#include "SoundMixerContainer.h"
#include <eikenv.h>
#include "CSndMixer.h"
#include "CWavLoader.h"

_LIT(KMusicFile, "music2.wav");
_LIT(KEffectFile, "effect.wav");
_LIT(KEffect2File, "effect2.wav");

void CSoundMixerContainer::ConstructL(const TRect& aRect)
    {

    // load sound samples
    CWavLoader* wavLoader = CWavLoader::NewLC();
  
    iMusicSample = wavLoader->LoadL(KMusicFile());
    iEffectSample = wavLoader->LoadL(KEffectFile());
    iEffectSample2 = wavLoader->LoadL(KEffect2File());
  
    CleanupStack::PopAndDestroy( wavLoader );
  
    // create sound mixer
    iSndMixer = CSndMixer::NewL();
  
    // make iMusicSample repeat itself
    iMusicSample.iRepEnd = iMusicSample.iLength;
  
    // and play music on channel 0 at 16KHz with volume 256 ( max )
    iSndMixer->Play( iMusicSample, 0, 16000, 256 );
  
      CreateWindowL();
  
    // create periodic timer to move blocks
      iTimer = CPeriodic::NewL( EPriorityNormal );
  
    // initial positions and speeds for blocks
    TInt i;
    for( i=0; i<8; i++ )
      {
      iSpeed[ i ] = TPoint( ( i & 3 ) -1, ( ( i+2 ) & 3 ) -1 );
      iPosition[ i ] = TPoint( 10 + i * 10, 100 );
      }

    SetRect( aRect );
    ActivateL();

    // make this class observe changes is foreground events
    // to stop blocks and sound when application is inactive
    CEikonEnv::Static()->AddForegroundObserverL( *this );
    }

CSoundMixerContainer::~CSoundMixerContainer()
    {
    if(iTimer)
      {
      iTimer->Cancel();
      }
    delete iTimer;
    delete iSndMixer;
    delete iMusicSample.iData;
    delete iEffectSample.iData;
    delete iEffectSample2.iData;
    }

TKeyResponse CSoundMixerContainer::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
  {
  // here's the place for getting key events

  // set volume control to joystick's left / right
  if( aType == EEventKeyDown )
    {
    if( aKeyEvent.iScanCode == EStdKeyLeftArrow )
      {
      VolumeDown();
      }
    if( aKeyEvent.iScanCode == EStdKeyRightArrow )
      {
      VolumeUp();
      }
    }
  return EKeyWasConsumed;
  }


TInt CSoundMixerContainer::TimerCallBack( TAny* aPtr )
  {
  return ((CSoundMixerContainer*)aPtr)->DoTimer();
  }

TInt CSoundMixerContainer::DoTimer()
  {
  // here's everything moving and sounding
  
  // get gc to draw with
  CWindowGc& gc = SystemGc();
  
  gc.Activate( Window() );
  gc.Clear();
  gc.SetPenStyle(CGraphicsContext::ENullPen);
  gc.SetBrushColor( TRgb( 255,0,0 ) );
  gc.SetBrushStyle( CGraphicsContext::ESolidBrush );

  for( TInt i=0; i<8; i++ )
    {
    TBool edgeCollide = EFalse; // flag for boxes colliding against walls
    TBool boxCollide = EFalse;  // flag for boxes colliding each other

    TPoint& pos = iPosition[ i ];
    TPoint& spd = iSpeed[ i ];

    // move blocks
    pos += spd;

    TRect rect = Rect();

    // check box collide against walls
    if( ( pos.iX+8 >= rect.iBr.iX ) || ( pos.iX <= rect.iTl.iX ) )
      {
      spd.iX = -spd.iX;
      pos += spd;
      edgeCollide = ETrue;
      }

    if( ( pos.iY+8 >= rect.iBr.iY ) || ( pos.iY <= rect.iTl.iY ) )
      {
      spd.iY = -spd.iY;
      pos += spd;
      edgeCollide = ETrue;
      }


    // check box collide against each other
    TRect boxRect( pos, TSize( 8,8 ) );
    for( TInt j=0; j<8; j++ )
      {
      TRect boxRect2( iPosition[ j ], TSize( 8,8 ) );
      if( ( i != j ) && ( boxRect.Intersects( boxRect2 ) ) )
        {
        spd.iY = -spd.iY;
        spd.iX = -spd.iX;
        boxCollide = ETrue;
        }
      }

    // play sounds if collide
    if( edgeCollide )
      {
      iSndMixer->Play( iEffectSample, 1 + i, 8000 + i * 1000, 32 );
      }

    if( boxCollide )
      {
      iSndMixer->Play( iEffectSample2, 1 + i, 8000 + i * 1000, 32 );
      }

    // draw current box
    gc.DrawRect( boxRect );
    }

  gc.Deactivate();
  return ETrue;
  }


void CSoundMixerContainer::HandleGainingForeground()
  {
  // application gets focused
  // this function is called first time when application starts
  // that's why there's no need to start timer and sound in constructor
  iTimer->Cancel();
  iTimer->Start( 10000, 10000, TCallBack( TimerCallBack, this ) );
  iSndMixer->Resume();
  }


void CSoundMixerContainer::HandleLosingForeground()
  {
  // application loses focus
  // so stop moving blocks and playing sound
  iTimer->Cancel();
  iSndMixer->Pause();
  }


void CSoundMixerContainer::StartMixer()
  {
  iSndMixer->Resume();
  }

void CSoundMixerContainer::StopMixer()
  {
  iSndMixer->Pause();
  }

void CSoundMixerContainer::VolumeUp()
  {
  TInt volume = iSndMixer->Volume();
  volume += 20;
  if( volume > 255 ) volume = 255;
  iSndMixer->SetVolume( volume );
  }

void CSoundMixerContainer::VolumeDown()
  {
  TInt volume = iSndMixer->Volume();
  volume -= 20;
  if( volume < 0 ) volume = 0;
  iSndMixer->SetVolume( volume );
  }
  
// End of File

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -