我在開發大多數無聲應用程序時所看到的主要問題是關於移入/移出前景並與其他音頻很好地播放。爲了更好地瞭解我通常會做的事情,我將給出最近項目的一些片段。 (這些是故意不完整的,只是爲了說明一個觀點。)爲了論證,我們假設我們有一個負責維護音頻會話的AudioManager類。這個類是我們用來實例化我們的自定義音頻播放器的。在這樣的課堂上,我們提出:
@interface MyAudioManager()
@property (nonatomic, retain) BOOL alwaysMaintainAudioSession;
@property (nonatomic, retain) MyCustomAudioPlayer *player;
@end
@implementation MyAudioManager
@synthesize alwaysMaintainAudioSession;
@synthesize player;
-(void) applicationWillEnterForeground
{
isInBackground = NO;
if (NO==[self anyAudioIsPlaying] && self.alwaysMaintainAudioSession) {
[self activateAudioSession];
}
}
-(void) activateAudioSession
{
AudioSessionSetActive(TRUE);
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, AudioPropertyListener, self);
}
-(BOOL) anyAudioIsPlaying
{
return [self otherAudioIsPlaying] || [player isPlaying];
}
-(BOOL) otherAudioIsPlaying
{
UInt32 yesNo;
UInt32 propertySize = sizeof(yesNo);
OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &yesNo);
if (kAudioSessionUnsupportedPropertyError == status) {
return MPMusicPlaybackStatePlaying == [theiPodMusicPlayer playbackState];
} else {
return MPMusicPlaybackStatePlaying == [theiPodMusicPlayer playbackState] || yesNo;
}
}
該管理器允許您設置始終保持聲音鏈接到應用程序的音量控制,這意味着我們始終確保一個屬性或者是會話活動或一些其他應用程序正在播放音頻。在任何其他情況下,音量控制恢復爲控制鈴聲。所以當進入前臺時,我們必須檢查是否有其他音頻播放並有條件地激活音頻會話。我們還需要在移至後臺時關閉會話以恢復鈴聲音量控制。
-(void) applicationDidEnterBackground
{
if (NO==[self anyAudioIsPlaying]) {
AudioSessionSetActive(NO);
}
}
在我的解決方案我有一堆其他的代碼來處理之類的東西智能響應時的藍牙音頻設備連接,工廠方法用於創建自定義播放器,自定義音頻壓縮等等。然而,主要想法是處理播放音頻的其他應用程序,同時嘗試在前臺保持音量控制與應用程序音量相關聯。
你看過iOS文檔嗎?這種行爲在人機交互準則中有明確的定義,甚至還包含了實現示例。 – 2012-03-20 18:52:00
即使是在話題上,您的問題似乎對於問答格式有點過於寬泛。 – hotpaw2 2012-03-21 00:01:13