gpt4 book ai didi

ios - 声音无法在 iPad 扬声器上播放,但在耳机和 iPod Touch/iPhone 扬声器上可以正常播放

转载 作者:可可西里 更新时间:2023-11-01 04:26:37 24 4
gpt4 key购买 nike

我知道这个问题已经被问了很多次了,我已经检查了 SO 中的大部分相关答案,但我没有找到正确答案来解决我的问题。

问题是:

我尝试在触发某些事件时在游戏中播放 mp3 文件(最多仅 2 秒),我使用 AudioPlayer 来播放,下面是代码块:

NSError *error;
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"ding" withExtension: @"mp3"] error:&error] autorelease];
if (error) {
NSLog(@"Error creating audio player: %@", [error userInfo]);
}
else {
BOOL success = [audioPlayer play];
// This always is "Play sound succeeded"
NSLog(@"Play sound %@", success ? @"succeeded" : @"failed");
}

当我在 iPhone 4s、iTouch 3/4 上运行此代码时,声音总是播放得很好且清晰,但在 iPad 1 或 iPad2 上,扬声器没有声音。但是当我插入耳机时,奇怪的事情发生了,我的耳机有声音! iPad 未处于静音模式且 URL 正确。

我很困惑为什么会这样。

PS:我尝试了以下代码(来自 HERE )来输出音频输出端口类型:

CFDictionaryRef asCFType = nil;
UInt32 dataSize = sizeof(asCFType);
AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &asCFType);
NSDictionary *easyPeasy = (NSDictionary *)asCFType;
NSDictionary *firstOutput = (NSDictionary *)[[easyPeasy valueForKey:@"RouteDetailedDescription_Outputs"] objectAtIndex:0];
NSString *portType = (NSString *)[firstOutput valueForKey:@"RouteDetailedDescription_PortType"];
NSLog(@"first output port type is: %@!", portType);

当我插入耳机时,输出是"first output port type is headphone!"当我拔下耳机时,输出结果是"first output port type is扬声器!”

如果有人可以提供一些帮助或建议,那就太好了。

最佳答案

对此有一个代码更改解决方案,但也有一个最终用户解决方案:打开“响铃/静音开关”。具体来说,问题在于 AVAudioSessions 的默认设置 AVAudioSessionCategorySoloAmbient 如果手机处于静音模式,则为静音。

正如原始发帖人所提到的,您可以通过调用来覆盖此行为:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

AVAudioSession Reference建议设置 AVAudioSessionCategoryPlayback 类别:

For playing recorded music or other sounds that are central to the successful use of your app.

关于ios - 声音无法在 iPad 扬声器上播放,但在耳机和 iPod Touch/iPhone 扬声器上可以正常播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com