当前位置:操作系统 > 安卓/Android >>

怎样在Android本地视频播放器开发

在上一章Android本地视频播放器开发--SDL编译编译中编译出sdl的支持库,当时我们使用的2.0,但是有些api被更改了,所以在以下的使用者中我们使用SDL1.3的库,这个库我会传上源码以及编译出的库,接下来这张我们使用ffmpeg解码视频文件中的视频帧同时使用SDL去显示。

1、Decodec_Video.c 这是我视频解码的文件,其中内容如下:


[cpp]
#include <stdio.h> 
#include <android/log.h> 
 
#ifdef __MINGW32__ 
#undef main /* Prevents SDL from overriding main() */ 
#endif 
 
#include "../SDL/include/SDL.h" 
#include "../SDL/include/SDL_thread.h" 
 
#include "VideoPlayerDecode.h" 
#include "../ffmpeg/libavutil/avutil.h" 
#include "../ffmpeg/libavcodec/avcodec.h" 
#include "../ffmpeg/libavformat/avformat.h" 
#include "../ffmpeg/libswscale/swscale.h" 
 
AVFormatContext *pFormatCtx;
int             i, videoStream;
AVCodecContext  *pCodecCtx;
AVCodec         *pCodec;
AVFrame         *pFrame;
AVPacket        packet;
int             frameFinished;
float           aspect_ratio;
 
static struct SwsContext *img_convert_ctx;
SDL_Su易做图ce     *screen;
SDL_Overlay *bmp;
SDL_Rect        rect;
SDL_Event       event;
 
 
JNIEXPORT jint JNICALL Java_com_zhangjie_graduation_videopalyer_jni_VideoPlayerDecode_VideoPlayer
(JNIEnv *env, jclass clz, jstring fileName)
{
    const char* local_title = (*env)->GetStringUTFChars(env, fileName, NULL);
    av_register_all();//注册所有支持的文件格式以及编解码器 
    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
        exit(1);
    }
    if(avformat_open_input(&pFormatCtx, local_title, NULL, NULL) != 0)
                return -1;
    if(avformat_find_stream_info(pFormatCtx, NULL) < 0)
                return -1;
    av_dump_format(pFormatCtx, -1, local_title, 0);
    videoStream=-1;
    for(i=0; i<pFormatCtx->nb_streams; i++)
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
            videoStream=i;
            break;
        }
    if(videoStream==-1)
        return -1; // Didn't find a video stream 
    // Get a pointer to the codec context for the video stream 
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;
 
    // Find the decoder for the video stream 
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
    if(pCodec==NULL) {
        fprintf(stderr, "Unsupported codec!\n");
        return -1; // Codec not found 
    }
    if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0)return -1;
    pFrame = avcodec_alloc_frame();
    if(pFrame == NULL)return -1;
    // Make a screen to put our video 
#ifndef __DARWIN__ 
    screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0);
#else 
    screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 24, 0);
#endif 
    if(!screen) {
        fprintf(stderr, "SDL: could not set video mode - exiting\n");
        exit(1);
    }
    // Allocate a place to put our YUV image on that screen 
    bmp = SDL_CreateYUVOverlay(pCodecCtx->width,
            pCodecCtx->height,
            SDL_YV12_OVERLAY,
            screen);
    img_convert_ctx = sws_getContext(pCodecCtx->width,  
                          pCodecCtx->height, pCodecCtx->pix_fmt,  
                          pCodecCtx->width, pCodecCtx->height,  
                          PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);  
 
    // Read frames and save first five frames to disk 
    i=0;
    while(av_read_frame(pFormatCtx, &packet)>=0) {
        // Is this a packet from the video stream? 
        if(packet.stream_index==videoStream) {
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
            // Did we get a video frame? 
            if(frameFinished) {
                SDL_LockYUVOverlay(bmp);
                
                AVPicture *pict;
                pict->data[0] = bmp->pixels[0];
                pict->data[1] = bmp->pixels[2];
                pict->data[2] = bmp->pixels[1];
 
                pict->linesize[0] = bmp->pitches[0];
                pict->linesize[1] = bmp->pitches[2];
                pict->linesize[2] = bmp

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,