c语言如何随机获取1kb,基于VS2010+C语言实现播放器的顺序播放、随机播放

1.[文件] music.h ~ 920B     下载(38)

/*

* File: music.h

* Time: 2014/10/11

*/

#ifndef __MUSIC_H__

#define __MUSIC_H__

typedef enum { UNPLAYED, PLAYED } BOOL; // 自定义一个bool类型

typedef enum { ORDER, RANDOM } PLAY_MODEL; // 自定义一个播放类型

typedef char *MUSIC_NAME; // 歌曲名

typedef MUSIC_NAME *MUSIC_NAME_TAB; // 歌曲名表

#define MUSIC_NAME_SIZE 60

typedef struct _music_ {

char str[MUSIC_NAME_SIZE];

BOOL isPlayed; // 是否被播放过

} MUSIC; // 歌曲 MUSIC == struct _music_

typedef MUSIC *MUSIC_TAB; // 歌曲播放列表 MUSIC_TAB == MUSIC *

void systemInit(void); // 系统初始化,必须在listPlay()之前调用,且只能调用一次

void play(const MUSIC_NAME_TAB musNametab, const int musnum, const PLAY_MODEL playModel);

// 按照歌曲名表musNametab播放歌曲;musnum为歌曲数量;playModel为播放模式;

// playModel:ORDER(按歌曲名称顺序播放) RANDOM(随机播放)

#endif

/*

* End of file

*/

2.[文件] music.c ~ 4KB     下载(33)

/*

* File: music.c

* Time: 2014/10/11

*/

#include "music.h"

#include

#include

#include

#include

#include

static MUSIC_TAB newtab = NULL; // 全局变量,一张经过处理(排序/洗牌)的新的歌曲表单

static void playOneMusic(MUSIC mus); // 播放歌曲mus

static MUSIC_TAB orderList(const MUSIC_TAB oldtab, const int musnum); // 获得新的有序列表

static MUSIC_TAB randomLis(const MUSIC_TAB oldtab, const int musnum); // 获得新的随机列表

static void listPlay(const MUSIC_TAB mustab, int musnum, const PLAY_MODEL playModel); // 按照新表单播放

//==========================================================================================================

// 系统初始化,必须在listPlay()之前调用,且只能调用一次

void systemInit(void) {

srand((unsigned int)time(NULL));

}

// 按照歌曲名表musNametab播放歌曲

void play(const MUSIC_NAME_TAB musNametab, const int musnum, const PLAY_MODEL playModel) {

int i;

newtab = (MUSIC_TAB)malloc(sizeof(MUSIC) * musnum);

if (newtab != NULL) {

for (i = 0; i < musnum; i++) { // 装载歌曲名信息,并清零已播放标志

memset(&(newtab[i].str[0]), 0, sizeof(char)*MUSIC_NAME_SIZE);

strcpy(newtab[i].str, musNametab[i]);

newtab[i].isPlayed = UNPLAYED;

}

listPlay(newtab, musnum, playModel);

} else {

fprintf(stderr,"\n Program Running Wrong!\n");

return;

}

}

//==========================================================================================================

static void listPlay(const MUSIC_TAB mustab, int musnum, const PLAY_MODEL playModel) { // 按照新表单播放

//以下注释部分是最初的错误代码

//if (playModel == ORDER) {

//newtab = orderList(mustab, musnum); // 获得新的有序列表

//} else {

//newtab = randomLis(mustab, musnum); // 获得新的随机列表

//}

//while(--musnum >= 0) {

//playOneMusic(*newtab++);// 播放某一歌曲

//}

//free(newtab);

//以下部分是调试之后改正的代码 ========================================

MUSIC_TAB tmp = NULL;

if (playModel == ORDER) {

newtab = orderList(mustab, musnum); // 获得新的有序列表

} else {

newtab = randomLis(mustab, musnum); // 获得新的随机列表

}

tmp = newtab;

while(--musnum >= 0) {

playOneMusic(*tmp++);// 播放某一歌曲

}

if (newtab != NULL) {

free(newtab);

}

}

static void playOneMusic(MUSIC mus) {

if (mus.isPlayed == UNPLAYED) { // 如果歌曲mus在当前循环尚未播放过

if (mus.str != NULL) {

if (strlen(mus.str) <= 0) { // 歌曲名为空字符串

fprintf(stderr,"\n The file of the song \"%s\" is empty !!!\n");

} else {

printf("\n Start playing the song: "); // 模拟歌曲播放

puts(mus.str);

printf(" ");

putchar('.'); Sleep(500); putchar('.'); Sleep(500); putchar('.');

printf("\n Stop playing the song.\n ");

Sleep(500);

}

} else { // 歌曲名为空

fprintf(stderr,"\n Unfound the song \"%s\" !!!\n", mus.str);

}

}

mus.isPlayed = PLAYED;

}

static MUSIC_TAB orderList(const MUSIC_TAB oldtab, const int musnum) { // 获得新的有序列表

if (newtab != NULL) { // newtab为全局变量

int i,j;

MUSIC temp;

memcpy(newtab, oldtab, sizeof(MUSIC)*musnum);

// 将newtab[0]~newtab[musnum-1]排序

for (i = 0; i < musnum-1; i++) {

for (j = i+1; j < musnum; j++) {

if (strcmp(newtab[i].str, newtab[j].str) > 0) { // newtab[i]->str > newtab[j]->str

temp = newtab[i];

newtab[i] = newtab[j];

newtab[j] = temp;

}

}

}

return newtab;

} // @ if (newtab != NULL)

// 如果newtab==NULL则不作任何处理,统一由listPlay()处理

}

static int myRand(int len) { //生成一个[0,len-1]之间的随机数

int iRand;

iRand = rand()%len; // 这就是为什么需要systemInit()的原因

return iRand;

}

#define SWAP(a,b,_t) do { _t t = a; a = b; b = t; } while(0)

static void shuffle(const MUSIC_TAB table, const int musnum) { // 洗牌

int n;

// 将table[0]~table[musnum-1]洗牌

// 具体算法:http://bbs.bccn.net/thread-331122-1-1.html

for (n = musnum; n > 1; --n) {

int i = myRand(n);

SWAP(table[n-1], table[i], MUSIC);

}

}

static MUSIC_TAB randomLis(const MUSIC_TAB oldtab, const int musnum) { // 获得新的随机列表

if (newtab != NULL) { // newtab为全局变量

memcpy(newtab, oldtab, sizeof(MUSIC)*musnum);

shuffle(newtab, musnum);

return newtab;

} // @ if (newtab != NULL)

// 如果newtab==NULL则不作任何处理,统一由listPlay()处理

}

/*

* End of file

*/

3.[文件] playmusic.c ~ 1KB     下载(30)

/*

* File: playmusic.c

* Time: 2014/10/11

*/

#include "music.h"

#include

// �˳��򽻲�ʵ��5��������Ÿ�����4��˳�򲥷Ÿ���

// �˸������ԣ�http://music.baidu.com/explore/������Ӣ�ĸ���?fm=altg8

#define SONGS_LEN 6

char *Songs[SONGS_LEN] = {

// strlen(������) <= MUSIC_NAME_SIZE ��@ music.h��

// �Ҹ�������= NULL

"Moves Like Jagger",

"Love Story",

"Unforgivable Sinner",

"Bad Romance",

"Father And Son",

"Wonderful",

};

int main(void) {

systemInit();

puts("\n Initialize system....");

puts("\n ==== ORDER ==== ");

play(Songs, SONGS_LEN, ORDER);

puts("\n ==== RANDOM ==== ");

play(Songs, SONGS_LEN, RANDOM);

puts("\n ==== ORDER ==== ");

play(Songs, SONGS_LEN, ORDER);

puts("\n ==== RANDOM ==== ");

play(Songs, SONGS_LEN, RANDOM);

puts("\n ==== ORDER ==== ");

play(Songs, SONGS_LEN, ORDER);

puts("\n ==== RANDOM ==== ");

play(Songs, SONGS_LEN, RANDOM);

puts("\n Exit system....\n");

return 0;

}

/*

* End of file

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值