更改iPhone应用程序音量而不显示音量变化框(日本应用程序)

时间:2011-10-14 07:08:38

标签: iphone audio openal

我正在制作具有拍照功能的增强现实应用程序。它使用我的自定义函数来创建UIImage来保存屏幕。根据日本的法律规定,相机必须具有快门噪音,这就是iPhone相机始终播放它的原因。到目前为止,我已经找到了一种方法来播放声音,即使iPhone静音但它仍然依赖于用户设置的音量。所以我找到了一种使用MPMusicPlayerController来控制应用程序量的方法。这样可行,但是当音量改变时,会弹出一个框,表示音量已更改。

这是我的代码,即使在静音时播放声音:

    AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                         sizeof(sessionCategory),&sessionCategory);

我使用Finch库播放声音(openAL的光包装),然后使用MPMusicPlayerController在播放前调整音量。

appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[appMusicPlayer setVolume:0.5f];

任何人都有这方面的经验,或者已经为日本制作了这样的应用程序?我真的很茫然。感谢。

1 个答案:

答案 0 :(得分:1)

MPVolumeView虽然可见,但会阻止浮动框,即使用户实际上看不到它。

一些示例代码......

// create/synthesize ivars for "MPVolumeView" and "UIView" (both are necessary)
// I called them "mpVolumeView" and "mpVolumeViewParentView" respectively

// the UIView containing the MPVolumeView can have a frame of (0,0,1,1)
// this way, the user never sees the slider, but it still works normally

- (void)viewDidLoad {
    ...
    // with this, only the slider is visible
    mpVolumeViewParentView.backgroundColor = [UIColor clearColor];

    // initialize the volume slider (link the parent view in IB, or init here)
    mpVolumeView = [[MPVolumeView alloc] initWithFrame:
                                                mpVolumeViewParentView.bounds];

    // since it's a programmatic init, the subview must be added like so
    [mpVolumeViewParentView addSubview:mpVolumeView];

    // allows the floating box to appear without destroying mpVolumeView
    mpVolumeView.hidden = YES; // or [mpVolume setHidden:YES]; if you prefer
    ...
}

在改变音量以强制相机发出声音之前......

mpVolumeView.hidden = NO; // view visible, box doesn't appear

听完声音之后,看起来好像你搞砸了什么......

mpVolumeView.hidden = YES; // view hidden, box appears

可能需要一些调整来获得你想要的东西,但它应该是一个很好的起点。

此代码适用于iOS 5.1
我不知道与旧版本的兼容性是什么。