mp3编程
㈠ c语言程序设计MP3或MP4文件基本信息的解析
找了个MP3看了下,comment是29个字节。
可参考 http://www.chineselinuxuniversity.net/articles/27374.shtml
拿VC的C语言写了下:
头文件:
#include <stdio.h>
// TODO: reference additional headers your program requires here
#define MP3_ID3_LENGTH 10
typedef struct tag_ID3_INFO
{
} ID3_INFO;
#define IDENTIFY_LEN 3
#define TITLE_LEN 30
#define ARTIST_LEN 30
#define ALBUM_LEN 30
#define YEAR_LEN 4
#define COMMENT_LEN 30
#define GENRE_LEN 1
// IDENTIFY_LEN + TITLE_LEN +...
#define MP3_INFO_LENGTH 128
typedef struct tag_MP3_INFO
{
char Identify[IDENTIFY_LEN + 1]; //TAG三个字母
//这里可以用来鉴别是不是文件信息内容
char Title[TITLE_LEN + 1]; //歌曲名,30个字节
char Artist[ARTIST_LEN + 1]; //歌手名,30个字节
char Album[ARTIST_LEN + 1]; //所属唱片,30个字节
char Year[YEAR_LEN + 1]; //年,4个字节
char Comment[COMMENT_LEN + 1]; //注释,28个字节
char Genre[GENRE_LEN + 1]; //类型 ,1个字节
} MP3_INFO;
C文件:
#include <string.h>
#define MP3_OK 1
#define MP3_ERROR 1
FILE *OpenMp3File(char *pFileName);
int ReadMP3Info(FILE *pFile, MP3_INFO *pstInfo);
int OutputMP3Info(MP3_INFO *pstMp3Info);
int main(int argc, char* argv[])
{
FILE *pFile = NULL;
MP3_INFO stMp3Info = {0};
char *pfname = "E:\\Project\\MP3\\test.MP3";
pFile = OpenMp3File(pfname);
if (NULL == pFile)
{
return MP3_ERROR;
}
ReadMP3Info(pFile, &stMp3Info);
printf("\r\nMP3 file: %s", pfname);
OutputMP3Info(&stMp3Info);
printf("Hello World!\n");
return MP3_OK;
}
FILE *OpenMp3File(char *pFileName)
{
FILE *pFile = NULL;
pFile = fopen(pFileName,"rb");
if (NULL==pFile)
{
printf("open read file error!!");
return NULL;
}
return pFile;
}
int ReadMP3Info(FILE *pFile, MP3_INFO *pstInfo)
{
int len = 0;
if ((NULL == pFile) || (NULL == pstInfo))
{
return MP3_ERROR;
}
fseek(pFile, 0, SEEK_END);
len = ftell(pFile);
if (len <= (MP3_INFO_LENGTH + MP3_ID3_LENGTH))
{
return MP3_ERROR;
}
memset(pstInfo, 0, sizeof(MP3_INFO));
fseek(pFile, -MP3_INFO_LENGTH, SEEK_END);
len = fread((char *)(pstInfo->Identify), 1, IDENTIFY_LEN, pFile);
len += fread((char *)(pstInfo->Title), 1, TITLE_LEN, pFile);
len += fread((char *)(pstInfo->Artist), 1, ARTIST_LEN, pFile);
len += fread((char *)(pstInfo->Album), 1, ALBUM_LEN, pFile);
len += fread((char *)(pstInfo->Year), 1, YEAR_LEN, pFile);
len += fread((char *)(pstInfo->Comment), 1, COMMENT_LEN, pFile);
len += fread((char *)(pstInfo->Genre), 1, GENRE_LEN, pFile);
if (MP3_INFO_LENGTH != len)
{
return MP3_ERROR;
}
return MP3_OK;
}
int OutputMP3Info(MP3_INFO *pstMp3Info)
{
printf("\r\nTag : %s", pstMp3Info->Identify);
printf("\r\nTitle : %s", pstMp3Info->Title);
printf("\r\nArtist : %s", pstMp3Info->Artist);
printf("\r\nAlbum : %s", pstMp3Info->Album);
printf("\r\nYear : %s", pstMp3Info->Year);
printf("\r\nComment: %s", pstMp3Info->Comment);
return MP3_OK;
}
测试:
MP3 file: E:\Project\MP3\test.MP3
Tag : TAG
Title : TrackTitle
Artist : ArtistName
Album : AlbumTitle
Year : 2012
Comment: This is a comment
㈡ python如何播放mp3
使用以下代码即可播放。
01 import mp3play
02
03 filename = r'C:\Documents and Settings\Michael\Desktop\music.mp3'
04 mp3 = mp3play.load(filename)
05
06 mp3.play()
07
08 # Let it play for up to 30 seconds, then stop it.
09 import time
10 time.sleep(min(30, mp3.seconds()))
11 mp3.stop()
㈢ 怎样用C语言编程打开一个文件(比如mp3,mp4)
可以使用PlaySound()函数播放mp3音频,该函数原型位于windows.h。
PlaySound函数的声明为:
BOOL PlaySound(LPCSTR pszSound, HMODULE hwnd,DWORD fdwSound);
参数pszSound是指定了要播放声音的字符串。
参数hwnd是应用程序的实例句柄,除非pszSound的指向一个资源标识符(即fdwSound被定义为SND_RESOURCE),否则必须设置为NULL。
参数fdwSound是标志的组合,如下表所示。若成功则函数返回TRUE,否则返回FALSE。
使用PlaySound函数时需要在#include<windows.h>后面加上(注意:不能加在前面):
例程:
CFileDialog dlg(TRUE, "mp3", "*.mp3", OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT, "mp3文件(*.mp3)|*.mp3|");
/*创建选择对话框,让用户从文件夹中选取一个MP3文件*/
int iRet = dlg.DoModal();//获得对话框返回值
if(IDOK == iRet) //如果返回值成功,表明成功获取一个MP3文件
{
CString pathName= dlg.GetPathName(); //得到文件的路径名称
PlaySound( pathName , NULL, SND_FILENAME | SND_ASYNC);//用playsound函数播放该文件
}