⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 id3tag.cpp

📁 一些关于C++开发的多媒体制作书籍的源代码
💻 CPP
字号:
/*
* Copyright (c) 2002, Bcdliang
* All rights reserved.
*
* 文件名称:ID3TAG.cpp
* 摘    要:类CID3TAG的实现
*
* 当前版本:1.01
* 作    者:LIANG Zheng
* 完成日期:2002年8月11日
*/

#include "stdafx.h"
#include "ID3TAG.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CID3TAG::CID3TAG()
:m_Title(_T("")),
m_Artist(_T("")),
m_Album(_T("")),
m_Year(_T("")),
m_Comment(_T("")),
m_Genre(_T("")),
m_f()
{
}

CID3TAG::~CID3TAG()
{
	Close();
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:Open
* 函数介绍:只读/读写、共享打开MP3文件, 为读取MP3文件信息做准备.
            要求打开MCI时用MCI_OPEN_SHAREABLE.
* 输入参数:LPSTR strPath, MP3文件路径
            BOOL bRead, true=读, false=写
* 输出参数:无
* 返回值  :BOOL, true=成功, false=失败
*/
BOOL CID3TAG::Open(LPSTR strPath, BOOL bRead)
{
	TRY{
	if (bRead)
		m_f.Open(strPath,
			CFile::typeBinary |
			CFile::modeRead |
			CFile::shareDenyNone);
	else
		m_f.Open(strPath,
			CFile::typeBinary |
			CFile::modeReadWrite |
			CFile::shareDenyWrite);
//	return this->GetData();
	}CATCH( CFileException, e )
	{	return false;
	}
	END_CATCH
	if (m_f.m_hFile == (UINT)CFile::hFileNull)
		return false;
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:Close
* 函数介绍:关闭已打开的MP3文件
* 输入参数:无
* 输出参数:无
* 返回值  :BOOL, true=成功, false=失败
*/
BOOL CID3TAG::Close()
{	
	if (m_f.m_hFile!=(UINT)CFile::hFileNull)
		m_f.Close();
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetData
* 函数介绍:读取ID3信息, 并存储到数据成员
            ID3信息:(MP3文件的后128字节)
            "TAG"(标志):3
            Title(标题):30
            Artist(艺术家):30
            Album(纪念册):30
            Year(年份):4
            Comment(注释):30
            Genre(流派):1
* 输入参数:无
* 输出参数:无
* 返回值  :BOOL, true=成功, false=失败
*/
BOOL CID3TAG::GetData()
{
	TRY{
	CString str=_T("");
	m_f.Seek(-128, CFile::end);
	m_f.Read(str.GetBuffer(3), 3);
/*直接修改CString:
The GetBuffer and ReleaseBuffer member functions allow you 
to gain access to the internal character buffer of a CString 
object and modify it directly. The following steps show how 
to use these functions for this purpose: 

1.Call GetBuffer for a CString object, specifying the length of 
the buffer you require.

2.Use the pointer returned by GetBuffer to write characters
directly into the CString object.

3.Call ReleaseBuffer for the CString object to update all the 
internal CString state information (such as the length of the 
string). After modifying a CString object’s contents directly,
you must call ReleaseBuffer before calling any other CString
member functions. 
*/
	str.ReleaseBuffer();

	if (str==_T("TAG"))	//标志为"TAG"
	{	m_f.Read(m_Title.GetBuffer(30), 30);
		m_Title.ReleaseBuffer();
		m_f.Read(m_Artist.GetBuffer(30), 30);
		m_Artist.ReleaseBuffer();
		m_f.Read(m_Album.GetBuffer(30), 30);
		m_Album.ReleaseBuffer();
		m_f.Read(m_Year.GetBuffer(4), 4);
		m_Year.ReleaseBuffer();
		m_f.Read(m_Comment.GetBuffer(30), 30);
		m_Comment.ReleaseBuffer();
		m_f.Read(m_Genre.GetBuffer(1), 1);
		m_Genre.ReleaseBuffer();
	}else
		return false;
	}CATCH( CFileException, e )
	{	return false;
	}
	END_CATCH
	return true;
}

////////////////////////////////////////////////////////////////////////
/*
* 函数名称:GetSongTitle
* 函数介绍:返回Title
* 输入参数:无
* 输出参数:无
* 返回值  :CString, Title
*/
//返回Title
CString CID3TAG::GetSongTitle()
{
	m_Title.TrimLeft();
	m_Title.TrimRight();
	return m_Title;
}

////////////////////////////////////////////////////////////////////////
//返回Artist
CString CID3TAG::GetSongArtist()
{
	m_Artist.TrimLeft();
	m_Artist.TrimRight();
	return m_Artist;
}

////////////////////////////////////////////////////////////////////////
//返回Album
CString CID3TAG::GetSongAlbum()
{
	m_Album.TrimLeft();
	m_Album.TrimRight();
	return m_Album;
}

////////////////////////////////////////////////////////////////////////
//返回Year
CString CID3TAG::GetSongYear()
{
	m_Year.TrimLeft();
	m_Year.TrimRight();
	return m_Year;
}

////////////////////////////////////////////////////////////////////////
//返回Comment
CString CID3TAG::GetSongComment()
{
	m_Comment.TrimLeft();
	m_Comment.TrimRight();
	return m_Comment;
}

////////////////////////////////////////////////////////////////////////
//返回Genre
CString CID3TAG::GetSongGenre()
{
	m_Genre.TrimLeft();
	m_Genre.TrimRight();
	return m_Genre;
}

////////////////////////////////////////////////////////////////////////
//将MP3文件的各种信息写入文件
BOOL CID3TAG::SetData()
{	TRY
	{
	CString str=_T("");
	m_f.Seek(-128, CFile::end);
	m_f.Read(str.GetBuffer(3), 3);
	str.ReleaseBuffer();
	if (str!=_T("TAG"))	//标志为"TAG"
	{	m_f.SeekToEnd();
		str="TAG";
		m_f.Write(str.GetBuffer(str.GetLength()),3);
	}
	m_f.Write(m_Title.GetBuffer(m_Title.GetLength()),30);
	m_f.Write(m_Artist.GetBuffer(m_Artist.GetLength()),30);
	m_f.Write(m_Album.GetBuffer(m_Album.GetLength()),30);
	m_f.Write(m_Year.GetBuffer(m_Year.GetLength()),4);
	m_f.Write(m_Comment.GetBuffer(m_Comment.GetLength()),30);
	m_f.Write(m_Genre.GetBuffer(m_Genre.GetLength()),1);

	}CATCH( CFileException, e )
	{	TRACE("不能打开文件!\n");
		return false;
	}
	END_CATCH
	return true;
}

////////////////////////////////////////////////////////////////////////
//设置Title的值
void CID3TAG::SetSongTitle(const CString &strSongTitle)
{
	m_Title=strSongTitle;
}

////////////////////////////////////////////////////////////////////////
//设置Artist的值
void CID3TAG::SetSongArtist(const CString &strSongArtist)
{
	m_Artist=strSongArtist;
}

////////////////////////////////////////////////////////////////////////
//设置Album的值
void CID3TAG::SetSongAlbum(const CString &strSongAlbum)
{	
	m_Album=strSongAlbum;
}

////////////////////////////////////////////////////////////////////////
//设置Year的值
void CID3TAG::SetSongYear(const CString &strSongYear)
{
	m_Year=strSongYear;
}

////////////////////////////////////////////////////////////////////////
//设置Comment的值
void CID3TAG::SetSongComment(const CString &strSongComment)
{
	m_Comment=strSongComment;
}

////////////////////////////////////////////////////////////////////////
//设置Genre的值
void CID3TAG::SetSongGenre(const CString &strSongGenre)
{
	m_Genre=strSongGenre;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -