您的位置:首页 > 运维架构

OpenWRT中使用mpd作为音频Server及其使用

2017-02-05 22:14 585 查看

说明

MPD可以播放音乐与streaming Radio,可以用于娱乐用。其官网的说法为:

Music Player Daemon (MPD) is a flexible, powerful, server-side application for playing music.
Through plugins and libraries it can play a variety of
sound files while being controlled by its network protocol.即MPD是CS架构的Daemon端。
接下来我们将在iMX6UL上面使用MPD, 并在手机与Ubuntu等上面使用其Client来控制与播放音乐。

ALSA验证声卡发声

要完成音乐的播放,首先iMX6UL需要可以播放声音。 iMX6UL使用的Codec是WM8960, 这个和TQE9使用的是一样的。

在播放之前,我们先查看声卡是哪个:

root@(none):/# cat /proc/asound/card1/id
wm8960audio

root@(none):/# aplay -L
null
Discard all samples (playback) or generate zero samples (capture)
default:CARD=wm8960audio
wm8960-audio,
Default Audio Device
sysdefault:CARD=wm8960audio
wm8960-audio,
Default Audio Device

首先需要确定ALSA识别了哪些声卡,然后再确定声卡的设备Index, 例如这里面WM8960不是Card0,所以如果直接默认使用aplay来播放,那么就会出错。
当然启动的时候也有:

[ 3.546365] ALSA device list:
[ 3.549498] #0: Virtual MIDI Card 1
[ 3.553209] #1: wm8960-audio

确定设备Index后,我们需要一个WAV文件来播放。
直接使用MP3来convert一个:



然后我们使用wavinfo查看一下wav文件的属性:

Header info for: /home/hexiongjun/Music/CloudMusic/XXX.wav
WaveHeader Size: 12
ChunkHeader Size: 8
FormatChunk Size: 16
RIFF ID: RIFF
Total Size: 56536450
Wave ID: WAVE
Chunk ID: fmt
Chunk Size: 16
Compression format is of type: 1
Channels: 2
Sample Rate: 44100
Bytes / Sec: 176400
wBlockAlign: 4
Bits Per Sample Point: 16
wavDataPtr: 44
wavDataSize: 56535552
然后将其放到设备中去,同时也放入MP3文件,作为后面验证用。

alsamixer配置声音

如果直接播放我们会发现没有声音,这个是因为没有将volume开启, 然后使用下面命令完成:

amixer cset name='PCM Playback Volume' 100%,100%
amixer cset name='HP DAC Playback Volume' 100%,100%

播放测试

root@(none):/# aplay -D hw:1,0 Two\ Steps\ From\ Hell\ -\ Victory.wav
Playing WAVE 'Two Steps From Hell[11482.358980] data->clk_frequency = 12288000
- Victory.wav' : Signed 16 bit L[11482.364517] sample_rate = 44100
ittle Endian, Rate 44100 Hz, Stereo

然后可以听到声音即可。

配置mpd

mpd需要一个配置文件,记录文件都放在where,需要配置声卡output。

因此我们直接更改即可:

vim /etc/mpd.conf

找到存放音乐的目录

music_directory "/var/lib/mpd/music"

然后找到audio_output section:

audio_output {
type "alsa"
name "My ALSA Device"
device "hw:1,0" # optional
## format "44100:16:2" # optional
## mixer_type "hardware" # optional
## mixer_device "default" # optional
## mixer_control "PCM" # optional
## mixer_index "0" # optional
}
然后保存, 并将MP3放入到设置的目录中。

运行

查看帮助

root@OpenWrt:~# mpd --help
Usage:
mpd [OPTION...] [path/to/mpd.conf]

Music Player Daemon - a daemon for playing music.

Options:
-h, --help show help options
--kill kill the currently running mpd session
--no-config don't read from config
--no-daemon don't detach from console
--stderr print messages to stderr
-v, --verbose verbose logging
-V, --version print version number

尝试运行
root@OpenWrt:~# mpd --no-daemon -v
config_file: loading file /etc/mpd.conf
root@OpenWrt:~# echo $?
1

我们发现起直接退出了, 而且返回了一个错误值:
EPERM 1 Operation not permitted调查发现是mpd.conf里面我们的user改错了。
看看其进程是否存在

root@OpenWrt:~# ps | grep mpd
1376 root 16332 S mpd -v
1382 root 1160 S grep mpd
root@OpenWrt:~#

如果开启了debug或者verbose模式,那么可以看到:
root@(none):/# mpd -v
config_file: loading file /etc/mpd.conf
Jan 31 12:31 : server_socket: bind to '0.0.0.0:6600' failed: Address already in use (continuing anyway, because binding to '[::]:6600' succeeded)
Jan 31 12:31 : path: SetFSCharset: fs charset is: UTF-8
Jan 31 12:31 : libsamplerate: libsamplerate converter 'Fastest Sinc Interpolator'
Jan 31 12:31 : vorbis: Xiph.Org libVorbis 1.3.5
Jan 31 12:31 : opus: libopus 1.1.2
Jan 31 12:31 : sndfile: libsndfile-1.0.26
Jan 31 12:31 : db: reading DB
Jan 31 12:31 : curl: version 7.47.1
Jan 31 12:31 : curl: with GnuTLS/3.4.9这里面说明了使用的端口(CS架构),然后使用的charset等信息。

确定端口开启

root@OpenWrt:~# netstat -apn | grep mpd
tcp 0 0 :::6600 :::* LISTEN 1376/mpd
unix 2 [ ] DGRAM 18753 1376/mpd

这样我们就可以使用client来查看确定了。

mpd client的使用

板子本身的 Linux command line client

有mpc命令可以用, 我们可以查看都有那些歌曲(mpc listall):

root@OpenWrt:~# mpc listall
Lady Antebellum - Need You Now.mp3
root@OpenWrt:~#

root@OpenWrt:~# ls music/
Lady Antebellum - Need You Now.mp3
root@OpenWrt:~#

然后就是要将歌曲添加到playlist中, 这个网上绝大部分查到的都是使用下面这个命令:
mpd --create-db

但是如果你真的拿来用,会发现mpd根本就不再具备这个功能了。因此很多人就会卡死在这里了。
正确的做法直接使用directory功能。

我们这里使用mpc add是可以用的。

Ubuntu上面的Client

对于Ubuntu可以到Software中下载Client:



然后安装一个,这里安装的是QMPDClient, 打开,然后设置:



添加完成后, 连接



然后就可以看到Directory的文件, 然后双击即可添加到PlayList中了:



接下来就可以播放了。

Android端Client

Android手机端,使用MPDroid, 可以到github的release页面下载

安装后设置端口与IP地址,然后就可以用了:



参考

MPD Arch Wiki



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐