今天突发奇想,就在以前音频播放器(详细情况请看这里——http://blog.csdn.net/baymoon/archive/2006/11/16/1388693.aspx)的基础上用ffmpeg写了个简单的多媒体播放器,这里把源代码贴出来,供大家参评;这里的多媒体播放,并没有用到什么很强大的音视频同步技术,而只是简单的使用了视频随着音频同步,想必你看了代码之后会有所悟的。。。不多说了,看代码。。。
/**/
/***************************************************************************
* main.cc
*
* Thu Nov 9 20:47:33 2006
* Copyright 2006
* Email lsosa.cs2c
****************************************************************************/
#include < avcodec.h >
#include < avformat.h >
#include < avutil.h >
#include < assert.h >
#include < stdio.h >
#include < stdlib.h >
#include < X11 / Xlib.h >
#include < sys / soundcard.h >
#include < sys / stat.h >
#include < fcntl.h >
#include < sys / ioctl.h >
#include < unistd.h >
#include < errno.h >
#include < string .h >
#include < sched.h >
#include < SDL / SDL.h >
#define ALL_DEBUG
#ifdef ALL_DEBUG
#define AV_DEBUG
#define AUDIO_DEBUG
#endif
// ------------------------------------------------------------------------------
// manipulations for file
int open_file ( char * file_name, int mode)
... {
// open file file_name and return the file descriptor;
int fd;
if ((fd = open (file_name, mode)) < 0)
...{
fprintf (stderr, " Can't open %s! ", file_name);
exit (-1);
}
return fd;
}
int set_audio ( int fd, AVCodecContext * pCodecCtx)
... {
// set the properties of audio device with pCodecCtx;
int i, err;
/**//* 设置适当的参数,使得声音设备工作正常 */
/**//* 详细情况请参考Linux关于声卡编程的文档 */
i = 0;
ioctl (fd, SNDCTL_DSP_RESET, &i);
i = 0;
ioctl (fd, SNDCTL_DSP_SYNC, &i);
i = 1;
ioctl (fd, SNDCTL_DSP_NONBLOCK, &i);
// set sample rate;
#ifdef AUDIO_DEBUG
printf ("pCodecCtx->sample_rate:%d ", pCodecCtx->sample_rate);
#endif
i = pCodecCtx->sample_rate;
if (ioctl (fd, SNDCTL_DSP_SPEED, &i) == -1)
...{
fprintf (stderr, "Set speed to %d failed:%s ", i,
strerror (errno));
return (-1);
}
if (i != pCodecCtx->sample_rate)
...{
fprintf (stderr, "do not support speed %d,supported is %d ",
pCodecCtx->sample_rate, i);
return (-1);
}
// set channels;
i = pCodecCtx->channels;
#ifdef AUDIO_DEBUG
printf ("pCodecCtx->channels:%d ", pCodecCtx->channels);
#endif
if ((ioctl (fd, SNDCTL_DSP_CHANNELS, &i)) == -1)
...{
fprintf (stderr, "Set Audio Channels %d failed:%s ", i,
* main.cc
*
* Thu Nov 9 20:47:33 2006
* Copyright 2006
* Email lsosa.cs2c
****************************************************************************/
#include < avcodec.h >
#include < avformat.h >
#include < avutil.h >
#include < assert.h >
#include < stdio.h >
#include < stdlib.h >
#include < X11 / Xlib.h >
#include < sys / soundcard.h >
#include < sys / stat.h >
#include < fcntl.h >
#include < sys / ioctl.h >
#include < unistd.h >
#include < errno.h >
#include < string .h >
#include < sched.h >
#include < SDL / SDL.h >
#define ALL_DEBUG
#ifdef ALL_DEBUG
#define AV_DEBUG
#define AUDIO_DEBUG
#endif
// ------------------------------------------------------------------------------
// manipulations for file
int open_file ( char * file_name, int mode)
... {
// open file file_name and return the file descriptor;
int fd;
if ((fd = open (file_name, mode)) < 0)
...{
fprintf (stderr, " Can't open %s! ", file_name);
exit (-1);
}
return fd;
}
int set_audio ( int fd, AVCodecContext * pCodecCtx)
... {
// set the properties of audio device with pCodecCtx;
int i, err;
/**//* 设置适当的参数,使得声音设备工作正常 */
/**//* 详细情况请参考Linux关于声卡编程的文档 */
i = 0;
ioctl (fd, SNDCTL_DSP_RESET, &i);
i = 0;
ioctl (fd, SNDCTL_DSP_SYNC, &i);
i = 1;
ioctl (fd, SNDCTL_DSP_NONBLOCK, &i);
// set sample rate;
#ifdef AUDIO_DEBUG
printf ("pCodecCtx->sample_rate:%d ", pCodecCtx->sample_rate);
#endif
i = pCodecCtx->sample_rate;
if (ioctl (fd, SNDCTL_DSP_SPEED, &i) == -1)
...{
fprintf (stderr, "Set speed to %d failed:%s ", i,
strerror (errno));
return (-1);
}
if (i != pCodecCtx->sample_rate)
...{
fprintf (stderr, "do not support speed %d,supported is %d ",
pCodecCtx->sample_rate, i);
return (-1);
}
// set channels;
i = pCodecCtx->channels;
#ifdef AUDIO_DEBUG
printf ("pCodecCtx->channels:%d ", pCodecCtx->channels);
#endif
if ((ioctl (fd, SNDCTL_DSP_CHANNELS, &i)) == -1)
...{
fprintf (stderr, "Set Audio Channels %d failed:%s ", i,