1.基于linux的音乐播放器的简单快进,后退,暂停播放歌曲实现学习;
实验环境:基于ubutu系统的开发;系统环境已经配置好了;
#include <gtk/gtk.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "./sungtk_interface/sungtk_interface.h"
#define LOGIN_W 800
#define LOGIN_H 480
/*************************************************
函数功能:快退
函数参数:widget:控件,data:传过来的结构体变量地址
函数返回值:无
**************************************************/
void deal_backward(GtkWidget *widget,gpointer data)
{
int fd = open("./fifo_cmd",O_RDWR);
write(fd,"seek -5\n",strlen("seek -5\n"));
printf("cmd is seek -5\n");
close(fd);
}
/*************************************************
函数功能:暂停
函数参数:widget:控件,data:传过来的结构体变量地址
函数返回值:无
**************************************************/
void deal_pause(GtkWidget *widget,gpointer data)
{
int fd = open("./fifo_cmd",O_RDWR);
write(fd,"pause\n",strlen("pause\n"));
printf("cmd is pause\n");
close(fd);
}
/*************************************************
函数功能:快进
函数参数:widget:控件,data:传过来的结构体变量地址
函数返回值:无
**************************************************/
void deal_forward(GtkWidget *widget,gpointer data)
{
int fd = open("./fifo_cmd",O_RDWR);
write(fd,"seek +5\n",strlen("seek +5\n"));
printf("cmd is seek +5\n");
close(fd);
}
先安装:
/*************************************************
函数功能:mplayer控件的初始化
函数参数:无
函数返回值:无,有宏控制
**************************************************/
void mplayer_win_init()
{
//打开glade文件,返回类型GladeXML
GladeXML *gxml = glade_xml_new("./glade_file/mplayer.glade",NULL,NULL);
//通过该接口,获取创建的控件
GtkWidget *window = glade_xml_get_widget(gxml,"window_back");
// 背景图
sungtk_background_set_picture(window, "./skin/bg.jpg",LOGIN_W, LOGIN_H );
//快退
GtkWidget *button_backward = glade_xml_get_widget(gxml,"button_backward");//获取按钮控件
GtkWidget *image_backward = glade_xml_get_widget(gxml,"image_backward");//获取图片控件
sungtk_image_load_picture(image_backward,"./skin/back.png",80,80);//按钮图片
g_signal_connect(button_backward, "pressed", G_CALLBACK(deal_backward), NULL);//入口deal_backward
//暂停
GtkWidget *button_pause = glade_xml_get_widget(gxml,"button_pause");//获取按钮控件
GtkWidget *image_pause = glade_xml_get_widget(gxml,"image_pause");//获取图片控件
sungtk_image_load_picture(image_pause,"./skin/pause.png",80,80);//按钮图片
g_signal_connect(button_pause, "pressed", G_CALLBACK(deal_pause), NULL);//入口deal_backward
//快进
GtkWidget *button_forward = glade_xml_get_widget(gxml,"button_forward");//获取按钮控件
GtkWidget *image_forward = glade_xml_get_widget(gxml,"image_forward");//获取图片控件
sungtk_image_load_picture(image_forward,"./skin/front.png",80,80);//按钮图片
g_signal_connect(button_forward, "pressed", G_CALLBACK(deal_forward), NULL);//入口deal_backward
gtk_widget_show_all(window);
}
/*************************************************
函数功能:创建进程启动mplayer并初始化ui
函数参数:无
函数返回值:无
**************************************************/
void mplayer_process()
{
remove("./fifo_cmd");
mkfifo("./fifo_cmd",0666);
pid_t pid;
pid = fork();
if(pid < 0)
{
perror("fork");
}
if(pid == 0)
{
//子进程启动mplayer
execlp("mplayer","mplayer","-quiet","-idle",
"-slave","-input","file=./fifo_cmd",
"./song/coffee.mp3",NULL);
perror("execl");
_exit(1);
}
}
int main(int argc,char *argv[])
{
gtk_init(&argc,&argv); //循环等待事件发生
mplayer_win_init();//界面初始化
mplayer_process();//启动mplayer
gtk_main();
return 0;}
2.
简单代码如下:
#include<gtk/gtk.h>
void callback( GtkButton *button,gpointer user_data )
{
printf("button pressed!\n");
execlp("mplayer","mplayer","-slave","-quiet","-idle","/home/edu/share/music/sad_or_happy.mp3",NULL);
//"-input",\
//"file=./fifo_cmd",\ "/home/edu/share/music/sad_or_happy.mp3",NULL);
}
int main(int argc,char *argv[]){
gtk_init(&argc,&argv);
//创建一个窗口
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title((GtkWindow *)window,"first window");
gtk_widget_set_size_request(window,600,300);
GtkWidget *button=gtk_button_new_with_label("button");
gtk_container_add((GtkContainer *)window,button);
g_signal_connect(button,"pressed",G_CALLBACK(callback),NULL);
gtk_widget_show_all(window);
gtk_main(); //相当于循环
return 0;
}