简体   繁体   中英

Using Window Media Player In Visual Studio

I am trying to make a Music Player in Visual Studio as a school project. I am facing some issues. I tried to seek help from Google but couldn't. Here is code:

#pragma once

namespace Musicplayer {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace AxWMPLib;
    using namespace WMPLib;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }
        String ^ files;
        String ^ paths;
        //array<String^>^ files ;
        //array<String^>^ paths ;
    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: AxWMPLib::AxWindowsMediaPlayer^  player;
    private: System::Windows::Forms::Button^  btnOpen;
    private: System::Windows::Forms::Button^  btnnext;
    private: System::Windows::Forms::ListBox^  listBox1;
    private: System::Windows::Forms::Button^  btnprev;
    private: System::Windows::Forms::Button^  btnStop;
    private: System::Windows::Forms::OpenFileDialog^  open;
    protected: 

    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
            this->player = (gcnew AxWMPLib::AxWindowsMediaPlayer());
            this->btnOpen = (gcnew System::Windows::Forms::Button());
            this->btnnext = (gcnew System::Windows::Forms::Button());
            this->listBox1 = (gcnew System::Windows::Forms::ListBox());
            this->btnprev = (gcnew System::Windows::Forms::Button());
            this->btnStop = (gcnew System::Windows::Forms::Button());
            this->open = (gcnew System::Windows::Forms::OpenFileDialog());
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->player))->BeginInit();
            this->SuspendLayout();
            // 
            // player
            // 
            this->player->Enabled = true;
            this->player->Location = System::Drawing::Point(48, 41);
            this->player->Name = L"player";
            this->player->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^  >(resources->GetObject(L"player.OcxState")));
            this->player->Size = System::Drawing::Size(163, 134);
            this->player->TabIndex = 0;
            this->player->Visible = false;
            this->player->PlayStateChange += gcnew AxWMPLib::_WMPOCXEvents_PlayStateChangeEventHandler(this, &Form1::player_PlayStateChange);
            // 
            // btnOpen
            // 
            this->btnOpen->Location = System::Drawing::Point(12, 213);
            this->btnOpen->Name = L"btnOpen";
            this->btnOpen->Size = System::Drawing::Size(75, 23);
            this->btnOpen->TabIndex = 1;
            this->btnOpen->Text = L"Open";
            this->btnOpen->UseVisualStyleBackColor = true;
            this->btnOpen->Click += gcnew System::EventHandler(this, &Form1::btnOpen_Click);
            // 
            // btnnext
            // 
            this->btnnext->Location = System::Drawing::Point(93, 213);
            this->btnnext->Name = L"btnnext";
            this->btnnext->Size = System::Drawing::Size(75, 23);
            this->btnnext->TabIndex = 2;
            this->btnnext->Text = L"Next";
            this->btnnext->UseVisualStyleBackColor = true;
            this->btnnext->Click += gcnew System::EventHandler(this, &Form1::btnnext_Click);
            // 
            // listBox1
            // 
            this->listBox1->FormattingEnabled = true;
            this->listBox1->Location = System::Drawing::Point(237, 41);
            this->listBox1->Name = L"listBox1";
            this->listBox1->Size = System::Drawing::Size(120, 134);
            this->listBox1->TabIndex = 3;
            this->listBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBox1_SelectedIndexChanged);
            // 
            // btnprev
            // 
            this->btnprev->Location = System::Drawing::Point(174, 213);
            this->btnprev->Name = L"btnprev";
            this->btnprev->Size = System::Drawing::Size(75, 23);
            this->btnprev->TabIndex = 4;
            this->btnprev->Text = L"Prev";
            this->btnprev->UseVisualStyleBackColor = true;
            this->btnprev->Click += gcnew System::EventHandler(this, &Form1::btnprev_Click);
            // 
            // btnStop
            // 
            this->btnStop->Location = System::Drawing::Point(255, 213);
            this->btnStop->Name = L"btnStop";
            this->btnStop->Size = System::Drawing::Size(75, 23);
            this->btnStop->TabIndex = 4;
            this->btnStop->Text = L"Stop";
            this->btnStop->UseVisualStyleBackColor = true;
            // 
            // open
            // 
            this->open->FileName = L"open";
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(369, 261);
            this->Controls->Add(this->btnStop);
            this->Controls->Add(this->btnprev);
            this->Controls->Add(this->listBox1);
            this->Controls->Add(this->btnnext);
            this->Controls->Add(this->btnOpen);
            this->Controls->Add(this->player);
            this->Name = L"Form1";
            this->Text = L"Form1";
            (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->player))->EndInit();
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void btnOpen_Click(System::Object^  sender, System::EventArgs^  e) {
                // array<String^>^ files = gcnew array<String^>(1000);
                // array<String^>^ paths = gcnew array<String^>(1000);
                 OpenFileDialog ^ open = gcnew OpenFileDialog();
                 open->ShowDialog();
                 open->Multiselect=true;
                 files=open->SafeFileName;
                 paths=open->FileName;
                 for(int i=0; i < files->Length ; i++)
                 {
                     listBox1->Items->Add(files[i]);
                 }

             }
private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
             player->URL=paths[listBox1->SelectedIndex];
             player->Ctlcontrols->play();
         }
private: System::Void btnnext_Click(System::Object^  sender, System::EventArgs^  e) {
             player->Ctlcontrols->next();

         }
private: System::Void btnprev_Click(System::Object^  sender, System::EventArgs^  e) {
             player->Ctlcontrols->previous();
         }
private: System::Void player_PlayStateChange(System::Object^  sender, AxWMPLib::_WMPOCXEvents_PlayStateChangeEvent^  e) {

         }
};
}

Now when I try to run the above code, it shows the following error:

error C2664: 'AxWMPLib::AxWindowsMediaPlayer::URL::set' : cannot convert parameter 1 from 'wchar_t' to 'System::String ^'

How can I resolve this?
Thank You

Basically your problem is convertion wchar_t to system::string in this fragment of code

private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
             player->URL=paths[listBox1->SelectedIndex];
             player->Ctlcontrols->play();
         }

So what you need to do is to convert paths[listBox1->SelectedIndex] to System::String for that see answer in this post

How do i convert const wchar_t* to System::String?

Also you can take a look at documentation:

https://msdn.microsoft.com/pl-pl/eng-eng/library/ms235219.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM