草庐IT

ios - 在 iOS7 及更高版本中切换 Receiver 和 Speaker 之间的音频输出?

coder 2023-09-25 原文

我有一个音频播放器,当接近传感器通知 1 时,它可以选择将音频输出从扬声器切换到接收器/听筒(无论是否连接耳机)。以下是我这样做的代码。

- (void) switchAudioOutput:(NSString*)output{
    AVAudioSession* audioSession = [AVAudioSession sharedInstance];
    BOOL success;
    NSError* error;

    if([output isEqualToString:keAudioOutputReciever]){
        //Force current audio out through reciever
        //set the audioSession override
        success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone
                                             error:&error];
        if (!success)
            NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);

        //activate the audio session
        success = [audioSession setActive:YES error:&error];
        if (!success)
            NSLog(@"AVAudioSession error activating: %@",error);
        else
            NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone");

    }else if([output isEqualToString:keAudioOutputSpeaker]){
        //set the audioSession override
        success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                                  error:&error];
        if (!success)
            NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);

        //activate the audio session
        success = [audioSession setActive:YES error:&error];
        if (!success)
            NSLog(@"AVAudioSession error activating: %@",error);
        else
            NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideSpeaker");

    }
 }

这是基于答案 Toggle Button route audio to speaker and receiverenter link description here .我注意到这只会强制将音频单独发送到扬声器,但不能确保路由单独发送到接收器。此外,在切换到扬声器时出现以下错误:

AVAudioSession error overrideOutputAudioPort:Error Domain=NSOSStatusErrorDomain Code=-50 "The operation couldn’t be completed. (OSStatus error -50.)"

最佳答案

我通过避免覆盖 Receiver 找到了答案

- (void) setAudioSession:(NSString*)audioOutput{

        NSError* error;
        if([audioOutput isEqualToString:audioOutputSpeaker.lowercaseString]){

            //set the audioSession override
            if(![self setCategory:AVAudioSessionCategoryPlayAndRecord
                              withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionAllowBluetooth
                                    error:&error])
                NSLog(@"AVAudioSession error AVAudioSessionCategoryPlayAndRecord:%@",error);

            //activate the audio session
            if (![self setActive:YES error:&error])
                NSLog(@"AVAudioSession error activating: %@",error);
            else
                NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone");
        }else if ([audioOutput isEqualToString:audioOutputReciever.lowercaseString]){
            //Force current audio out through reciever
            //set the audioSession override
            if(![self setCategory:AVAudioSessionCategoryPlayAndRecord
                              withOptions:AVAudioSessionCategoryOptionAllowBluetooth
                                    error:&error])
                NSLog(@"AVAudioSession error AVAudioSessionCategoryPlayAndRecord:%@",error);

            if (![self overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error])
                NSLog(@"AVAudioSession error overrideOutputAudioPort to Reciever:%@",error);

            //activate the audio session
            if (![self setActive:YES error:&error])
                NSLog(@"AVAudioSession error activating: %@",error);
            else
                NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone");
        }
    }

关于ios - 在 iOS7 及更高版本中切换 Receiver 和 Speaker 之间的音频输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26141175/

有关ios - 在 iOS7 及更高版本中切换 Receiver 和 Speaker 之间的音频输出?的更多相关文章

  1. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  2. ruby-on-rails - Ruby on Rails with Haml - 如何从 erb 切换 - 2

    我正在从erb文件切换到HAML。我将hamlgem添加到我的系统中。我创建了app/views/layouts/application.html.haml文件。我应该只删除application.html.erb文件吗?此外,仍然有/public/index.html文件被呈现为默认页面。我想创建自己的默认index.html.haml页面。我应该把它放在哪里以及如何使系统呈现该文件而不是默认索引文件?谢谢! 最佳答案 是的,您可以删除任何已转换为HAML的View的ERB版本。至于你的另一个问题,删除public/index/h

  3. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  4. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  5. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  6. ruby - 当使用::指定模块时,为什么 Ruby 不在更高范围内查找类? - 2

    我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or

  7. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  8. ruby-on-rails - `a ||= b` 和 `a = b if a.nil 之间的区别? - 2

    我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行

  9. ruby-on-rails - 如果我将 ruby​​ 版本 2.5.1 与 rails 版本 2.3.18 一起使用会怎样? - 2

    如果我使用ruby​​版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby​​1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更

  10. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

随机推荐