allstar
allstar

Reputation: 109

openfiledialog file and directory?

I m trying to make simple mp3 player.when i use this

 listBox2.Items.Add(openFileDialog1.FileName);

to add songs to my listbox it is working but it shows file directory so i changed like this

listBox2.Items.Add(openFileDialog1.SafeFileName);

then it looks song name on listbox1 but when i click play button it is not working:(

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication12222
{
    public partial class Form1 : Form
    {
        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
        public string Pcommand;
        public bool isOpen;
        public Form1()
        {
            InitializeComponent();
        }

        public void Stop()
        {
            Pcommand = "close MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = false;
        }

        public void Start()
        {
            Pcommand = "open \"" + listBox1.SelectedItem + "\" type mpegvideo alias MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = true;
            Play(true);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Media File(*.mpg,*.dat,*.avi,*.wmv,*.wav,*.mp3)|*.wav;*.mp3;*.mpg;*.dat;*.avi;*.wmv";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName != ""){

               // listBox1.Items.Add(openFileDialog1.SafeFileName);

                listBox2.Items.Add(openFileDialog1.FileName);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Stop();
        }
        public void Play(bool loop)
        {
            if (isOpen)
            {
                Pcommand = "play MediaFile";
                if (loop)
                    Pcommand += " REPEAT";
                mciSendString(Pcommand, null, 0, IntPtr.Zero);
            }
        }
        int x;
        private void button2_Click(object sender, EventArgs e)
        {

            if (listBox1.SelectedIndex ==listBox1.Items.Count-1 ) { x++; }
            else
            {
                Stop();
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;

                Start();
            }
        }
        int y;
        private void button5_Click(object sender, EventArgs e)
        {

            if (listBox1.SelectedIndex ==0) { y++; }
            else
            {
                Stop();
                listBox1.SelectedIndex = listBox1.SelectedIndex - 1;

                Start();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = 0;

            Stop();
            Start();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
            Stop();
            Start();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            Pcommand = "close MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = false;
        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

    }
}

extra question is it possible to mix (random) a listbox .I add songs on mp3 player then when i want to click mix button i want list mixed.Is there any listbox command for this?

Upvotes: 0

Views: 979

Answers (2)

indolentdeveloper
indolentdeveloper

Reputation: 1313

You should be able to use the link below to shuffle your playlist.

Random playlist algorithm

Thanks, Naval

Upvotes: 1

hattenn
hattenn

Reputation: 4399

Create a custom class:

public class FileItem
{
    public string FilePath { get; set; }
    public string ShortName { get; set; }
}

And then create a new instance of this class when you get a file from the OpenFileDialog, save the openFileDialog1.FileName to FilePath property, and then get the short file name using the Windows.IO.Path.GetFileName() method.

Rather than adding the file path to the ListBox directly from the OpenFileDialog, add this instance of your class.

And change the DisplayMember property of your ListBox to "ShortName", this way the "short name" of your file path will be displayed in the ListBox.

Upvotes: 2

Related Questions