标签: avaudiosession

AVAudioSession阻止系统音频

我正在编写一个播放背景音乐的应用程序,为此我正在使用AVAudioSession,如下所示:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"song" ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithData:data error:NULL];
player.delegate = self;
[player setVolume:1.0];
...
[player prepareToPlay];
[player play];
Run Code Online (Sandbox Code Playgroud)

然后,稍后,用户可以选择禁用音乐.如果他们选择这样做,则运行以下代码:

[[AVAudioSession sharedInstance] setActive:NO error:nil];
[player stop];
Run Code Online (Sandbox Code Playgroud)

哪个成功阻止音乐播放.但是,如果音乐应用程序(或其他类似Spotify)的音乐启动,然后应用程序切换到,则用户音乐将淡出并暂停.我的应用程序的音乐无法播放,因为它仍处于禁用状态,因此用户可以保持沉默状态.

如何让我的应用程序暂停用户的音乐?

iphone objective-c avaudioplayer avaudiosession

2
推荐指数
1
解决办法
5779
查看次数

iOS 在没有音频会话的情况下播放视频

MPMoviePlayerController我正在尝试使用 a或 .在我的应用程序中播放短视频AVPlayer。问题是(因为我的视频没有任何声音)我不想干扰后台其他应用程序播放的声音。我尝试玩AVAudioSession

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient  withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[audioSession setActive:YES error:nil];
Run Code Online (Sandbox Code Playgroud)

但我没有运气。视频开始播放后,背景音乐就会停止。我什至尝试将音频会话设置为非活动状态:

   [[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,声音停止半秒,然后恢复,视频播放器停止播放。有什么方法可以实现我想要做的事情吗?谢谢。

mpmovieplayercontroller ios avaudiosession avplayer

2
推荐指数
1
解决办法
3040
查看次数

'|' 不是后缀一元运算符

我正在尝试为AVAudioSession类别添加选项:

let session = AVAudioSession.sharedInstance()
    let options = .MixWithOthers.toRaw()|.DefaultToSpeaker.toRaw()
    if session.setCategory(AVAudioSessionCategoryPlayAndRecord,
      withOptions: options,
      error: &error){
...
}
Run Code Online (Sandbox Code Playgroud)

我也试过了

let options = .MixWithOthers|.DefaultToSpeaker
Run Code Online (Sandbox Code Playgroud)

但它给了我同样的错误.

我该如何结合这些选项?

avaudiosession swift ios8

2
推荐指数
1
解决办法
3610
查看次数

录制后如何恢复背景音乐

在我的摄像机视图控制器的viewWillAppear方法中,我将音频会话的类别设置为记录并将其设置为活动状态.在viewWillDisappear的方法中,我将类别设置为环境并停用它.如果我在后台播放音乐,当我进入我的摄像机视图控制器时,它会停止,这很好.但是,当我离开时,在后台播放的音乐不会恢复,即使我告诉操作系统我的音频会话不再有效.

还有什么需要做才能恢复背景音乐?

avaudiosession

2
推荐指数
1
解决办法
1319
查看次数

核心音频:用于提升信号电平的音频单元

我们的VOIP应用程序使用语音处理IO单元和远程IO单元(我们根据我们需要的IO单元重建AUGraph).我们注意到音频输出电平不像其他一些VOIP应用程序(如Skype)那么响亮.

我正在研究如何将其他音频单元插入路径以提升信号电平,而不是操纵传入的PCM数据本身.我已经成功地使用了N波段和参数均衡器音频单元来做到这一点,但是想知道是否有更简单的方法或更合适的音频单元来提升信号电平?

audio boost core-audio ios avaudiosession

2
推荐指数
1
解决办法
310
查看次数

在macOS上播放声音

我喜欢在我的应用中播放声音.我发现了很多例子,也可以在Swift 3上编译和运行它们.但是它们总是用于iOS.当在我的应用程序中使用此代码时,AVAudioSession保持未定义.我需要做什么?OSX有什么不同之处吗?

import AVFoundation
...
    var audioPlayer = AVAudioPlayer()
    func PlaySound( ) {
        let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "Sound", ofType: "mp3")!)
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: alertSound)
        } catch _{
        }
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }
Run Code Online (Sandbox Code Playgroud)

audio macos avaudiosession swift

2
推荐指数
2
解决办法
2824
查看次数

AVAudioSession :通过耳机扬声器播放音频

我正在尝试通过耳机扬声器播放音频,它工作正常。相同的代码在以下情况下不起作用。

  1. 打开相机录制视频
  2. 不要开始录制,而是取消它
  3. 然后,尝试通过耳机播放音频不起作用。正在通过主扬声器播放

这是我通过耳机播放音频的代码。

-(void)initialAVaudioPlayer
{
    if (player==nil) {
        NSError *error = nil;
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
        [session setActive: YES error:nil];

        AVAudioSessionPortDescription *routePort = session.currentRoute.outputs.firstObject;
        NSString *portType = routePort.portType;

        if ([portType isEqualToString:@"Receiver"]) {
            [session  overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
        } else {
            [session  overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
        }        


        NSString *path;
        NSError *err;
        NSString *name;
        name = @"referenceaudio";
        path = [[NSBundle mainBundle] pathForResource:name ofType:@"wav"];
        if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
            NSURL *url = [NSURL fileURLWithPath:path];
            player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&err];
            if (!err) { …
Run Code Online (Sandbox Code Playgroud)

audio objective-c ios avaudiosession

2
推荐指数
1
解决办法
6561
查看次数

将音频更改为蓝牙设备并返回

当用户选择操作表上的选项时,我试图将音频输出更改为设备。这是代码,当我选择将音频发送到设备时,下次不会出现蓝牙。:

for input in AVAudioSession.sharedInstance().availableInputs!{
            if input.portType == AVAudioSessionPortBluetoothA2DP || input.portType == AVAudioSessionPortBluetoothHFP || input.portType == AVAudioSessionPortBluetoothLE{
                let bluetooth = UIAlertAction(title: input.portName, style: .default, handler: {
                    (alert: UIAlertAction!) -> Void in
                    let audioSession = AVAudioSession.sharedInstance()
                    do {
                        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth)
                        try audioSession.setActive(true)
                    } catch {
                        fatalError("Error Setting Up bluetooth output \(input.portName)")
                    }


                })

                bluetooth.setValue(UIImage(named:"bluetooth.png"), forKey: "image")
                optionMenu.addAction(bluetooth)
            }

let iphomeOutput = UIAlertAction(title: "iPhone", style: .default, handler: {
                (alert: UIAlertAction!) -> Void in
                let audioSession = AVAudioSession.sharedInstance()
                do {
                    do …
Run Code Online (Sandbox Code Playgroud)

audio bluetooth ios avaudiosession swift

2
推荐指数
1
解决办法
3329
查看次数

AVAudioSession 不适用于某些蓝牙设备

我的蓝牙播放有问题,但仅限于某些蓝牙设备。其他应用程序在这些相同的蓝牙设备上运行良好。我设置类别的方式是否遗漏了什么AVAudioSession

    let session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker, .allowBluetooth, .allowAirPlay])
        try session.setActive(true)

        session.requestRecordPermission({(granted:Bool) in
            if granted {
                Variables.appHasMicAccess = true
            } else {
                Variables.appHasMicAccess = false
            }
        })
    } catch let error as NSError {
        print("AVAudioSession configuration error: \(error.localizedDescription)")
    }
Run Code Online (Sandbox Code Playgroud)

更新

解决方案是添加一个附加选项.allowBluetoothA2DP。这是在 iOS 10 中引入的。

从 iOS 10.0 开始,使用 playAndRecord 类别的应用程序也可能允许将输出路由到配对的蓝牙 A2DP 设备。要启用此行为,您需要在设置音频会话的类别时传递此类别选项。

更多细节在这里

bluetooth ios avaudiosession swift

2
推荐指数
1
解决办法
1900
查看次数

想要在应用程序进入后台时保持AVPlayer播放

我必须使用avplayer播放mp3文件,但是当我去背景时它会停止,我如何保持它在背景状态下播放.任何建议都将受到高度赞赏.谢谢.

iphone xcode4 avaudiosession avplayer ios7

1
推荐指数
2
解决办法
4627
查看次数