2012-03-15 71 views
0

我想用delphi做一些語音識別,發現這個簡單的項目效果很好,但我不能使用它,因爲我無法導入SAPI 5.1。
在「導入類型庫」窗口中,我可以找到SAPI 5.4,但遺憾的是我無法使用它。現在我想知道是否有辦法導入SAPI 5.1,以便我可以使用下面的示例並根據需要進行擴展。德爾福不能導入SAPI 5.1

unit Unit3; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, SpeechLib_TLB, StdCtrls; 

const SP_GETWHOLEPHRASE = -1; 

type 
    TForm3 = class(TForm) 
    Memo1: TMemo; 
    procedure FormCreate(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    private 
    { Private-Deklarationen } 
    public 
    SpSharedRecoContext1: TSpSharedRecoContext; 
    MyGrammar : ISpeechRecoGrammar; 
    procedure SpSharedRecoContext1Recognition(ASender: TObject; 
               StreamNumber: Integer; 
               StreamPosition: OleVariant; 
               RecognitionType: SpeechRecognitionType; 
               const Result: ISpeechRecoResult); 
    procedure SpSharedRecoContext1Hypothesis(ASender: TObject; 
              StreamNumber: Integer; 
              StreamPosition: OleVariant; 
              const Result: ISpeechRecoResult); 
    { Public-Deklarationen } 
    end; 

var 
    Form3: TForm3; 

implementation 

{$R *.dfm} 

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
SpSharedRecoContext1.Free; 
end; 

procedure TForm3.FormCreate(Sender: TObject); 
begin 
    SpSharedRecoContext1 := TSpSharedRecoContext.Create(self); 
    SpSharedRecoContext1.OnRecognition := SpSharedRecoContext1Recognition; 
    SpSharedRecoContext1.OnHypothesis := SpSharedRecoContext1Hypothesis; 
    MyGrammar := SpSharedRecoContext1.CreateGrammar(0); 
    MyGrammar.DictationSetState(SGDSActive); 
end; 

procedure TForm3.SpSharedRecoContext1Recognition(ASender: TObject; 
               StreamNumber: Integer; 
               StreamPosition: OleVariant; 
               RecognitionType: SpeechRecognitionType; 
               const Result: ISpeechRecoResult); 
begin 
Caption := ''; 
Memo1.Lines.Add(Result.PhraseInfo.GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,true)); 
end; 

procedure TForm3.SpSharedRecoContext1Hypothesis(ASender: TObject; 
               StreamNumber: Integer; 
               StreamPosition: OleVariant; 
               const Result: ISpeechRecoResult); 
begin 
Caption := 'I am listening...'; 
end; 


end. 

任何幫助表示讚賞,

THX timo314

編輯: 我有德爾福6個人版
SAPI 5.1是不是在 「導入類型庫」 窗口中列出
編譯時我在第46行和第47行發現錯誤,說「不兼容的類型參數列表不同」

我注意到SpeechLib_TL示例中附帶的B.pas不同於在「導入」目錄中創建的導入。
更改SpSharedRecoContext1Recognition和SpSharedRecoContext1假設的參數列表後,應用程序將運行但不調用該過程。就好像程序沒有注意到有人在說話。

+2

歡迎來到StackOverflow。 Delphi的哪個版本? 「無法導入」是什麼意思?你的意思是沒有安裝你的Windows版本,但你有SAPI 5.4?如果是這樣,5.4與早期版本向後兼容;你有什麼問題?你會收到錯誤消息嗎?請編輯您的問題,並記住這樣做時,我們只有您提供給我們的信息來嘗試並幫助您解決問題。如果你不告訴我們,我們無法知道 - 我們無法看到你的屏幕或代碼,或從這裏讀出你的想法。 :) – 2012-03-15 22:42:39

回答

1

很難告訴你提供了什麼,但對我來說RecognitionTypeTOleEnum,我會用的 Memo1.Lines.Add(Result.PhraseInfo.GetText(0,SP_GETWHOLEPHRASE,true));
代替
Memo1.Lines.Add(Result.PhraseInfo.GetText(SP_GETWHOLEPHRASE,SP_GETWHOLEPHRASE,true));
作爲第一個參數指定要檢索的第一個元素。

我也要設置SpSharedRecoContext1.EventInterests := SREAllEvents;

您可能會發現在my blog一些有用的例子和信息...

PS:我曾與SAPI 5.4沒有問題,但從來沒有嘗試過的D6 ....

+0

感謝您的回覆,我會看看您的網站。關於這個WepPage [鏈接(] http://www.blong.com/Conferences/DCon2002/Speech/SAPI51/SAPI51.htm#GramNotify)在Delphi6和更早的版本中似乎有些不同。但是,您的解決方案和對上述網站添加(Result.PhraseInfo.GetText)的更改都不能解決我的問題。 – timo314 2012-03-16 00:00:27

+0

剛剛發現:
DELPHI 6用戶重要信息 Delphi 6中的類型庫導入有一個錯誤,請參閱文章「Delphi 6 - 導入的自動化事件錯誤」。該示例仍然可以在Delphi 6中通過libary類型創建的單元工作,但僅僅是因爲沒有使用該組件的事件。如果您想使用任何SPVoice事件,您需要閱讀文章「Delphi 6 - 導入的自動化事件錯誤」。
看到[鏈接](http://delphicikk.atw.hu/listaz.php?id=2380&oldal=51)希望我會在那裏找到解決方案,如果是的話,我會在這裏發佈。 – timo314 2012-03-16 00:25:08

+0

[在此網站上](http://delphi.cjcsoft.net/viewthread.php?tid = 47605)某人對這個問題表現出了一些駭人的解決方法,但是用delphi 5創建的speechLib_TLB取代了[來自這裏]的示例項目(http://www.blong.com/Conferences/DCon2002/Speech/ SAPI51/SAPI51.htm#DSR)工作沒有任何錯誤,所以我會在該示例中構建我的應用程序。感謝您的時間和幫助,timo314 – timo314 2012-03-16 01:06:50