首页 » IOS » SDL ios音频简单测试例子-借鉴雷大神的代码

SDL ios音频简单测试例子-借鉴雷大神的代码

原文 http://blog.csdn.net/u013654125/article/details/79226397

2018-02-02 02:01:23阅读(474)

本实例主要代码如下:

AudioPlayerSDL2.mm

//
//  AudiosDLTest.m
//  AudioTest
//
//  Created by zhufu on 2018/1/20.
//  Copyright © 2018年 zhufu. All rights reserved.
//
#import "AudioPlayerSDL2.h"
//Buffer:
//|-----------|-------------|
//chunk-------pos---len-----|
static  Uint32  audio_len;
static  Uint8  *audio_pos;
/* Audio Callback
 * The audio function callback takes the following parameters:
 * stream: A pointer to the audio buffer to be filled
 * len: The length (in bytes) of the audio buffer
 *
 */
void  fill_audio(void *udata,Uint8 *stream,int len){
    //SDL 2.0
    SDL_memset(stream, 0, len);
    if(audio_len==0)        /*  Only  play  if  we  have  data  left  */
        return;
    
    len=(len>audio_len?audio_len:len);    /*  Mix  as  much  data  as  possible  */
    printf("audio   %d %d\n", audio_len, len);
    SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
    audio_pos += len;
    
    audio_len -= len;
}
int cppTest()
{
    SDL_SetMainReady();
    //Init
    if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        printf( "Could not initialize SDL - %s\n", SDL_GetError());
        return -1;
    }
    //SDL_AudioSpec
    SDL_AudioSpec wanted_spec;
    wanted_spec.freq = 44100;
    wanted_spec.format = AUDIO_S16SYS;
    wanted_spec.channels = 2;
    wanted_spec.silence = 0;
    wanted_spec.samples = 1024;
    wanted_spec.callback = fill_audio;
    
    if (SDL_OpenAudio(&wanted_spec, NULL)<0){
        printf("can't open audio.\n");
        return -1;
    }
    
    NSString *ppth = [[NSBundle mainBundle] pathForResource:@"testv" ofType:@"pcm"];
    FILE *fp=fopen([ppth UTF8String],"r");
    if(fp==NULL){
        printf("cannot open this file\n");
        return -1;
    }
    //For YUV420P
    int pcm_buffer_size=4096;
    char *pcm_buffer=(char *)malloc(pcm_buffer_size);
    int data_count=0;
    
    while(1){
        if (fread(pcm_buffer, 1, pcm_buffer_size, fp) != pcm_buffer_size){
            // Loop
            fseek(fp, 0, SEEK_SET);
            fread(pcm_buffer, 1, pcm_buffer_size, fp);
            data_count=0;
        }
        printf("Now Playing %10d Bytes data.\n",data_count);
        data_count+=pcm_buffer_size;
        //Set audio buffer (PCM data)
        //Audio buffer length
        audio_len =pcm_buffer_size;
        audio_pos = (Uint8 *) pcm_buffer;
        //Play
        
        SDL_PauseAudio(0);
        while(audio_len>0){ //Wait until finish
            SDL_Delay(1);
        }
    }
    
    return 0;
}

AudioPlayerSDL2.h

//
//  AudioPlayerSDL2.h
//  AudioTest
//
//  Created by zhufu on 2018/1/20.
//  Copyright © 2018年 zhufu. All rights reserved.
//
#ifndef AudioPlayerSDL2_h
#define AudioPlayerSDL2_h
#include <stdio.h>
#include "include/SDL.h"
#include "AudioSDLTest.h"
void  fill_audio(void *udata,Uint8 *stream,int len);
int cppTest(void);
#endif /* AudioPlayerSDL2_h */

源代码https://github.com/zhu12345618/iOSAudioTest

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹