Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
My project is to play 2 wave files (play x0.wav then play WaitOneSecond.wave then play x1.wav)
i added Windows Media Player .COM not the Windows Media Player from toolbox
WaitOneSecond.wave is an empty file it is just to insert a gap between x0.wav and x1.wav
my error is i could only play x0.wav
if i removed the if satement from the code below it only play x1.wav ,i could not here x0.wav
as far as i know ,the variable
PlayerFinished
shall be equal to 1 if wav file ended
so how can i play the 2 wave files ?
thanks

C#
using WMPLib;
namespace zenat
{
    public partial class Form1 : Form
    {
        int PlayerFinished = 0;
        WMPLib.WindowsMediaPlayer Player;
        public Form1()
        {
            InitializeComponent();
            //Sound
            Player = new WMPLib.WindowsMediaPlayer();
            Player.PlayStateChange +=
                new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
            Player.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
            Player.settings.volume = 100;
        }
        //
        private void Player_PlayStateChange(int NewState)
        {
            if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
            PlayerFinished = 1;
        }
        //
        private void Player_MediaError(object pMediaObject)
        {
            MessageBox.Show("Cannot play media file.");
        }
        //
        private void pronouncClick(object sender, EventArgs e)
        {   string sound;
            //PlayerFinished = 0;
            //Player.URL = "PalySilence4_OneSecond.wav";

            PlayerFinished = 0;
            sound = "x0.wav";
            if (File.Exists(sound)) Player.URL = sound;
            else Player.URL = "PalySilence4_OneSecond.wav";//

            if (PlayerFinished == 1)//start playing x1.wav if only the x0.wav ended 
            {
                //PlayerFinished = 0;
                //Player.URL = "PalySilence4_OneSecond.wav";

                PlayerFinished = 0;
                sound = "x1.wav";
                if (File.Exists(sound)) Player.URL = sound;
                else Player.URL = "PalySilence4_OneSecond.wav";
            }
        }

    }
}
Posted
Updated 23-Dec-14 7:56am
v3
Comments
[no name] 23-Dec-14 12:44pm    
Without that I followed all details : Do you have a thought that two songs simultaneously play need two players?
Engineer khalid 23-Dec-14 12:57pm    
i want to play song one when song one finished and using the same player i want to play song two ,untill now things seems to me right please show me my error
Engineer khalid 23-Dec-14 13:13pm    
look at this codes
x=10; Area=x*2; MessageBox.Show("Area="+Area.ToString());
x=100; Area=x*2; MessageBox.Show("Area="+Area.ToString());
so i am reusing same x vaiable ,so what is wrong with Player
do you mean i have to use player1 to play song1,then use palyer2 to play song2 ?
Engineer khalid 23-Dec-14 13:26pm    
most likley my mistake is in method Player_PlayStateChange(int NewState)

1 solution

The following has solved the error
int NwavFilePlayed = 0;//declared globalley

private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
{
NwavFilePlayed++;
if (NwavFilePlayed < 5)
{
//MessageBox.Show("NwavFilePlayed=", NwavFilePlayed.ToString());
Pronounce("hellow");
}
//else MessageBox.Show("no more file to play", "NwavFilePlayed=" + NwavFilePlayed.ToString());

}
}//Player_PlayStateChange

private void Pronounce(string str)//
{
string sound;

Player = new WMPLib.WindowsMediaPlayer();
Player.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
Player.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
Player.settings.volume = 100;

//MessageBox.Show(str, "NwavFilePlayed=" + NwavFilePlayed.ToString());

sound = "x"+NwavFilePlayed.ToString()+".wav";
if (File.Exists(sound)) Player.URL = sound;
else Player.URL = "PalySilence4_OneSecond.wav";//
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900