更改 iPhone 应用程序音量而不出现音量更改框(适用于日本的应用程序)

发布于 2024-12-09 13:03:39 字数 817 浏览 0 评论 0原文

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

有人有这方面的经验或者为日本制作过这样的应用程序吗?我真是不知所措了。谢谢。

I am making an Augmented Reality application that has picture taking functionality. It uses a custom function of mine to create a UIImage to save the screen. By law in Japan, cameras must have a shutter noise, which is why the iPhone camera always plays it. So far I have found a way to play sounds even when the iPhone is muted but it still relies on the user set volume. So I found a way using MPMusicPlayerController to control the application volume. This works, but when the volume is changed a box pops up signaling that the volume was changed.

Here is my code to play sounds even when muted:

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

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

I use the library Finch to play the sound (a light wrapper for openAL) and then MPMusicPlayerController to adjust the volume before play.

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

Anyone have experience with this or have made apps like this for Japan? I'm really at a loss. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-12-16 13:03:39

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

The MPVolumeView will, while visible, block the floating box, even if the user can't actually see it.

Some sample code…

// 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
    ...
}

Before changing volume to force the camera to make sound…

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

And after sounds, so it doesn't look like you messed with anything…

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

It might take some tweaking to get what you want, but it should be a good starting point.

This code is for iOS 5.1
I don't know what the compatibility is with older versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文