locked
XAudio component for use in C#/XAML

    Question

  • Before I go write one,

    Has anyone created a component to play sound effects via XAudio for use in XAML?

    Saturday, January 26, 2013 5:07 PM

Answers

All replies

  • Hi,

    You can follow this document to use XAudio2 in XAML.
    http://msdn.microsoft.com/en-us/magazine/jj883962.aspx

    Here is the document for XAudio2 Audio Effects
    http://msdn.microsoft.com/en-us/library/windows/desktop/ee415756(v=vs.85).aspx

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 28, 2013 6:51 AM
    Moderator
  • Sorry but this is c++ but shows how I did it.

    void Renderer::playmrdj()
    {
    
    
    		Windows::ApplicationModel::Package^ package = Windows::ApplicationModel::Package::Current;
    Windows::Storage::StorageFolder^ item = package->InstalledLocation;
    
    	auto getFileOp = item->GetFileAsync("mrdjwhereareyou.wav");
    	getFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>
        ([=](IAsyncOperation<StorageFile^>^ operation, AsyncStatus status)
      {
        auto storageFile = operation->GetResults();
    	auto openOp = storageFile->OpenAsync(FileAccessMode::Read);
        openOp->Completed = 
          ref new AsyncOperationCompletedHandler <IRandomAccessStream^>
          ([=](IAsyncOperation<IRandomAccessStream^>^ operation, AsyncStatus status)
        {
    
          auto istream = operation->GetResults();  
    	  auto dr = ref new DataReader(istream);
          auto loadOp = dr->LoadAsync(istream->Size);
          loadOp->Completed = ref new AsyncOperationCompletedHandler<UINT>
            ([=](IAsyncOperation<UINT>^ operation, AsyncStatus status)
          {
    		   auto bytesRead = operation->GetResults();
    			soundlength=bytesRead;
    			int cx,si;
    			byte al;
    			si=0;
    			for (cx=0;cx<bytesRead;cx++)
    			{
    			al=(dr->ReadByte());
    			soundData[si]=al;
    			si++;
    			}
    	
    			
    		  dr->DetachStream();	
    
    		  
    		
    	  		try{
    	operation->GetResults();		//Get the result of task by forcing it.
    		mrdjloaded=1;		 
    		  /////////////////////////////////////////////////////////////////////////////////
    		
      // Create an IXAudio2 object
      HRESULT hr = XAudio2Create(&pXAudio2);
      if (FAILED(hr))
      {
    //    errorText->Text = "XAudio2Create failure: " + hr.ToString();
        return;
      }
      // Create a mastering voice
      hr = pXAudio2->CreateMasteringVoice(&pMasteringVoice);
      if (FAILED(hr))
      {
    //    errorText->Text = "CreateMasteringVoice failure: " + hr.ToString();
        return;
      }
      // Create a source voice
      WAVEFORMATEX waveformat;
      waveformat.wFormatTag = WAVE_FORMAT_PCM;
      waveformat.nChannels = 1;
      waveformat.nSamplesPerSec = 88200;
      waveformat.nAvgBytesPerSec = 88200 * 2;
      waveformat.nBlockAlign = 2;
      waveformat.wBitsPerSample = 16;
      waveformat.cbSize = 0;
      hr = pXAudio2->CreateSourceVoice(&pSourceVoice, &waveformat);
      if (FAILED(hr))
      {
     //   errorText->Text = "CreateSourceVoice failure: " + hr.ToString();
        return;
      }
      // Start the source voice
      hr = pSourceVoice->Start();
      if (FAILED(hr))
      {
     //   errorText->Text = "Start failure: " + hr.ToString();
        return;
      }
    	
    
    	
    	 XAUDIO2_BUFFER buffer = { 0 };
      buffer.AudioBytes = 3000000;
      buffer.pAudioData = soundData;
      buffer.Flags = XAUDIO2_END_OF_STREAM;
      buffer.PlayBegin = 0;
      buffer.PlayLength = soundlength/3;
      // Submit the buffer
      hr = pSourceVoice->SubmitSourceBuffer(&buffer);
      if (FAILED(hr))
      {
    //    errorText->Text = "SubmitSourceBuffer failure: " + hr.ToString();
    //    submitButton->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        return;
      }
    
    
    
    
    		/////////////////////////////////////////////////////////////////////////////////////
    		  }
    		  catch(...)
    		  {
    			  mrdjloaded=2; //error
    			  return;
    		  }
    		  
    
    
    
    	  });
        });
      });
    
    
    }
    
    
    


    n.Wright

    Wednesday, October 09, 2013 11:33 PM