2015-09-10 157 views
4

我已將AVFoundationAudioToolbox框架添加到我的項目中。在我想要播放系統聲音的課程中,我和#include <AudioToolbox/AudioToolbox.h>AudioServicesPlaySystemSound(1007);。我在運行iOS 8的設備上進行測試,聲音已打開且音量足夠高,但我在運行應用程序時聽不到任何系統聲音,並且調用了AudioServicesPlaySystemSound(1007); ...我可能會丟失什麼?AudioServicesPlaySystemSound不能在iOS 8設備中播放任何聲音

+2

你是否檢查了無聲開關? –

+0

@JasonNam是的,聲音在... – AppsDev

+0

其他音頻聽起來不錯嗎? –

回答

2

這將播放系統聲音。

但記住系統聲音不會播放更長的聲音。

NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"engine" ofType:@"mp3"]; 
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound); 
AudioServicesPlaySystemSound(_engineSound); 
+0

你可以給出一些建議,如何發揮更長的聲音或重複音?我必須播放重複的聲音,直到通話未被選中。 – Aditya

3

根據文檔:

此功能(AudioServicesPlaySystemSound())將在 被廢棄以後的版本中。改爲使用AudioServicesPlaySystemSoundWithCompletion 。

使用下面的代碼片段播放聲音:

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; //filename can include extension e.g. @"bang.wav" 
if (fileURL) 
{ 
    SystemSoundID theSoundID; 
    OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID); 
    if (error == kAudioServicesNoError) 
    { 
     AudioServicesPlaySystemSoundWithCompletion(theSoundID, ^{ 
      AudioServicesDisposeSystemSoundID(theSoundID); 
     }); 
    } 
} 

此外,完成塊確保其設置的前播放聲音完成。

如果這不能解決問題,也許你的問題不是代碼相關的,而是相關的設置(靜音/模擬器的設備聲音從MAC系統偏好靜音,確保「播放用戶界面聲音效果」被選中)

+0

你能鏈接到文檔嗎?我找不到你的報價。 – Suragch

+1

這是來自SDK頭文件。嘗試使用已棄用的方法,XCode會顯示警告消息。 –

6

隨着iOS10這樣播放音頻不起作用

SystemSoundID audioID; 

AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &mySSID); 
AudioServicesPlaySystemSound(audioID); 

使用這個代替:

AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioID); 

AudioServicesPlaySystemSoundWithCompletion(audioID, ^{ 
    AudioServicesDisposeSystemSoundID(audioID); 
}); 
0

我剛剛測試了運行iOS 8的iPad和iPhone上的代碼,並且它正在使用真實設備。

對於一些非常奇怪的原因,它不適用於任何設備的iOS 8模擬器,即使它適用於iOS 7和7.1模擬器。

否則下面的代碼在所有實際設備中都可以正常工作。

NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"engine" ofType:@"mp3"]; 
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_engineSound); 
AudioServicesPlaySystemSound(_engineSound); 
0

的迅速3.x和Xcode中8:

var theSoundID : SystemSoundID = 0 
let bundleURL = Bundle.main.bundleURL 
let url = bundleURL.appendingPathComponent("Invitation.aiff") 

let urlRef = url as CFURL 

let err = AudioServicesCreateSystemSoundID(urlRef, &theSoundID) 
if err == kAudioServicesNoError{ 
    AudioServicesPlaySystemSoundWithCompletion(theSoundID, { 
     AudioServicesDisposeSystemSoundID(theSoundID) 
    }) 
} 
相關問題