2012-12-07 44 views
3

在界面生成器中,UIButton的屬性位於Accessibility的「播放聲音」下。在界面生成器中使用UIButton的「Plays Sound」屬性

任何人都可以解釋這是什麼。其實我正在製作一個應用程序,每按一下按鈕就會播放聲音,我可以從設置屏幕中禁用聲音。 UIButton的這個屬性可以幫助我嗎?

感謝

+0

如果你只需要點擊按鍵上的鍵盤聲音,你可以使用'[[UIDevice currentDevice] playInputClick];'。 – iDev

+0

是的,我只需要播放一個點擊聲音。我在按鈕動作中添加了這一行,並在模擬器中進行了測試,但沒有播放聲音。 –

回答

8

您可以使用[[UIDevice currentDevice] playInputClick];彈奏鍵盤輸入點擊的聲音是在可用。勾選此apple documentation for more details.

你需要做的這個以下,

  1. 採用UIInputViewAudioFeedback協議在你輸入視圖類。
  2. 執行enableInputClicksWhenVisible委託方法返回YES。

UIView來實現這個目的,

@interface MyView : UIView <UIInputViewAudioFeedback> 

然後實現enableInputClicksWhenVisible方法

- (BOOL)enableInputClicksWhenVisible 
{ 
    return YES; 
} 

如果您正在使用此面臨的問題,你可以check this.

1

,嘿,你想在按鈕單擊事件播放的聲音,然後用波紋管代碼也..

-(IBAction)playSound:(id)sender 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"adriantnt_release_click" ofType:@"mp3"]; /// set .mp3 name which you have in project 
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate=self; 
    [theAudio play]; 
} 

,並使用像波紋管..

-(IBAction)yourButton_Clicked:(id)sender 
{ 
    [self performSelector:@selector(playSound:) withObject:sender]; 
    //your code write here 
} 

注:添加AudioToolbox.framework框架並且還導入 #import<AVFoundation/AVAudioPlayer.h> in .h file and als o增加代表AVAudioPlayerDelegate.h文件

我希望這會幫助你...

+0

乾草目前我正在使用這種方法,它工作正常。但我需要縮短我的代碼。我可以使用UIButton的Plays Sound屬性播放聲音嗎? –

+0

是的,你可以直接使用UIDevice類的屬性 –

2

回答你原來的您是否可以使用Plays Sound'屬性'的問題(它是p (實際上並非屬性)使您的按鈕播放聲音是:否。播放聲音是描述使用VoiceOver的對象(在此情況下爲按鈕)的可訪問性的「特性」(即「特性」)。最有可能的人是盲人)。您可以閱讀文檔here