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函數播放該文件
}