我做了一个iPhone应用程序,在运动时使用.它会播放铃声,表示您(用户)应该从练习例程的一个步骤切换到下一个步骤.我设计了应用程序,以便您在使用应用程序时可以在iPod上听音乐,我希望音调可以在音乐上充分发声.我已经让这个工作......有点......
当音乐响亮时,很难听到音调.我理想的解决方案类似于iPod应用程序处理传入短信或电子邮件的方式.音乐音量降低,声音播放,音乐音量逐渐消失.
以下是我到目前为止尝试过的方法:
我以前常常AudioServicesPlaySystemSound播放声音.
我初始化声音是这样的:
CFBundleRef mainBundle = CFBundleGetMainBundle();
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, CFSTR ("bell"), CFSTR ("wav"), NULL);
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileID);
Run Code Online (Sandbox Code Playgroud)
我在适当的时候使用以下方式播放声音:
AudioServicesPlaySystemSound (self.soundFileID);
Run Code Online (Sandbox Code Playgroud)
这播放声音很好,但是听到嘈杂的音乐太难了.试图2 ...
我试图降低iPod音量,播放声音,然后将音量恢复到之前的水平.
如果当前步骤还剩1秒,则开始降低音量:
if ([self.intervalSet currentStepTimeRemainingInSeconds] == 1) {
self.volumeIncrement = originalVolume/5.0f;
[NSTimer scheduledTimerWithTimeInterval:0.5f
target:self
selector:@selector(fadeVolumeOut:)
userInfo:[NSNumber numberWithInt:1]
repeats:NO];
}
Run Code Online (Sandbox Code Playgroud)
这是fadeVolumeOut:方法:
- (void) fadeVolumeOut:(NSTimer *)timer {
if (!TARGET_IPHONE_SIMULATOR) {
NSNumber *volumeStep = (NSNumber *)[timer userInfo];
int vStep = [(NSNumber *)volumeStep intValue];
float volume = [[MPMusicPlayerController iPodMusicPlayer] volume];
volume = volume - self.volumeIncrement; …Run Code Online (Sandbox Code Playgroud)我有以下代码(重新)开始AVAudioEngine连接到AVAudioEngineConfigurationChangeNotification:
do {
try self.engine.start()
} catch {
DDLogError("could not start sound engine")
self.soundEnabled = false
return
}
Run Code Online (Sandbox Code Playgroud)
self.engine定义为
private let engine = AVAudioEngine()
Run Code Online (Sandbox Code Playgroud)
但是,我经常通过Crashlytics收到崩溃报告
致命异常:com.apple.coreaudio.avfaudio错误561015905
在包含的线上try self.engine.start().
561015905是AVAudioSessionErrorCodeCannotStartPlaying从我的理解,这应该是一个NSError错误代码,而不是一个例外,应该由我catch在上面的代码中的空白捕获.不过,该应用程序似乎只是崩溃了.我错过了什么?
我知道有些情况下应用程序会在可能发生此错误的后台唤醒,我会很好,只要我能够以某种方式发现它,就像我想的那样do/catch.
我需要在我的iPhone应用程序中循环播放.caf文件.
AVAudioPlayer看起来很有前途.但是有一个问题.即使我按下iPhone上的静音按钮,它也不会停止或静音.
我知道在iOS 5中无法以编程方式找出是否按下了静音按钮.
所以,我需要知道的可能是AVAudioPlayer或AVAudioSession的一些属性.或者,也许,我可能需要考虑其他库来循环播放我的.caf文件.
任何的想法?
尽管有很多关于这个主题的帖子(AVPlayer和AVAudioSession,在AppDelegate.m中设置AVAudioSession类别,AvAudioSession在iOS7中不起作用?),我无法解决iOS 7中的这个特定问题:
- 我想收到AVAudioSession路由更改通知在后台
- 我的应用程序在任何时候都没有播放任何音乐,并且不应该干扰其他播放声音的应用程序
设置:
- 在p-list UIBackgroundModes中启用音频
- 在主控制器中:
// AUDIO
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[audioSession setActive:YES error:nil];
// NOTIFICATIONS
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioSessionRouteChanged:)
name:AVAudioSessionRouteChangeNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
这在前台工作正常,所以我必须缺少一些简单的启用背景通知的东西.我从AVAudioSessionCategoryAmbient开始尝试了几个不同的类别,没有运气.
播放声音时只接收背景通知是否有限制?
谢谢你的帮助!
更新需要播放AVPlayer才能在后台接收通知.所以这是不可行的,期待通过播放一个无法接受苹果的无声音轨......
我的应用程序在后台 播放得很好.即使我的应用程序是使用注册的方法的后台,暂停和播放也很有效AVAudioSessionInterruptionNotification.
问题到达方案的步骤:
代码:我在appdelegate中这样做. didfinishlaunching
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error: nil];
self.player = [[AVQueuePlayer alloc] initWithURL:[NSURL URLWithString:@"http://someurl.com:8040/;stream.mp3"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
- (void) onAudioSessionEvent: (NSNotification *) notification
{
//Check the type of notification, especially if you are sending multiple AVAudioSession events here
NSLog(@"Interruption notification name %@", notification.name);
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"Interruption …Run Code Online (Sandbox Code Playgroud) 我想使用音频录制视频AVCaptureSession.为此,我需要AudioSessionCategory AVAudioSessionCategoryPlayAndRecord,因为我的应用程序还播放带声音的视频.
我希望从默认扬声器中听到音频,我希望它能与其他音频混音.所以我需要选项AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers.
如果我在播放其他音频时执行以下操作,则其他应用程序的音频中会出现明显的可听故障:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
Run Code Online (Sandbox Code Playgroud)
有没有办法摆脱这个小故障?
当没有麦克风输入AVAudioSessionRouteChangeReasonRouteConfigurationChange通知AVAudioSessionRouteChangeReasonCategoryChange麦克风输入时,似乎会出现这种情况.
当应用程序进入后台时(无论是否取消激活音频会话),也会发生配置更改(和故障).当从后台返回而不停用音频会话时,当AVCaptureSession配置改变时,即当摄像机从前向后切换时,毛刺开始发生.在这种情况下,不触摸音频路由,并且仅在从后台返回而不停用音频会话时发生.路由更改的通知被触发两次.一旦禁用麦克风,一次再启用它.
请注意,通过下载Apple的AVCamManual示例,可以轻松重现此行为.添加以下到viewDidLoad的AAPLCameraViewController.m:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
…
session.usesApplicationAudioSession = YES;
session.automaticallyConfiguresApplicationAudioSession = NO;
Run Code Online (Sandbox Code Playgroud)
其他一些奇怪的事情,可能是相关的:
将音频类别设置为AVAudioSessionCategoryAmbient第一个并激活它:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
Run Code Online (Sandbox Code Playgroud)
然后更改类别:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
Run Code Online (Sandbox Code Playgroud)
尽管有选项标志,其他音频停止播放.没有错误被抛出.
objective-c avfoundation ios avcapturesession avaudiosession
我正在尝试使用AVAudioRecorder的averagePowerForChannel方法来监控iPad/iPhone应用程序的麦克风输入电平.我有一个回调轮询循环中的平均水平 - 在iPhone上它工作正常并返回合理的水平,但由于某种原因在iPad上它总是返回-120.0.
这是我的一些设置代码:
- (void) setupMic {
if (micInput) {
[micInput release];
micInput = nil;
}
NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"];
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
[recordSettings setObject:[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:22050.0] forKey: AVSampleRateKey];
// [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityLow] forKey: AVEncoderAudioQualityKey];
micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil];
// [micInput setMeteringEnabled:YES];
[newURL release];
[recordSettings removeAllObjects];
[recordSettings release];
}
Run Code Online (Sandbox Code Playgroud)
以及我的开始录制方法:
- (void) startRecording { …Run Code Online (Sandbox Code Playgroud) 所以我有一个应用程序播放一堆歌曲,而用户可以浏览漫画书.我使用AVAudioPlayer,我将它设置为按设定顺序播放歌曲.所以当一首歌结束时,下一首歌将播出.当应用程序打开时,这可以完美运行.当应用程序在后台时出现问题.我将应用程序设置为在后台播放,并且工作正常.因此,当用户按下主屏幕时,音乐继续播放.当歌曲结束时会出现问题,假设播放应用程序打开时播放下一首歌曲.相反没有任何反应 根据我的NSLog语句,正在调用正确的方法,但没有任何反应.这是我的代码:
- (void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully: (BOOL) flag {
NSLog(@"Song finished");
if ([songSelect isEqualToString: @"01icecapades"]) {
isPlay = @"yes";
songSelect = @"02sugarcube";
imageSelect = @"playbanner02";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"02sugarcube"]) {
isPlay = @"yes";
songSelect = @"03bullets";
imageSelect = @"playbanner03";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"03bullets"]) {
isPlay = @"yes";
songSelect = @"04satanama";
imageSelect = @"playbanner04";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) …Run Code Online (Sandbox Code Playgroud) 我有一个如下设置,在带有iOS 10.3的iPhone 5s上进行测试,无论是进出调试.
AVAudioRecorder火灾record(forDuration: 5.0)CADisplayLink监视录像机的水平(录像机meteringEnabled),同步动画和跟踪recorder.currentTime(但可以通过最低限度地跟踪显示链接中的时间来重现问题)recorder.currentTime持续达到值> 5(通常5.2至5.5).结果与值基本一致recorder.deviceTime,并CFAbsoluteTimeGetCurrentAVAudioPlayer并验证声音资产的持续时间恰好为5.0秒. 根据我的理解,录音机currentTime以秒为单位进行测量,当录音机恢复为假时会重置为0.0,这会在录音机停止时立即发生(这与我所看到的一致audioRecorderDidFinishRecording)...所以观察a CADisplayLink应该产生currentTime值严格小于5.0?
问题是:这里可能出现什么问题?录音机正好在5秒后停止,但在内部它认为它录制时间超过5秒?我正在听我的所有录音,听起来不错.
我不确定它是否相关,但我AVAudioSession的类型AVAudioSessionCategoryPlayAndRecord,我的音频设置如下(所有这些除了采样率是以后分析所必需的):
audioSettings = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVSampleRateKey: 44100,
AVNumberOfChannelsKey: 2,
AVLinearPCMIsBigEndianKey: 0,
AVLinearPCMIsFloatKey: 0,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsNonInterleaved: 0
]
Run Code Online (Sandbox Code Playgroud)
我已经尝试摆弄所有这些,并没有看到任何改变行为.
在CADisplayLink从主线程通过加入
recorderDisplayLink?.add(to: RunLoop.current, forMode: RunLoopMode.commonModes)
Run Code Online (Sandbox Code Playgroud)
我可以在堆栈交换中找到的唯一类似的问题就是这个,但是这个问题没有说明,答案也没有用(至少对我来说).
起初我想也许问题可能是主队列超载,所以不知何故记录器的时间感变得臃肿(我认为这仍然会构成不良行为),但是在禁用动画之后(并且还尝试使用Timer而不是CADisplayLink),问题仍然存在!也许它仍然是一个线程问题,但我不明白为什么会这样.如果我确实需要多线程,我可以使用一些帮助理解和实施:)任何想法赞赏.
avaudiosession ×10
ios ×10
avfoundation ×2
objective-c ×2
swift ×2
background ×1
cocoa-touch ×1
iphone ×1
touch ×1