C# WindowsForm メディアプレイヤー Form1.cs2022年08月25日 11:53


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myWMPlayer
{
    public partial class Form1 : Form
    {
        WMPLib.IWMPPlaylist myplaylist;
        WMPLib.IWMPMedia songs;
        string strFileName;

        public Form1(string[] args)
        {
            InitializeComponent();
            myplaylist = axWindowsMediaPlayer1.playlistCollection.newPlaylist("myplaylist");// プレイリストを作成            
            axWindowsMediaPlayer1.currentPlaylist = myplaylist;// プレイリストをメディアプレイヤーに設定
            axWindowsMediaPlayer1.settings.enableErrorDialogs = true;
            axWindowsMediaPlayer1.settings.setMode("shuffle", false);// シャッフル再生する場合は true。しない場合は false。
            if (args.Length > 0)
            {
                songs = axWindowsMediaPlayer1.newMedia(args[0]);
                myplaylist.appendItem(songs);// プレイリストにファイル追加
                plyToList();
            }
        }

        private void fileToPly()// ファイルをプレイリストにセット
        {
            foreach (string strFilePath in openFileDialog1.FileNames)//複数選択したファイル分だけ繰り返す
            {
                songs = axWindowsMediaPlayer1.newMedia(strFilePath);
                myplaylist.appendItem(songs);// プレイリストにファイル追加
            }
            plyToList();
        }

        private void plyToList()// リストボックスにプレイリスト内のファイル名を表示
        {
            listBox1.Items.Clear();
            for (int i = 0; i <= myplaylist.count - 1; i++)
            {
                listBox1.Items.Add(System.IO.Path.GetFileName(myplaylist.Item[i].sourceURL));
            }
            if (myplaylist.count >= 2)
            {
                btnSHazusu.Enabled = true;
                btnEHazusu.Enabled = true;
            }
            else
            {
                btnSHazusu.Enabled = false;
                btnEHazusu.Enabled = false;
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {                
                //MessageBox.Show(openFileDialog1.FileName,"Infomation",MessageBoxButtons.OK,MessageBoxIcon.Information);
                //axWindowsMediaPlayer1.URL = @"C:\myC\myWMPlayer\deta\制服_オールナイトニッポンより.mp4";
                //axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
                this.Text = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
                fileToPly();
                btnTuika.Enabled = true;
                timer1.Enabled = true;
            }            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = axWindowsMediaPlayer1.currentMedia.durationString;
            toolStripStatusLabel2.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPositionString;
            toolStripStatusLabel3.Text = System.IO.Path.GetFileName(axWindowsMediaPlayer1.currentMedia.sourceURL);
        }

        private void btnTuika_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileToPly();
            }
        }

        private void btnEHazusu_Click(object sender, EventArgs e)
        {
            myplaylist.removeItem(myplaylist.Item[myplaylist.count - 1]); //最後のアイテムを削除
            plyToList();
        }

        private void btnSHazusu_Click(object sender, EventArgs e)
        {
            myplaylist.removeItem(myplaylist.Item[0]); //先頭のアイテムを削除
            plyToList();
        }

        private void listBox1_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] drpfiles = (string[])e.Data.GetData(DataFormats.FileDrop);
                string drpfilePath;
                for (int i = 0; i < drpfiles.Length; i++)
                {
                    drpfilePath = drpfiles[i];
                    songs = axWindowsMediaPlayer1.newMedia(drpfilePath);
                    myplaylist.appendItem(songs);// プレイリストにファイル追加
                }
                plyToList();
            }
        }
    }
}



コメント

トラックバック

このエントリのトラックバックURL: http://tukasa.asablo.jp/blog/2022/08/25/9520618/tb

<< 2022/08
01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

このブログについて

ネットで見つけたいろいろ雑記

バックナンバー

RSS