Playing Audio Folder (.NET)

Post December 18th, 2005, 11:20 pm

Hi,

I've got the following code to play individual audio files:

Code: [ Download ] [ Select ]
OpenFileDialog1.FileName = ""
AxMediaPlayer1.FileName = ""
OpenFileDialog1.ShowDialog()
If Not OpenFileDialog1.FileName = "" Then
cmdLoad.Enabled = False
AxMediaPlayer1.AutoStart = False
AxMediaPlayer1.FileName = OpenFileDialog1.FileName
MessageBox.Show("The following file has been loaded in the Media Player control: " + AxMediaPlayer1.FileName)
cmdPlay.Enabled = True
Else
cmdPlay.Enabled = False
End If
  1. OpenFileDialog1.FileName = ""
  2. AxMediaPlayer1.FileName = ""
  3. OpenFileDialog1.ShowDialog()
  4. If Not OpenFileDialog1.FileName = "" Then
  5. cmdLoad.Enabled = False
  6. AxMediaPlayer1.AutoStart = False
  7. AxMediaPlayer1.FileName = OpenFileDialog1.FileName
  8. MessageBox.Show("The following file has been loaded in the Media Player control: " + AxMediaPlayer1.FileName)
  9. cmdPlay.Enabled = True
  10. Else
  11. cmdPlay.Enabled = False
  12. End If

That's only the loading procedure; however, what I want to load a whole folder to play? How would I go about doing that instead of playing an individual file?

Regards,
Phil
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 18th, 2005, 11:20 pm

  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post December 19th, 2005, 2:08 am

What you might want to do is look for something like a play list and add the files to the play list.

Then enable the play list to true.

http://msdn.microsoft.com/library/defau ... ionadd.asp

I think this is what you are looking for
My Software Development Company
Music I have recorded (fixed now :))
Image

Post December 19th, 2005, 2:28 am

Rabid Dog wrote:
What you might want to do is look for something like a play list and add the files to the play list.

Then enable the play list to true.

http://msdn.microsoft.com/library/defau ... ionadd.asp

I think this is what you are looking for

Hmm, thanks. That makes sense; loading it into a playlist. How would I do that though? I understand how to load it into the playlist, but how do I play the playlist?
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post December 19th, 2005, 2:31 am

Unfotunately I don't have the library you are using on my machine. Point me to where I can get it and I will have a look for you :)
My Software Development Company
Music I have recorded (fixed now :))
Image

Post December 19th, 2005, 2:37 am

Rabid Dog wrote:
Unfotunately I don't have the library you are using on my machine. Point me to where I can get it and I will have a look for you :)

I found it here. Thanks for the help! :wink:
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post December 19th, 2005, 2:51 am

There is a newer version available

http://msdn.microsoft.com/windowsmedia/ ... fault.aspx

I would recommend downloading that one.

I will give it a look see in a bit. Something urgent has just come up
My Software Development Company
Music I have recorded (fixed now :))
Image

Post December 19th, 2005, 3:00 am

Okay, I'm downloading it right now. :)
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post December 20th, 2005, 8:05 am

OKay I need a few things before proceeding.

What is this intended to do? Are you using it as an OLE?

I did some tests but it fell over everytime because I had not instantiated an instance of the Media PLayer Application.

Give me a few more details and I'll see how I can help :)
My Software Development Company
Music I have recorded (fixed now :))
Image

Post December 20th, 2005, 8:17 am

Well, basically, what I'm trying to achieve is to allow the user to select a folder and then playing all of the music files in the folder. Hopefully, the files will also be able to loop.

Hence, I'm currently playing around with http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmplay10/mmp_sdk/mediacollectionadd.asp and http://support.microsoft.com/?kbid=821767.
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post December 20th, 2005, 11:51 pm

lol, I still ain't understanding the purpose of the application. Is it just a "I wanna see how to do this" application? Or does it fill a purpose?

If you right click on any directory in windows you are given the option to add it to the play list.

I'll see what I can come up with though. If you get stuck along the way gimme a shout :)
My Software Development Company
Music I have recorded (fixed now :))
Image

Post December 21st, 2005, 1:08 am

It's actually for a game I'm working on. so yeah, it's pretty much a "I wanna see how to do this" project. Within the game manager, I would like to give users the option to listen to some music while they play the game.

The problem I've got is that I can only get it to play one song at the moment. What I would like to do is allow them to click a "Browse..." type button and select a folder. The application would then play all of the audio files in that folder (looping through them).

That way, people can listen to their own music instead of the default game music - which may not appeal to everyone.
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post December 21st, 2005, 1:38 am

You said you have it playing one track at a time?


This is the code to read a directory (excuse the VB.NET it might be a very loose translation
Code: [ Download ] [ Select ]
//C#
using System.IO;
DirectoryInfo dir = new DirectoryInfo(path);
foreach(FileInfo file in dir.GetFiles()){
string mediaPath     = file.FullName;
if (mediaPath.EndsWith(".wma")){
 // add files to playlist / collection here
 }
}
//VB.NET is something like this i think
Imports System.IO
Dim dir As New DirectoryInfo(path);
foreach file As FileInfo in dir.GetFiles()
 Dim mediaPath As String
 mediaPath = file.FullName;
 if mediaPath.EndsWith(".wma")
  // add files to playlist / collection here
 end if
next
  1. //C#
  2. using System.IO;
  3. DirectoryInfo dir = new DirectoryInfo(path);
  4. foreach(FileInfo file in dir.GetFiles()){
  5. string mediaPath     = file.FullName;
  6. if (mediaPath.EndsWith(".wma")){
  7.  // add files to playlist / collection here
  8.  }
  9. }
  10. //VB.NET is something like this i think
  11. Imports System.IO
  12. Dim dir As New DirectoryInfo(path);
  13. foreach file As FileInfo in dir.GetFiles()
  14.  Dim mediaPath As String
  15.  mediaPath = file.FullName;
  16.  if mediaPath.EndsWith(".wma")
  17.   // add files to playlist / collection here
  18.  end if
  19. next
My Software Development Company
Music I have recorded (fixed now :))
Image

Post December 21st, 2005, 1:46 am

Okay, as soon as I load files into the playlist. How do I play it?
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post December 21st, 2005, 2:52 am

Woooohooooo!

Here is the C# version of the code, I am sure you will be able to translate it


If you copy and pasted into a C# project (windows application) it should work. I am going to let you re write the code yourself so you can see what is happening. If you got any questions let me know. The function you will wanna look at is LoadDir. Very simple. The LoadDir function takes a path to the folder as an argument. So use the value returned from the open folder dialog you want to present

The variable named mp is the media player

Another reason I want you to rewrite it is so you can take the credit for doing it in VB.NET :)

Let me know if you get stuck
Code: [ Download ] [ Select ]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using AxWMPLib;
using WMPLib;

namespace MediaPlayer
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
  private AxWindowsMediaPlayer mp;
  private System.Windows.Forms.RichTextBox rtbOutput;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            
   LoadDir("C:\\MUSIC\\Dave Mattews Band\\Stand Up");
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        
        private void LoadDir(String path){
         
         DirectoryInfo dir = new DirectoryInfo(path);
         IWMPPlaylist pl = mp.playlistCollection.newPlaylist("tempPlayList");
         int i      = 0;
         foreach(FileInfo file in dir.GetFiles()){
          string mediaPath     = file.FullName;
          if (mediaPath.EndsWith(".wma")){
           IWMPMedia media = mp.newMedia(mediaPath);
           pl.insertItem(i, media);
           i++;
          }
         }
         mp.currentPlaylist = pl;
         rtbOutput.AppendText("\n" + mp.currentPlaylist.name);
         mp.Ctlcontrols.play();
                 mp.Visible = false; // display the media player or not
         
         
         
        }

        #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>
        private void InitializeComponent()
        {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.mp = new AxWMPLib.AxWindowsMediaPlayer();
   this.rtbOutput = new System.Windows.Forms.RichTextBox();
   ((System.ComponentModel.ISupportInitialize)(this.mp)).BeginInit();
   this.SuspendLayout();
   //
   // mp
   //
   this.mp.Enabled = true;
   this.mp.Location = new System.Drawing.Point(8, 8);
   this.mp.Name = "mp";
   this.mp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mp.OcxState")));
   this.mp.Size = new System.Drawing.Size(160, 165);
   this.mp.TabIndex = 0;
   //
   // rtbOutput
   //
   this.rtbOutput.Location = new System.Drawing.Point(256, 8);
   this.rtbOutput.Name = "rtbOutput";
   this.rtbOutput.Size = new System.Drawing.Size(344, 336);
   this.rtbOutput.TabIndex = 1;
   this.rtbOutput.Text = "";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(680, 376);
   this.Controls.Add(this.rtbOutput);
   this.Controls.Add(this.mp);
   this.Name = "Form1";
   this.Text = "Form1";
   ((System.ComponentModel.ISupportInitialize)(this.mp)).EndInit();
   this.ResumeLayout(false);

  }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }
}
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. using AxWMPLib;
  9. using WMPLib;
  10. namespace MediaPlayer
  11. {
  12.     /// <summary>
  13.     /// Summary description for Form1.
  14.     /// </summary>
  15.     public class Form1 : System.Windows.Forms.Form
  16.     {
  17.   private AxWindowsMediaPlayer mp;
  18.   private System.Windows.Forms.RichTextBox rtbOutput;
  19.         /// <summary>
  20.         /// Required designer variable.
  21.         /// </summary>
  22.         private System.ComponentModel.Container components = null;
  23.         public Form1()
  24.         {
  25.             //
  26.             // Required for Windows Form Designer support
  27.             //
  28.             InitializeComponent();
  29.             
  30.    LoadDir("C:\\MUSIC\\Dave Mattews Band\\Stand Up");
  31.             //
  32.             // TODO: Add any constructor code after InitializeComponent call
  33.             //
  34.         }
  35.         /// <summary>
  36.         /// Clean up any resources being used.
  37.         /// </summary>
  38.         protected override void Dispose( bool disposing )
  39.         {
  40.             if( disposing )
  41.             {
  42.                 if (components != null)
  43.                 {
  44.                     components.Dispose();
  45.                 }
  46.             }
  47.             base.Dispose( disposing );
  48.         }
  49.         
  50.         private void LoadDir(String path){
  51.          
  52.          DirectoryInfo dir = new DirectoryInfo(path);
  53.          IWMPPlaylist pl = mp.playlistCollection.newPlaylist("tempPlayList");
  54.          int i      = 0;
  55.          foreach(FileInfo file in dir.GetFiles()){
  56.           string mediaPath     = file.FullName;
  57.           if (mediaPath.EndsWith(".wma")){
  58.            IWMPMedia media = mp.newMedia(mediaPath);
  59.            pl.insertItem(i, media);
  60.            i++;
  61.           }
  62.          }
  63.          mp.currentPlaylist = pl;
  64.          rtbOutput.AppendText("\n" + mp.currentPlaylist.name);
  65.          mp.Ctlcontrols.play();
  66.                  mp.Visible = false; // display the media player or not
  67.          
  68.          
  69.          
  70.         }
  71.         #region Windows Form Designer generated code
  72.         /// <summary>
  73.         /// Required method for Designer support - do not modify
  74.         /// the contents of this method with the code editor.
  75.         /// </summary>
  76.         private void InitializeComponent()
  77.         {
  78.    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  79.    this.mp = new AxWMPLib.AxWindowsMediaPlayer();
  80.    this.rtbOutput = new System.Windows.Forms.RichTextBox();
  81.    ((System.ComponentModel.ISupportInitialize)(this.mp)).BeginInit();
  82.    this.SuspendLayout();
  83.    //
  84.    // mp
  85.    //
  86.    this.mp.Enabled = true;
  87.    this.mp.Location = new System.Drawing.Point(8, 8);
  88.    this.mp.Name = "mp";
  89.    this.mp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mp.OcxState")));
  90.    this.mp.Size = new System.Drawing.Size(160, 165);
  91.    this.mp.TabIndex = 0;
  92.    //
  93.    // rtbOutput
  94.    //
  95.    this.rtbOutput.Location = new System.Drawing.Point(256, 8);
  96.    this.rtbOutput.Name = "rtbOutput";
  97.    this.rtbOutput.Size = new System.Drawing.Size(344, 336);
  98.    this.rtbOutput.TabIndex = 1;
  99.    this.rtbOutput.Text = "";
  100.    //
  101.    // Form1
  102.    //
  103.    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  104.    this.ClientSize = new System.Drawing.Size(680, 376);
  105.    this.Controls.Add(this.rtbOutput);
  106.    this.Controls.Add(this.mp);
  107.    this.Name = "Form1";
  108.    this.Text = "Form1";
  109.    ((System.ComponentModel.ISupportInitialize)(this.mp)).EndInit();
  110.    this.ResumeLayout(false);
  111.   }
  112.         #endregion
  113.         /// <summary>
  114.         /// The main entry point for the application.
  115.         /// </summary>
  116.         [STAThread]
  117.         static void Main()
  118.         {
  119.             Application.Run(new Form1());
  120.         }
  121.     }
  122. }
My Software Development Company
Music I have recorded (fixed now :))
Image

Post December 21st, 2005, 3:39 am

Wow. Thank you so much for that! It's a good thing too, I'll be able to learn some C# and understand exactly how the procedure works in Visual Basic. I'll get back to you as soon as I get it working! Again, thank you for your help - Ozzu can be proud of being blessed with such a valuable member. :D
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post December 21st, 2005, 3:39 am

Post Information

  • Total Posts in this topic: 30 posts
  • Users browsing this forum: No registered users and 350 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.