我正在制作具有拍照功能的增强现实应用程序。它使用我的自定义功能来创建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];

有人对此有经验或为日本制作过类似的应用程序吗?我真的很茫然。谢谢。

最佳答案

MPVolumeView会在可见时阻止 float 框,即使用户实际上看不到它也是如此。

一些示例代码...

// 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我不知道与旧版本的兼容性。

关于iphone - 更改iPhone应用程序的音量,而不会出现“更改音量”框(日本的应用程序),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7764195/

10-16 10:12