imx6ul之OpenWRT中使用mpd作爲音頻Server及其使用

說明

MPD可以播放音樂與streaming Radio,可以用於娛樂用。其官網的說法爲:

[plain] view plain copy
  1. Music Player Daemon (MPD) is a flexible, powerful, server-side application for playing music.   
  2. Through plugins and libraries it can play a variety of   
  3. sound files while being controlled by its network protocol.  
即MPD是CS架構的Daemon端。

接下來我們將在iMX6UL上面使用MPD, 並在手機與Ubuntu等上面使用其Client來控制與播放音樂。

ALSA驗證聲卡發聲

要完成音樂的播放,首先iMX6UL需要可以播放聲音。 iMX6UL使用的Codec是WM8960, 這個和TQE9使用的是一樣的。

在播放之前,我們先查看聲卡是哪個:

[plain] view plain copy
  1. root@(none):/# cat /proc/asound/card1/id   
  2. wm8960audio  
  3.   
  4. root@(none):/# aplay -L  
  5. null  
  6.     Discard all samples (playback) or generate zero samples (capture)  
  7. default:CARD=wm8960audio  
  8.     wm8960-audio,   
  9.     Default Audio Device  
  10. sysdefault:CARD=wm8960audio  
  11.     wm8960-audio,   
  12.     Default Audio Device  

首先需要確定ALSA識別了哪些聲卡,然後再確定聲卡的設備Index, 例如這裏面WM8960不是Card0,所以如果直接默認使用aplay來播放,那麼就會出錯。

當然啓動的時候也有:

[plain] view plain copy
  1. [    3.546365] ALSA device list:  
  2. [    3.549498]   #0: Virtual MIDI Card 1  
  3. [    3.553209]   #1: wm8960-audio  

確定設備Index後,我們需要一個WAV文件來播放。

直接使用MP3來convert一個:



然後我們使用wavinfo查看一下wav文件的屬性:

[plain] view plain copy
  1. Header info for: /home/hexiongjun/Music/CloudMusic/XXX.wav  
  2. WaveHeader Size:    12  
  3. ChunkHeader Size:   8  
  4. FormatChunk Size:   16  
  5. RIFF ID:    RIFF  
  6. Total Size: 56536450  
  7. Wave ID:    WAVE  
  8. Chunk ID:   fmt   
  9. Chunk Size: 16  
  10. Compression format is of type: 1  
  11. Channels:   2  
  12. Sample Rate:    44100  
  13. Bytes / Sec:    176400  
  14. wBlockAlign:    4  
  15. Bits Per Sample Point:  16  
  16. wavDataPtr: 44  
  17. wavDataSize: 56535552  
然後將其放到設備中去,同時也放入MP3文件,作爲後面驗證用。

alsamixer配置聲音

如果直接播放我們會發現沒有聲音,這個是因爲沒有將volume開啓, 然後使用下面命令完成:

[plain] view plain copy
  1. amixer cset name='PCM Playback Volume' 100%,100%  
  2. amixer cset name='HP DAC Playback Volume'  100%,100%     

播放測試

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

然後可以聽到聲音即可。

配置mpd

mpd需要一個配置文件,記錄文件都放在where,需要配置聲卡output。

因此我們直接更改即可:

vim /etc/mpd.conf

找到存放音樂的目錄

[plain] view plain copy
  1. music_directory         "/var/lib/mpd/music"  

然後找到audio_output section:

[plain] view plain copy
  1. audio_output {  
  2.         type            "alsa"  
  3.         name            "My ALSA Device"  
  4.         device          "hw:1,0"        # optional  
  5. ##      format          "44100:16:2"    # optional  
  6. ##      mixer_type      "hardware"      # optional  
  7. ##      mixer_device    "default"       # optional  
  8. ##      mixer_control   "PCM"           # optional  
  9. ##      mixer_index     "0"             # optional  
  10. }  
然後保存, 並將MP3放入到設置的目錄中。

運行

查看幫助

[plain] view plain copy
  1. root@OpenWrt:~# mpd --help  
  2. Usage:  
  3.   mpd [OPTION...] [path/to/mpd.conf]  
  4.   
  5. Music Player Daemon - a daemon for playing music.  
  6.   
  7. Options:  
  8.   -h, --help        show help options  
  9.   --kill            kill the currently running mpd session  
  10.   --no-config       don't read from config  
  11.   --no-daemon       don't detach from console  
  12.   --stderr          print messages to stderr  
  13.   -v, --verbose     verbose logging  
  14.   -V, --version     print version number  

嘗試運行

[plain] view plain copy
  1. root@OpenWrt:~# mpd --no-daemon -v  
  2. config_file: loading file /etc/mpd.conf  
  3. root@OpenWrt:~# echo $?  
  4. 1  

我們發現起直接退出了, 而且返回了一個錯誤值:

[plain] view plain copy
  1. EPERM 1 Operation not permitted  
調查發現是mpd.conf裏面我們的user改錯了。

看看其進程是否存在

[plain] view plain copy
  1. root@OpenWrt:~# ps | grep mpd  
  2.  1376 root     16332 S    mpd -v  
  3.  1382 root      1160 S    grep mpd  
  4. root@OpenWrt:~#   

如果開啓了debug或者verbose模式,那麼可以看到:

[plain] view plain copy
  1. root@(none):/# mpd -v  
  2. config_file: loading file /etc/mpd.conf  
  3. 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)  
  4. Jan 31 12:31 : path: SetFSCharset: fs charset is: UTF-8  
  5. Jan 31 12:31 : libsamplerate: libsamplerate converter 'Fastest Sinc Interpolator'  
  6. Jan 31 12:31 : vorbis: Xiph.Org libVorbis 1.3.5  
  7. Jan 31 12:31 : opus: libopus 1.1.2  
  8. Jan 31 12:31 : sndfile: libsndfile-1.0.26  
  9. Jan 31 12:31 : db: reading DB  
  10. Jan 31 12:31 : curl: version 7.47.1  
  11. Jan 31 12:31 : curl: with GnuTLS/3.4.9  
這裏面說明了使用的端口(CS架構),然後使用的charset等信息。

確定端口開啓

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

這樣我們就可以使用client來查看確定了。

mpd client的使用

板子本身的 Linux command line client

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

[plain] view plain copy
  1. root@OpenWrt:~# mpc listall  
  2. Lady Antebellum - Need You Now.mp3  
  3. root@OpenWrt:~#  
  4.   
  5. root@OpenWrt:~# ls music/  
  6. Lady Antebellum - Need You Now.mp3  
  7. root@OpenWrt:~#   

然後就是要將歌曲添加到playlist中, 這個網上絕大部分查到的都是使用下面這個命令:

[plain] view plain copy
  1. 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

發佈了82 篇原創文章 · 獲贊 34 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章