How to resolve object instance error in C++ Winform

José Carlos 886 Reputation points
2023-02-25T17:29:36.85+00:00

Guys

That application to play mp3 that I transferred to C++ Winforms, changed by mistake. Now it's giving the error object reference not set to an instance of an object saying that the WindowsMediaPlayer type variable is null. At the beginning of the program I put the line:

WindowsMediaPlayer^ musica;

When I order the music to play in the program section below, it gives the error.


			if (musica->playState == WMPPlayState::wmppsUndefined || musica->playState == WMPPlayState::wmppsStopped)
			{
				musica->URL = arquivoMP3;
				musica->controls->play(); // toca a m[usica
				// TROCA A IMAGEM DE PLAY PARA PAUSA
				btnPlay->ImageIndex = 1;
				timer1->Enabled = true;

			}
			else
				if (musica->playState == WMPPlayState::wmppsPlaying) // SE MUSICA ESTÁ TOCANDO
				{
					musica->controls->pause(); // SE CLICAR NO PLAY QDO A MUSICA TIVER TOCANDO, PAUSA A MUSICA
					btnPlay->ImageIndex = 0; // MUDA A IMAGEM PARA PLAY
				}
				else
					if (musica->playState == WMPPlayState::wmppsPaused) // SE A MÚSICA ESTÁ PAUSADA
					{
						musica->controls->play(); // SE CLICAR NA PAUSA A MUSICA COMEÇA A TOCAR
						btnPlay->ImageIndex = 1; // MUDA A IMAGEM DE PLAY PARA PAUSA
					}

		}

Here it already says that musica is null. How to create an instance of the music variable?
Tanks
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,912 questions
{count} votes

Accepted answer
  1. Castorix31 88,461 Reputation points
    2023-02-25T23:06:49.1133333+00:00

    This test with a .mp3 works for me (without GUI) :

    			WindowsMediaPlayer^ musica = gcnew WindowsMediaPlayerClass();
    			musica->URL = "E:
    			musica->settings->volume = 50;
    			musica->controls->play();
    
    

    or with GUI :

    			AxWMPLib::AxWindowsMediaPlayer^ musica = gcnew AxWMPLib::AxWindowsMediaPlayer();
    			musica->Size = System::Drawing::Size(400, 400);
    			musica->Location = System::Drawing::Point(10, 10);			
    			this->Controls->Add(musica);
    			musica->URL = "E:\\01. IMANY - Don't Be so Shy (Filatov & Karas Remix).mp3";
    			musica->Ctlcontrols->play();
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.