Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want my application to play .mid, .wav, .mp3 files, please could you help point me in the right direction. I have already tried SoundPlayer(System.Media) but this only seems to play wav files, and I really need to be able to play all the types listed.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Mar-15 19:04pm    
Why? Do you have any new ideas? Are you familiar with several best-known popular players? Do you have an idea for something better? If you do, why didn't you share it with us. If you don't want to share it, I don't know why would you need help. If not, I don't know why would you want to write one. Just to practice? But practice on what, if you, as I can understand, want to use available components. To say people "this is my own player"? Clever people won't be impressed by just that...
—SA
jennyneul 10-Mar-15 6:14am    
I'm not here to create any new ideas or to impress anyone, I just wanted a way to play sound files for my project.

This isn't a question suitable for this forum, please see the FAQ[^].

Apart from that, as a programmer, you should know about Google - these are the first two results I got by googling "c# mp3 player":

http://stackoverflow.com/questions/15025626/playing-a-mp3-file-in-a-winform-application[^]

https://www.caveofprogramming.com/c-sharp-tutorial/c-for-beginners-make-your-own-mp3-player-free.html[^]
 
Share this answer
 
Comments
jennyneul 9-Mar-15 18:27pm    
Yes I know about google, currently looking for options myself, as well as asking here. Thanks for the repy
[no name] 9-Mar-15 18:30pm    
Alright. If you hit a specific problem you're welcome to ask about that here. Happy coding!
I found a way to do it, first I had to upgrade to visual studio 2012, so I could use Net framework 4.5, then I used MediaPlayer from the namespace System.Windows.Media to play my sound files, look specifically at m_BGSM_Play_Click for the code that plays the sound files.
C#
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.Windows.Media;

namespace RPGInventor
{
    public partial class MapProperties : Form
    {
        CMap m_Map;
        string m_InitialMapName;
        bool m_bEdit;
        int m_nLayer;
        MediaPlayer m_SoundPlayer;

        public MapProperties()
        {
            InitializeComponent();
            m_bEdit = false;

            m_mapType.Items.Add("Normal");
            m_mapType.Items.Add("World Map");

            m_mapLayer.Items.Add("Layer 1");
            m_mapLayer.Items.Add("Layer 2");
            m_mapLayer.Items.Add("Layer 3");

            m_mapTileType.Items.Add("Normal");
            m_mapTileType.Items.Add("Floor");
            m_mapTileType.Items.Add("Wall");

            m_BGSM_SD.Items.Add("BGM");
            m_BGSM_SD.Items.Add("BGS");
            m_BGSM_SD.Items.Add("ME");
            m_BGSM_SD.Items.Add("SE");

            m_BI_SD.Items.Add("BGM");
            m_BI_SD.Items.Add("BGS");
            m_BI_SD.Items.Add("ME");
            m_BI_SD.Items.Add("SE");

            m_SoundPlayer = new MediaPlayer();
            //m_SoundPlayer.MediaEnded += m_SoundPlayer_MediaEnded;
        }

        //void m_SoundPlayer_MediaEnded(object sender, EventArgs e)
        //{
            //m_SoundPlayer.Position = new TimeSpan(0, 0, 0);
            //m_SoundPlayer.Play();
        //}

        public MapProperties(CMap map)
        {
            InitializeComponent();
            m_Map = map;
            m_bEdit = false;

            m_mapType.Items.Add("Normal");
            m_mapType.Items.Add("World Map");

            m_mapLayer.Items.Add("Layer 1");
            m_mapLayer.Items.Add("Layer 2");
            m_mapLayer.Items.Add("Layer 3");

            m_mapTileType.Items.Add("Normal");
            m_mapTileType.Items.Add("Floor");
            m_mapTileType.Items.Add("Wall");

            m_BGSM_SD.Items.Add("BGM");
            m_BGSM_SD.Items.Add("BGS");
            m_BGSM_SD.Items.Add("ME");
            m_BGSM_SD.Items.Add("SE");

            m_BI_SD.Items.Add("BGM");
            m_BI_SD.Items.Add("BGS");
            m_BI_SD.Items.Add("ME");
            m_BI_SD.Items.Add("SE");

            setMap(m_Map);

            m_SoundPlayer = new MediaPlayer();
            //m_SoundPlayer.MediaEnded += m_SoundPlayer_MediaEnded;
        }

        public void setMap(CMap map)
        {
            m_mpMapHolder1.setMap(map);
            m_bEdit = true;
            m_InitialMapName = map.getMapName();
            m_mapName.Text = map.getMapName();

            if (map.getMapType() == (int)CMap.MapType.NORMAL)
                m_mapType.SelectedIndex = 0;
            else
                m_mapType.SelectedIndex = 1;

            m_mapWidth.Value = map.getGridSize().X;
            m_mapHeight.Value = map.getGridSize().Y;

            m_mapLayer.SelectedIndex = 0;
            m_nLayer = 0;

            m_bEdit = false;
        }

        public CMap getMap()
        {
            return m_mpMapHolder1.getMap();
        }

        private void m_mapNameUpdate_Click(object sender, EventArgs e)
        {
            MainForm mf = (MainForm)this.Owner;
            mf.m_mapsListHolder1.changeMapName(m_InitialMapName, m_mapName.Text);
            m_Map = getMap();
            m_Map.delete(mf.m_Database);
            m_Map.setMapName(m_mapName.Text);
            m_Map.save(mf.m_Database);
            m_InitialMapName = m_mapName.Text;
        }

        private void MapProperties_Shown(object sender, EventArgs e)
        {
            //setMap(m_Map);
        }

        private void m_mapType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!m_bEdit)
            {
                m_Map = getMap();
                if (m_mapType.SelectedIndex == 0)
                {
                    m_Map.setMapType((int)CMap.MapType.NORMAL);
                }
                else
                {
                    m_Map.setMapType((int)CMap.MapType.WORLD_MAP);
                }
            }
        }

        private void m_mapSizeUpdate_Click(object sender, EventArgs e)
        {
            m_mpMapHolder1.setupGrid((int)m_mapWidth.Value, (int)m_mapHeight.Value);
            MainForm mf = (MainForm)this.Owner;
            mf.m_mapSetHolder1.setMap(getMap());
            m_Map = getMap();
            m_Map.save(mf.m_Database);
        }

        private void m_mapLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!m_bEdit)
            {
                m_nLayer = m_mapLayer.SelectedIndex;
                m_mpMapHolder1.changeLayer(m_nLayer + 1);
            }
        }

        public void UpdateTiles(CMapObject mapObj)
        {
            if (mapObj != null)
            {
                m_mapZOrder.Value = mapObj.getZOrder();
                if (mapObj.getType() == (int)CMapObject.MapObjectType.NORMAL)
                {
                    m_mapTileType.SelectedIndex = 0;
                }
                else if (mapObj.getType() == (int)CMapObject.MapObjectType.FLOOR)
                {
                    m_mapTileType.SelectedIndex = 1;
                }
                else if (mapObj.getType() == (int)CMapObject.MapObjectType.WALL)
                {
                    m_mapTileType.SelectedIndex = 2;
                }
            }
        }

        private void MapProperties_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_SoundPlayer.Stop();
            m_SoundPlayer.Close();

            m_Map = getMap();
            MainForm mf = (MainForm)this.Owner;
            m_Map.save(mf.m_Database);
        }

        private void m_BGSM_SD_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_BGSM_Sound.Items.Clear();
            m_BGSM_Sound.SelectedIndex = -1;
            m_BGSM_Sound.Text = "";

            string szItem = (string)m_BGSM_SD.SelectedItem;
            MainForm mf = (MainForm)this.Owner;
            if (szItem.Equals("BGM"))
            {
                for (int i = 0; i < mf.m_Database.m_BGMSounds.Length; ++i)
                {
                    string strSound = mf.m_Database.m_BGMSounds[i];
                    int nEnd = strSound.LastIndexOf("\\");
                    nEnd++;
                    m_BGSM_Sound.Items.Add(strSound.Substring(nEnd));
                }
            }
            if (szItem.Equals("BGS"))
            {
                for (int i = 0; i < mf.m_Database.m_BGSSounds.Length; ++i)
                {
                    string strSound = mf.m_Database.m_BGSSounds[i];
                    int nEnd = strSound.LastIndexOf("\\");
                    nEnd++;
                    m_BGSM_Sound.Items.Add(strSound.Substring(nEnd));
                }
            }
            if (szItem.Equals("ME"))
            {
                for (int i = 0; i < mf.m_Database.m_MESounds.Length; ++i)
                {
                    string strSound = mf.m_Database.m_MESounds[i];
                    int nEnd = strSound.LastIndexOf("\\");
                    nEnd++;
                    m_BGSM_Sound.Items.Add(strSound.Substring(nEnd));
                }
            }
            if (szItem.Equals("SE"))
            {
                for (int i = 0; i < mf.m_Database.m_SESounds.Length; ++i)
                {
                    string strSound = mf.m_Database.m_SESounds[i];
                    int nEnd = strSound.LastIndexOf("\\");
                    nEnd++;
                    m_BGSM_Sound.Items.Add(strSound.Substring(nEnd));
                }
            }
        }

        private void m_BGSM_Play_Click(object sender, EventArgs e)
        {
            string szSoundDir = (string)this.m_BGSM_SD.SelectedItem;
            string szSoundName = (string)this.m_BGSM_Sound.SelectedItem;
            MainForm mf = (MainForm)this.Owner;

            if (szSoundDir != null && szSoundName != null)
            {
                String szPath = "./Projects/" + mf.m_Database.m_Name + "/Audio/" + szSoundDir + "/" + szSoundName;

                m_SoundPlayer.Open(new Uri(szPath, UriKind.Relative));
                m_SoundPlayer.Play();
                
            }
        }
    }
}


I got my ideas from http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/PlaywithMediaPlayer.htm[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900