使用ffmpeg为库编写的小型多媒体播放器源代码

本文介绍了作者基于ffmpeg库编写的简单多媒体播放器,代码中并未涉及复杂的音视频同步技术,而是采用视频跟随音频同步的方式。源代码可供读者参考学习。
摘要由CSDN通过智能技术生成

 今天突发奇想,就在以前音频播放器(详细情况请看这里——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,
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值