在Visual Studio中使用Window Media Player

时间:2018-01-20 12:05:06

标签: c# c++ visual-c++ visual-studio-2012 c++-cli

我正在尝试将Visual Studio中的音乐播放器作为学校项目。我正面临一些问题。我试图寻求谷歌的帮助,但不能。 这是代码:

#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) {

         }
};
}

现在,当我尝试运行上面的代码时,它显示以下错误:

  

错误C2664:'AxWMPLib :: AxWindowsMediaPlayer :: URL :: set':无法将参数1从'wchar_t'转换为'System :: String ^'

我该如何解决这个问题? 谢谢

1 个答案:

答案 0 :(得分:0)

基本上你的问题是在这段代码

中将wchar_t转换为system :: string
private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
             player->URL=paths[listBox1->SelectedIndex];
             player->Ctlcontrols->play();
         }

所以您需要做的是将路径[listBox1-&gt; SelectedIndex]转换为System :: String,以查看此帖子中的答案

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

您还可以查看文档:

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