10 Replies - 1578 Views - Last Post: 19 September 2012 - 03:25 PM Rate Topic: -----

#1 H2012  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 25
  • Joined: 19-September 12

Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 06:56 AM

Hello and good day everyone,


I'm currently using Microsoft Visual C# 2010. I figured I would get that out of the way so that the main issue can be addressed.

Now to the issue at hand.
My long-term goal is to make a portable (as in it can be installed on any computer) music player with a play, pause, stop, and volume function that has a list of songs that can be loaded into the main player with the controls.
As of this moment, I've only succeeded in having a song play with no control whatsoever.

Currently, I've tried making a form, which worked just fine if I wanted to just hand over the music I want the user to play and make them load everything. After a few days of searching, I was unable to apply what I saw in any meaningful way.
The video, at least the one I can find, that I initially tried was this one.

http://www.youtube.c...h?v=WUOnXSCTe_Q

I found another that had a more list-based window, but I can't seem to find it. Regardless, the principles are roughly the same.

I've also tried XACT, and could get it to work as far as playing was concerned, but the key that I used to trigger the event would cause multiple instances to play on top of each other, and I couldn't get it to pause or stop.
I also had the issue of where a single key press would create multiple instances at almost the same time resulting in a chorused effect.


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DayNightExperiment
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        KeyboardState currentKeyState;

        AudioEngine engine;
        SoundBank soundBank;
        WaveBank waveBank;

        Cue cue;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 1200;
            graphics.PreferredBackBufferHeight = 750;
        }

        protected override void Initialize()
        {
            engine = new AudioEngine("Content\\soundexperiment.xgs");
            soundBank = new SoundBank(engine, "Content\\Sound Bank.xsb");
            waveBank = new WaveBank(engine, "Content\\Wave Bank.xwb");


            base.Initialize();
        }

        protected override void Update(GameTime gameTime)
        {
            Cycle += 1 * (float)gameTime.ElapsedGameTime.TotalSeconds;

            currentKeyState = Keyboard.GetState();
            currentMouseState = Mouse.GetState();

            cue = soundBank.GetCue("Sunlight");

            if (IsKeyPressed(Keys.P, "PRESS"))
                cue.Play();

            engine.Update();

            base.Update(gameTime);
        }




A quick note: the "IsKeyPressed" business is part of a code my friend showed me to basically force the game to only register one key stroke, and out of respect for him, I will not post the finer details of that code here.
And speaking of games, I've been working in the format of a game to try and make this music player. I'm not sure if that's important, but I figured I would mention it anyways.

Concerning this method of going about things, I've searched online for a few days now in an attempt to remedy and figure out the issues I'm having, but so far, no such luck.
With this failure, I moved on and back to an old idea I had, which is to use the SoundEffect method to create this player.

So far, I've again only succeeded in getting a sound to play, and only just got it to stop looping infinitely. I also recently had some success with placing a SoundEffectInstance inside an if statement (which is something that I have been unable to do up to this point. Using cues, I could place those in an if statement, but again, ran into the issue of multiple instances playing on top of each other upon key presses.)
Right now, I currently have this using the SoundEffect method.


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DIM
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        SoundEffect S1;
        SoundEffect S2;

        SoundEffectInstance SEI_1;
        SoundEffectInstance SEI_2;

        KeyboardState currentKeyState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            S1 = Content.Load<SoundEffect>("Descent Into Madness");
            S2 = Content.Load<SoundEffect>("Sunlight");

            SEI_1 = S1.CreateInstance();
            SEI_2 = S2.CreateInstance();

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (SEI_1.State == SoundState.Stopped)
            {
                SEI_1.Play();
            }

            if (SEI_1.State == SoundState.Playing && currentKeyState.IsKeyDown(Keys.D1))
            {
                SEI_1.Pause();
            }

            base.Update(gameTime);
        }




All in all, I'm not partial to any method. I just want something that will work.
The more practical idea seems to be using the first method, a media player, which uses Windows Media Player, however I must admit I've only been coding for about two months now in C# and can't quite grasp all the concepts that are being thrown at me each time I tried to use that method.


In the end, after about two weeks of trying this on my own, I figured it would be a good idea to get some input on this conundrum. If anyone can provide any sort of idea on how to achieve this, or has a different method of doing things, I am very much open to suggestion, and again, being a beginner, if you can keep your explanations in simple terms, I would really appreciate it.


Thank you all for your time if you're reading this and especially thank you if you so choose to try and help me with this issue.


-H2012

Is This A Good Question/Topic? 0
  • +

Replies To: Visual C#: Failing At Creating A Music Player

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 09:41 AM

Would you restate what you are trying to do? I read this twice and have lost the train of what your specific questions were outside of "I am bad at making a music player".

Statements that make no sense and took the train off the tracks:

Quote

Currently, I've tried making a form, which worked just fine if I wanted to just hand over the music I want the user to play and make them load everything.


Quote

After a few days of searching, I was unable to apply what I saw in any meaningful way.


Quote

Concerning this method of going about things, I've searched online for a few days now in an attempt to remedy and figure out the issues I'm having, but so far, no such luck.

Issues were not defined here..
Was This Post Helpful? 0
  • +
  • -

#3 H2012  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 25
  • Joined: 19-September 12

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 11:08 AM

Hello Modi123_1,


Apologies for that, I'll see if I can restate things a bit.

Basically, I'm looking for methods and other ways in which I could create a portable music player, meaning it has a playlist that's running off of its own files, and has the basics, such as play, pause, stop, and volume control. An auto-play function would be nice, but isn't critical in the initial build.

If that still isn't clear enough, definitely be sure to let me know and again, my apologies.


-H2012
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 11:15 AM

Yeah.. okay that's what you want the project to do.. where is it failing at?
Was This Post Helpful? 0
  • +
  • -

#5 H2012  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 25
  • Joined: 19-September 12

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 11:46 AM

It's failing when I try to add any feature other than "play."


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DIM
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        SoundEffect S1;
        SoundEffect S2;

        SoundEffectInstance SEI_1;
        SoundEffectInstance SEI_2;

        KeyboardState currentKeyState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void LoadContent()
        {
            S1 = Content.Load<SoundEffect>("Descent Into Madness");
            S2 = Content.Load<SoundEffect>("Sunlight");

            SEI_1 = S1.CreateInstance();
            SEI_2 = S2.CreateInstance();

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void Update(GameTime gameTime)
        {
            if (SEI_1.State == SoundState.Stopped)
            {
                SEI_1.Play();
            }

            if (SEI_1.State == SoundState.Playing && currentKeyState.IsKeyDown(Keys.D1))
            {
                SEI_1.Pause();
            }

            base.Update(gameTime);
        }




This is the whole code, minus draw code and basically code I haven't done anything with.
The part in particular that I'm working with is this.


            if (SEI_1.State == SoundState.Stopped)
            {
                SEI_1.Play();
            }

            if (SEI_1.State == SoundState.Playing && currentKeyState.IsKeyDown(Keys.D1))
            {
                SEI_1.Pause();
            }




It will play just fine, however if I try to pause it, or for that matter, anything other than play, it doesn't do as it's supposed to do.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 11:49 AM

... and why are you using XNA for this?
Was This Post Helpful? 0
  • +
  • -

#7 H2012  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 25
  • Joined: 19-September 12

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 11:55 AM

Well, I don't know what else to use to make it.
I'm very much a beginner and all I've been taught so far is console programs and XNA.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 12:01 PM

That doesn't parse too well, but what evs.

If you want this to be a desktop app then make it a desktop app.

Start with the Windows Media Player SDK.
http://msdn.microsof...v=vs.85%29.aspx
http://msdn.microsof...v=vs.85%29.aspx
Was This Post Helpful? 0
  • +
  • -

#9 H2012  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 25
  • Joined: 19-September 12

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 12:07 PM

Mm, is there a template in Visual C# for making a desktop application? If so, I'm not seeing it.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 12:10 PM

Template?

File -> new project -> visual c# -> windows -> windows form application.
Was This Post Helpful? 0
  • +
  • -

#11 H2012  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 25
  • Joined: 19-September 12

Re: Visual C#: Failing At Creating A Music Player

Posted 19 September 2012 - 03:25 PM

Alright, I've been able to make, minus a few details, a playlist.
One thing eludes me, though: I can't quite figure out how to get it to play files off of imported music. I can only get it to play music through a long file location, I.E. C:\Users\Desktop\Music\Artist\CD\Song 1.MP3.
I've created a "resources" folder and used it to add an image successfully, but I can't seem to do the same with music files.

If it helps any, I'm using "axWindowsMediaPlayer1.URL" to tell it which song to play.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1