#include<QtGui>
#include"into.h"
#include"client.h"
#include<connect_warn.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<unistd.h>
#include<signal.h>
#include"playmusic.h"
#include<pthread.h>
#define LIST_NUMBER 10
int socket_fd;
int dsp;
int c_pos;
int flag;
char last_song[50];
struct client_song
{
int d;
Client *c;
};
#include<sys/stat.h>
#include<dirent.h>
#include<wait.h>
int connectfunc(int PORT,char *ip)
{
if((socket_fd=socket(PF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
return -1;
}
printf("socket ok\n");
printf("ip=%s\nport=%d\n",ip,PORT);
struct sockaddr_in server_client;
server_client.sin_family=AF_INET;
server_client.sin_port=htons(PORT);
server_client.sin_addr.s_addr=inet_addr(ip);
bzero(server_client.sin_zero,sizeof(server_client.sin_zero));
if(connect(socket_fd,(struct sockaddr *)&server_client,sizeof(struct sockaddr))==-1)
{
perror("connect");
return -1;
}
printf("connect ok\n");
if(send(socket_fd,&server_client,sizeof(server_client),0)<0)
perror("send addr");
return socket_fd;
}
int change(char *s)
{
int m,n,i,j,z=0;
n=strlen(s);
for(i=0;i<n;i++)
{
m=s[i]-'0';
for(j=1;j<n-i;j++)
m*=10;
z+=m;
}
return z;
}
void f1(int sig)
{
if(sig==SIGALRM)
{
printf("\nconnect failed!\nPlease make sure that the ip and port rhigt!\n\n");
Connect_warn c_warn;
c_warn.exec();
flag=1;
}
}
//------------------------------------------------
Client::Client(QWidget *parent, Qt::WindowFlags f):QDialog(parent,f)
{
myui.setupUi(this);
this->show();
emit this->OnList(myui.listbtn->isChecked());
}
void Client::OnInto()
{
signal(SIGALRM,f1);
Into into;
char port[6];
char ip[20];
if(into.exec()==1)
{
alarm(10);
strcpy(port,into.ui.portEdit->text().toUtf8().data());
strcpy(ip,into.ui.ipEdit->text().toUtf8().data());
//int socket_fd;
int PORT=change(port);
while(flag==0)
{
if((socket_fd=connectfunc(PORT,ip))>0)
{
alarm(0);
myui.connectbtn->setText(QObject::trUtf8("已连接"));
break;
}
}
if(exec()==0)
::close(socket_fd);
}
} //-------------------------------------------连接服务器 槽函数---
void Client::OnList(bool Blist)
{
int height;
//this->myui.groupBox2->setVisible(Blist);
if(Blist==true)
{
height=this->myui.groupBox1->geometry().height()+this->myui.groupBox2->geometry().height();
}
else
height=this->myui.groupBox1->geometry().height();
}//-----------------------------------------歌曲列表 函数---------
void Client::Renew()
{
char s[50];
int recvlen;
if(send(socket_fd,"list",strlen("list")+1,0)<0)
perror("send renew");
myui.music_list->clear();
while(1)
{
if((recvlen=recv(socket_fd,s,sizeof(s),0))<0)
{
printf("Please enter the server!\n");
return ;
}
if(strcmp(s,"over")==0)
break;
myui.music_list->addItem(QObject::trUtf8(s));
}
}//--------------------------------------刷新 槽函数------
void *pthread_play(void *p)
{
client_song p_song=(*((struct client_song *)p));printf("d=%d\n",p_song.d);
Client *cl=p_song.c;
FILE * fp;
WAVEFILE_HEADER head;//wav 文件头
int i=0,pos;
char * buffer=NULL;
char song[30],t1[]="00:00",t2[]="/00:00";
if(p_song.d==1)
{
strcpy(song,"./");
strcat(song,last_song);
}
else
strcpy(song,"./song.txt");
if(access("./song.txt", R_OK)!=0) //检测文件是否存在
{
fprintf(stderr, "Can't access the wave file <%s>\n", "song.txt");
exit(0);
}
fp=fopen("./song.txt", "rb"); //-------已读方式打开音乐wav文件--------
if(fp==NULL)
{
fprintf(stderr, "Can't open the file for read!<%s>\n", strerror(errno));
exit(0);
}
if(fread(&head, sizeof(head), 1, fp)!=1) //------读取文件头---
{
fprintf(stderr, "Can't read the wave file!<%s>\n", strerror(errno));
//fclose(fp);
//exit(0);
}
if(memcmp(head.ChunkID, "RIF",3)) //------验证文件头,文件格式是否为RIF---------
{
perror("Unsupport format!\n");
fclose(fp);
exit(0);
}
if(head.BitsPerSample==32) //------验证bit率---------
{
perror("Unsupport sample size!\n");
fclose(fp);
exit(0);
}
dsp=open_dsp();//打开声卡设备文件
if(dsp!=-1)
{
if(setup_dsp(dsp, head.SampleRate, head.NumChannels-1)==0)//调用自定义setup_dsp:设置设备的参数
{
if(sync_dsp(dsp)==0)//参数设置好后开 始读wav文件,并向dsp设备写(播放)
{
long size,song_size=head.Subchunk2Size;
long reads;
size=(head.BitsPerSample*head.SampleRate+7)>>2;//每次写入设备的字节数。
buffer=(char *)malloc(size);//音乐数据缓存
int n=song_size/size;
int min=n/60,sec=n%60;
t2[1]=min/10+'0';
t2[2]=min%10+'0';
t2[4]=sec/10+'0';
t2[5]=sec%10+'0';
cl->myui.time2->setText(QObject::trUtf8(t2));
cl->myui.music_Slider->setMaximum(n);
if(buffer!=NULL)
{
printf("play now..\n");
do
{
if(flag==3)
break;
else if(flag!=1)
{
if(flag==4)
{
fseek(fp,(c_pos)*reads,SEEK_SET);
flag=0;
i=c_pos;
cl->myui.music_Slider->setSliderPosition(c_pos);
}
else
{
pos=cl->myui.music_Slider->sliderPosition();
fseek(fp,(pos)*reads,SEEK_SET);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
播放器.rar (24个子文件)
播放器
server
main.cpp 206B
ui_add.h 3KB
server.h 339B
moc_server.cpp 3KB
moc_add.cpp 2KB
server.cpp 9KB
exit_warn.cpp 160B
ui_server.h 6KB
ui_exit_warn.h 3KB
add.cpp 432B
exit_warn.h 261B
add.h 271B
moc_exit_warn.cpp 2KB
client
main.cpp 256B
moc_into.cpp 2KB
connect_warn.cpp 170B
moc_client.cpp 3KB
client.h 373B
moc_connect_warn.cpp 2KB
ui_client.h 7KB
connect_warn.h 270B
playmusic.h 2KB
client.cpp 12KB
into.h 251B
共 24 条
- 1
浅秋mylove
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页