linux定时播放音乐,linux下自动化任务的例子——定时播放音乐

cron是linux/UNIX/Mac OS等系统下的自动化程序,这里仅举一个实现定时广播的例子。

linux下自动化任务的例子

在linux系统中实现作业的自动化是非常便利的。

比较常用的是Cron服务的crontab这个命令。

⼀一个具体的任务列表

这个是学校室外广播的自动程序,只用了⼀一台被废弃的塞羊800的学生机,安装的系统是红旗linux,其他的linux、FreeBSD、unix或者是MacOS都可以。

将Cron设为自动运行。

chkconfig –level 35 crond on

进入系统终端

键入:

crontab -u caizhongyi -e

(crontab为命令,“-u cai”是指定执行作业的系统用户为cai,“-e”使用crontab自己的vi编辑器,防止出现编码或系统环境问题造成的错误)然后在编辑器(vi编辑器)中输入如下命令:

0 7 * * 1 mpg123 -q /home/cai/guangbo/001.mp3

21 8 * * 1 mpg123 -q /home/cai/guangbo/11.mp3

21 8 * * 2 mpg123 -q /home/cai/guangbo/21.mp3

21 8 * * 3 mpg123 -q /home/cai/guangbo/31.mp3

21 8 * * 4 mpg123 -q /home/cai/guangbo/41.mp3

21 8 * * 5 mpg123 -q /home/cai/guangbo/51.mp3

16 9 * * 2 mpg123 -q /home/cai/guangbo/212.mp3

16 9 * * 3 mpg123 -q /home/cai/guangbo/312.mp3

16 9 * * 4 mpg123 -q /home/cai/guangbo/412.mp3

16 9 * * 5 mpg123 -q /home/cai/guangbo/512.mp3

25 10 * * 1 mpg123 -q /home/cai/guangbo/12.mp3

25 10 * * 2 mpg123 -q /home/cai/guangbo/22.mp3

25 10 * * 3 mpg123 -q /home/cai/guangbo/32.mp3

25 10 * * 4 mpg123 -q /home/cai/guangbo/42.mp3

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux C 音乐播放可以通过调用 ALSA API 来实现。 ALSA (Advanced Linux Sound Architecture) 是一个为 Linux 提供音频支持的框架,它提供了一组开发者接口,使应用程序可以在 Linux 上播放音频。 在 C 语言中,可以通过 ALSA API 来实现音频播放。具体来说,你需要创建一个 PCM (脉冲编码调制) 设备,然后使用 PCM 接口来读取音频数据并将其写入 PCM 设备。要播放音频,你需要打开音频文件、解码音频数据并将其写入 PCM 设备。你还需要设置 PCM 设备的属性,如采样率、声道数、采样格式等。 下面是一个简单的示例代码,它使用 ALSA API 在 Linux 上播放音频: ``` #include <stdio.h> #include <stdlib.h> #include <alsa/asoundlib.h> #define SAMPLE_RATE 44100 #define NUM_CHANNELS 2 #define SAMPLE_SIZE 16 #define BUFFER_SIZE 4096 int main(int argc, char *argv[]) { char *filename = argv; FILE *file = fopen(filename, "r"); if (!file) { printf("Failed to open file: %s\n", filename); return 1; } // Initialize ALSA snd_pcm_t *pcm_handle; snd_pcm_hw_params_t *hw_params; int err; err = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, 0); if (err < 0) { printf("Failed to open PCM device: %s\n", snd_strerror(err)); return 1; } err = snd_pcm_hw_params_malloc(&hw_params); if (err < 0) { printf("Failed to allocate HW params: %s\n", snd_strerror(err)); return 1; } err = snd_pcm_hw_params_any(pcm_handle, hw_params); if (err < 0) { printf("Failed to initialize HW params: %s\n", snd_strerror(err)); return 1; } err = snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); if (err < 0) { printf("Failed to set access type: %s\n", snd_strerror(err)); return 1; } err = snd_pcm_hw_params_set_format(pcm_handle, hw_params, SND_PCM_FORMAT_S16_LE); if (err < 0) { printf("Failed to set sample format: %s\n", snd_strerror(err)); return 1; } unsigned int sample_rate = SAMPLE_RATE; err = snd_pcm_hw_params_set_rate_near(pcm_handle, hw_params, &sample_rate, 0); if (err < 0) { printf("Failed to set sample rate: %s\n", snd_strerror(err)); return 1; } err = snd_pcm_hw_params_set_channels(pcm_handle, hw_params, NUM_CHANNELS); if (err < 0) { printf("Failed to set channel count: %s\n", snd_strerror(err)); return 1; } err = snd_pcm_hw_params(pcm_handle, hw_params); if (err < 0) { printf("Failed to set HW params: %s\n", snd_strerror(err)); return 1; } snd_pcm_hw_params_free(hw_params); // Read and play audio data char buffer[BUFFER_SIZE]; while (!feof(file)) { size_t num_bytes = fread(buffer, 1, BUFFER_SIZE, file); if (num_bytes == 0) { break; } int num_frames = num_bytes / (NUM_CHANNELS * (SAMPLE_SIZE / 8)); int num_frames_written = snd_pcm_writei(pcm_handle, buffer, num_frames); if (num_frames_written < num_frames) { printf("Write error: %s\n", snd_strerror(num_frames_written)); break; } } // Close the PCM device and file snd_pcm_drain(pcm_handle); snd_pcm_close(pcm_handle); fclose(file); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值