測試環境:
Ubuntu 14.04
MonoDevelop
CodeBlocks
1、建立一個共享庫(shared library)
這裡用到了Linux下的音頻播放庫,alsa-lib。 alsa是linux下的一個開源項目,它的全名是Advanced Linux Sound Architecture。它的安裝命令如下:
sudo apt-get install libasound2-dev
使用 Coceblocks 建立一個 shared library 項目,命名為libTest2,編程語言選擇C。在main中加入下代碼:
#include <alsa/asoundlib.h>
#include<stdio.h>
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
int PcmOpen()
{
if ( snd_pcm_open(&handle, "hw:0,0", SND_PCM_STREAM_PLAYBACK, 0) < 0 )
{
printf("pcm open error");
return 0;
}
if (snd_pcm_set_params(handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 8000, 1, 500000) < 0) //0.5sec 500000
{
printf("pcm set error");
return 0;
}
return 1;
}
void Play(unsigned char* buffer, int length)
{
frames = snd_pcm_writei(handle, buffer, length);
if(frames < 0)
{
frames = snd_pcm_recover(handle, frames, 0);
}
}
int PcmClose()
{
snd_pcm_close(handle);
return 1;
}
在編譯的時候,記得鏈接alsa-lib庫。具體方法是在codeblocks的編譯對話框中,找到linker settings選項,在Other linker options中輸入:-lasound。
如圖所示:
當然,也可以手工編譯。cd 進main.c所在的目錄,執行以下命令:
gcc -o main.o -c main.c
gcc -o libTest1.so -shared main.o -lasound
2、在mono中調用共享庫
與.net調用動態庫一樣,使用DllImport特性來調用。關於mono調用共享庫,mono官方有一篇文章介紹得很詳細:《Interop with Native Libraries》。
使用monoDevolop建立一個控制台項目,並將上一步中生成的libTest1.so文件拷貝到/bin/Debug下。
在Program.cs文件中輸入以下代碼:
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
namespace helloworld
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("the app is started ");
PlayPCM();
Thread thread = new Thread(new ThreadStart(PlayPCM));
thread.Start();
while (true)
{
if (Console.ReadLine() == "quit")
{
thread.Abort();
Console.WriteLine("the app is stopped ");
return;
}
}
}
/// <summary>
/// Plaies the PC.
/// </summary>
static void PlayPCM()
{
using (FileStream fs = File.OpenRead("Ireland.pcm"))
{
byte[] data = new byte[4000];
PcmOpen();
while (true)
{
int readcount = fs.Read(data, 0, data.Length);
if (readcount > 0)
{
Play(data, data.Length);
}
else
{
break;
}
}
PcmClose();
}
}
[DllImport("libTest1.so")]
static extern int PcmOpen();
[DllImport("libTest1.so")]
static extern int Play(byte[] buffer, int length);
[DllImport("libTest1.so")]
static extern int PcmClose();
}
}
為了便於測試,附件中包含了一個聲音文件,可以直接使用這個聲音文件。
3、有關PCM文件的一些簡要說明
附件中的Ireland.pcm,采用的是PCMU編碼,采樣率為8000Hz,采樣深度為1字節,聲道數為1。這些對應於snd_pcm_set_params函數,這個函數用於以較簡單的方式來設定pcm的播放參數。要正確地播放聲音文件,必須使PCM的參數與聲音文件的參數一致。
更多有關alsa的信息,請查看官方網站。http://www.alsa-project.org/main/index.php/Main_Page
附件下載:
------------------------------------------分割線------------------------------------------
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2015年資料/3月/7日/Linux下mono播放PCM音頻/
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割線------------------------------------------