- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
嘿,下面是这段代码。我只是想得到它,以便如果他们是任何音乐、播客等正在从操作系统内的另一个应用程序播放,所有声音都会被静音。但是我已经尝试设置它的类别,但它不起作用!我没有使用正确的类别吗?也许不在正确的地方?
目标是用户可以在使用该应用程序时收听自己的音乐。
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("GameMusic9", ofType: "mp3")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("GameMusicRP", ofType: "mp3")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Death", ofType: "mp3")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Tap", ofType: "wav")!)!)
//Monsters
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Blue", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Brown", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Green", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Orange", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Purple", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Red", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Tan", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Yellow", ofType: "wav")!)!)
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Correct", ofType: "wav")!)!)//12
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Cowbell", ofType: "aiff")!)!)//13
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("12", ofType: "wav")!)!)//14
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Hybrid", ofType: "mp3")!)!)//15
sounds.append(NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Correct", ofType: "wav")!)!)//16
AVAudioSession.sharedInstance().setActive(true, error: nil)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
//Close or punch, or bycycle
lỗi ở đâu:NSError?
for var i:Int = 0; i < sounds.count; i++
{
audioPlayers.append(AVAudioPlayer(contentsOfURL: sounds[i], error: &error))
audioPlayers.last?.prepareToPlay()
}
câu trả lời hay nhất
不,您没有使用正确的类别。来自tài liệu :
AVAudioSessionCategoryAmbient
The category for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.
This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays while the Music app is playing. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).
thửAVAudioSessionCategoryPlayback
:
AVAudioSessionCategoryPlayback
The category for playing recorded music or other sounds that are central to the successful use of your app.
When using this category, your app audio continues with the Silent switch set to silent or when the screen locks. (The switch is called the Ring/Silent switch on iPhone.) To continue playing audio when your app transitions to the background (for example, when the screen locks), add the audio value to the UIBackgroundModes key in your information property list file.
By default, using this category implies that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing for this category, use the AVAudioSessionCategoryOptionMixWithOthers option.
或者尝试 AVAudioSessionCategorySoloAmbient
,它类似于 AVAudioSessionCategoryAmbient
,但默认情况下不可混合。
biên tập:我误解了这个问题。目标是仅在其他应用程序未播放任何内容时才播放音乐。这可以通过检查共享的 AVAudioSession
来实现:
[[AVAudioSession sharedInstance] isOtherAudioPlaying];
只有在返回 KHÔNG
时才播放您的音乐。请注意,从 iOS 8.0 开始,最好使用另一个属性(它们的语义略有不同):
[[AVAudioSession sharedInstance] secondaryAudioShouldBeSilencedHint];
The value is YES when another application with a non-mixable audio session is playing audio.
Applications should use this property as a hint to silence audio that is secondary to the functionality of the application. For example, a game using AVAudioSessionCategoryAmbient can use this property to decide to mute its soundtrack while leaving its sound effects unmuted.
您还应该订阅 AVAudioSessionSilenceSecondaryAudioHintNotification
通知并停止/开始您的音乐以反射(reflect) secondaryAudioShouldBeSilencedHint
中的变化。
NSNotificationCenter.defaultCenter().addObserverForName(AVAudioSessionSilenceSecondaryAudioHintNotification, object: nil, queue: nil) { notification in
// stat/stop music based on AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint
}
关于ios - AVAudioSession 在系统声音之上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32874204/
是否可以在无需用户点击或鼠标悬停的情况下播放声音文件? 我有一个记分牌,我想在球队得分时播放声音文件。任何指示将不胜感激。我基本上完成了记分牌,但没有声音。 谢谢。 最佳答案 https://gith
我正在创建一个音频应用程序,其中有两个名为 录制音频 浏览音频 当用户单击第一个按钮时,他可以录制音频。这已经实现。 当用户单击第二个按钮时,他可以浏览以查找iPhone库中已经存在的音频/声音。我对
香港专业教育学院一直在使用SoX来将文件修剪为恰好2秒长,但是我注意到音频文件最后总是额外多了32毫秒左右,显然它的额外数据是要告知其他解码器其信息,但是否必须添加放在文件的长度上? 我创建了一个程序
我将使用代码来获取设备的默认音量/声音,该默认音量/声音是使用设备上的音量调高或调低按钮设置的,下面是我要访问声音的代码, 为了解决此错误,我已经进行了研究,发现要访问此代码,我们需要使用CoreAu
我有解码 MP3 并用所有“值”填充数组的代码。 我的问题是:这些值(value)观是什么?它们是频率吗?它们是振幅吗? 这是代码: File file = new File(song.getFile
哈乌乌,我正在尝试实现 Pong。 现在我想播放声音,但它抛出异常(UnsupportedAudioFileException)。我做错了什么? AudioInputStream ainBalk;
我在大学的一个兄弟会中,在这个兄弟会中我们有楼梯。时不时有人从那些楼梯上掉下来。我们通常从吧台后面的电脑播放音乐(通常来自互联网或 iTunes)。我有一个 usb 按钮,想编写一个程序,当有人从楼梯
我想检测来自用户语音的声音/噪音,如果语音输入为空,它会自动停止。 为应用程序点赞 talking tom cat当有声音/语音输入时它会自动开始收听,当没有声音/语音输入时会自动停止。 任何帮助都将
我正在使用 jQuery Sound Plugin在我的网站上创建一些声音效果,但我无法播放。我收到此消息: settings.events.error(null, {msg: "You have n
我有一段代码可以在我点击一个按钮后播放声音。当我第二次单击此按钮时,首先会出现重置之类的东西。 我想要的是:每次单击按钮时我都想立即播放声音而无需重置按钮。 我的代码: -(IBAction)play
我在android studio中制作了一个闹钟。我可以运行该应用程序,除了播放闹钟铃声外,其他一切正常。实际上,当闹钟时间到来时,没有声音播放。我不知道我的代码有什么问题。请帮我找出错误。 主要 A
有什么方法可以在关闭声音的情况下播放 UILocalNotification 声音。实际上,我正在尝试创建一个闹钟,即使用户关闭了声音也能正常工作。或实现此目的的任何替代方法。 最佳答案 如果用户关闭
我试图从字符串创建音频,我试图举一个例子,用户输入他们的名字,然后将其转换为声音/音频 - 声音/音频会根据输入的字符串而有所不同。 (我不想在字符串上执行“文本到语音”,只是创建由字符串生成的声音,
Thật khó để nói những gì để hỏi ở đây. Câu hỏi không rõ ràng, mơ hồ, không đầy đủ, quá rộng hoặc hùng biện và không thể trả lời hợp lý ở dạng hiện tại. Để được trợ giúp làm rõ vấn đề này để có thể mở lại, hãy truy cập trung tâm trợ giúp Đóng 1.
对大量二进制文件(例如音频和视频文件)进行版本控制的最佳方法是什么? Git 似乎并不是真正为处理大量二进制文件而设计的。 另一个问题是内容制作者不一定想学习如何使用像 Git 这样的开发人员工具。
我想让一个 python 程序在它完成任务时发出哔声来提醒我。目前,我使用 import os 然后使用命令行语音程序说“处理完成”。我宁愿它是一个简单的“铃铛”。 我知道 Cocoa 应用程序中可以
请原谅这个愚蠢的新手问题,但是:当我(不小心)在命令行窗口中按退格键时,如何关闭 MATLAB 发出的极其烦人的“哔”声? 最佳答案 只是beep off在最新版本中。 https://www.mat
如何找出用户在控制面板中配置了哪些声音文件? 示例:我想播放“设备已连接”的声音。 哪个API可用于查询控制面板声音设置? 我看到控制面板对话框中有一些由第三方程序创建的自定义条目,因此必须有一种方法
我对实现与此人 link 类似的处理方式感兴趣。 据我了解,她将一段视频切成 tiff 格式,然后使用 RiTa 库进行合成 有谁知道如何实现这样的事情,只是改变我正在使用其他扩展名或文件格式的事实。
使用 C#,我试图捕获 PC 正在播放的音频,而不使用 WASAPI 和环回,因为我的声卡似乎不支持它。 TeamViewer 之类的程序是如何做到的?当我使用它时,人们可以从我的 PC 听到音频。
Tôi là một lập trình viên xuất sắc, rất giỏi!