Coding C++ to play *.wma files

This is a discussion on Coding C++ to play *.wma files within the C++ Programming forums, part of the General Programming Boards category; OK, I just want to know if I'm going the right direction with this. I'm trying to write a routine ...

  1. #1
    Domestic Goddess
    Join Date
    Feb 2005
    Location
    USA - Pacific NW
    Posts
    1

    Coding C++ to play *.wma files

    OK, I just want to know if I'm going the right direction with this.

    I'm trying to write a routine which will give a list of songs and artists, tied to an ID number, so when an ID number is selected the song will play.

    I know that you can play music directly from a command prompt ("C:\path to player\wmplayer.exe" "Path For the Song File\filename.wma") and am thinking that I could do this in C++ using a "while", "if" and "else if" format.

    Has anyone played with something like this in the past? If so, what brick walls did you run into and how did you break through them?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You might get a neater solution by looking at some Microsoft APIs. Maybe DirectX has WMA support - search MSDN (use google, just use msdn as a search term) for an API call you can use. It's likely you'll have to use some API calls anyway - inorder to decode information that's embedded in the file (like artist, etc...), you'll either need to program the whole thing yourself (www.wotsit.org may have the file format if that's what you want to do), or use an MS library.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    If you want to play the file with Windows Media Player, you could try the ShellExecute function:
    Code:
    ShellExecute(NULL, NULL, TEXT("C:\\SomePath\\filename.wma"), NULL, NULL, SW_SHOWMINNOACTIVE);
    If you want to play a WMA file without opening WMP, you can use mci as shown by this webpage. Here is a simple sample:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #if defined(_MSC_VER)
    #pragma comment(lib, "Winmm.lib")
    #endif
    
    BOOL PlaySong(LPCTSTR szFile)
    {
    	TCHAR szCommandString[1000];
    
    	wsprintf(szCommandString, TEXT("open \"%s\" type mpegvideo alias MediaFile"), szFile);
    
    	/* By default mci functions will return immediately and the task will be carried out
    	 * asynchronously. To have the function wait, place the word "wait" at the end of the
    	 * command string. ie. "play MediaFile wait" */	
    	if (ERROR_SUCCESS == mciSendString(szCommandString, NULL, 0, NULL) &&
    	    ERROR_SUCCESS == mciSendString(TEXT("play MediaFile"), NULL, 0, NULL))
    	{
    		return TRUE;
    	}
    
    	return FALSE;
    }
    
    int main(void)
    {
    	if ( !PlaySong(TEXT("C:\\PathTo\\My Music\\MySong.wma")) )
    	{
    		printf("Failed to play song!");
    	}
    
    	getchar();
    	return 0;
    }
    The mci command language has many more options. For example, you can pause and resume, play a specific part of the song and get notification of when the song finishes playing. The mci reference is here.

    Another alternative is DirectShow. This is a DirectShow sample that plays a file.
    Last edited by anonytmouse; 04-07-2005 at 10:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21