模拟器上有声音但设备上没有

发布于 2024-08-17 04:00:06 字数 598 浏览 3 评论 0原文

我正在使用以下内容来播放 m4a 文件:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: fileName];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);

它在模拟器上运行良好,但我在设备上听不到任何声音。我正在使用的声音文件都保留在捆绑包中。以下是设备上的 filePath 的样子:

file://localhost/var/mobile/Applications/418945F3-3711-4B4D-BC65-0D78993C77FB/African%20Adventure.app/Switch%201.m4a

文件路径是否存在问题,或者我需要为设备做任何不同的事情?

I'm using the following to play an m4a file:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: fileName];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);

It works fine on the simulator but I hear nothing on the device. Sounds files I'm using all stay in the bundle. Here is what filePath looks like from the device:

file://localhost/var/mobile/Applications/418945F3-3711-4B4D-BC65-0D78993C77FB/African%20Adventure.app/Switch%201.m4a

Is there an issue with the file path or any thing different I need to do for the device?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万人眼中万个我 2024-08-24 04:00:06

作为旁注 - 我遇到了完全相同的问题,并且花了大约一个小时将文件转换为正确的格式等。但问题是 iPad 上的“静音”开关。所以即使音量调大了,我也能听到iPad上的其他声音,但因为静音开关打开了,所以它并没有播放系统声音。

更让人困惑的是,这个应用程序使用文本转语音,并且来自听写的音量非常好,只是来自 AudioServicesPlaySystemSound() 的声音没有被播放。

Just as a sidenote - I was having the exact same problem and spent probably close to an hour on converting files to the correct format, etc.. Yet the problem was the "mute" switch on the iPad. So even though the volume was up, and I could hear other sounds on the iPad, because the mute switch was turned on, it wasn't playing system sounds.

To add to the confusion, this app uses text-to-speech and the volume coming from the dictation was perfectly fine, it was only the sounds coming from AudioServicesPlaySystemSound() that weren't being played.

命硬 2024-08-24 04:00:06

我也遇到了这个问题。最后我意识到这是因为 AudioServices 只能播放具有以下限制的音频。

使用此功能播放的声音文件
函数必须是:
- 持续时间不超过30秒
- 采用线性 PCM 或 IMA4 (IMA/ADPCM) 格式
- 打包为 .caf、.aif 或 .wav 文件

来自 Apple 文档:http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html

I had trouble with this too. Finally I realised it was because AudioServices can only play audio with the following constratints.

Sound files that you play using this
function must be:
- No longer than 30 seconds in duration
- In linear PCM or IMA4 (IMA/ADPCM) format
- Packaged in a .caf, .aif, or .wav file

From Apple docs: http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html

挽手叙旧 2024-08-24 04:00:06

您可能想使用 AVAudioPlayer 而不是 AudioServices。

以下代码将获取音频文件 (.m4a) 并播放该音频文件 1 次。完成后不要忘记释放“audioPlayer”。

NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"m4a"];

NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSError *error;

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;

if (audioPlayer == nil)
{
  NSLog([error description]);
}
else
{
  [audioPlayer play];
}

希望这个例子能帮助您在实际设备上播放音频。在播放文件时增加设备音频也可能是个好主意。

注意:如果您尚未将 AVFoundation 框架添加到您的项目中,则需要这样做。以及导入头文件。

#import

更新:

来自 Apple 的核心音频概述 文档

音频会话服务

音频会话服务可让您管理应用程序中的音频会话 — 协调应用程序中的音频行为与 iPhone 或 iPod touch 上的后台应用程序。音频会话服务由 AudioToolbox.framework 中的 AudioServices.h 头文件中声明的函数、数据类型和常量的子集组成。

AVAudioPlayer 类

AVAudioPlayer 类提供了一个简单的 Objective-C 接口来播放声音。如果您的应用程序不需要立体声定位或精确同步,并且您不播放从网络流捕获的音频,Apple 建议您使用此类进行播放。该类在AVFoundation.framework的AVAudioPlayer.h头文件中声明。

You might want to use the AVAudioPlayer instead of AudioServices.

The following code will take an audio file (.m4a) and play the audio file 1 time. Don't forget to release "audioPlayer" when you're done with it.

NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"m4a"];

NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSError *error;

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;

if (audioPlayer == nil)
{
  NSLog([error description]);
}
else
{
  [audioPlayer play];
}

Hope this example helps you with playing audio on the actual device. It might also be a good idea to increase the device audio when the file is playing.

Note: You will need to add the AVFoundation framework to your project if you have not already done so. As well as import the header file.

#import <AVFoundation/AVFoundation.h>

Update:

From Apple's Core Audio Overview Document

Audio Session Services

Audio Session Services lets you manage audio sessions in your application—coordinating the audio behavior in your application with background applications on an iPhone or iPod touch. Audio Session Services consists of a subset of the functions, data types, and constants declared in the AudioServices.h header file in AudioToolbox.framework.

The AVAudioPlayer Class

The AVAudioPlayer class provides a simple Objective-C interface for playing sounds. If your application does not require stereo positioning or precise synchronization, and if you are not playing audio captured from a network stream, Apple recommends that you use this class for playback. This class is declared in the AVAudioPlayer.h header file in AVFoundation.framework.

首先对您的退货进行错误检查。 filePath 是否为零? AudioServices 函数是否返回错误?最可能的原因是区分大小写。 iPhone 文件系统区分大小写,而 Mac 则不区分大小写。但调试的第一步是查看系统提供的错误。

Start by error-checking your returns. Is filePath nil? Do either of the AudioServices functions return an error? The most likely cause is case-sensitivity. The iPhone filesystem is case sensitive while the Mac is not. But the first step in debugging is to look at the errors the system is providing.

有深☉意 2024-08-24 04:00:06

模拟器使用常规 QuickTime 进行播放,因此很容易让媒体资源在 sim 中工作,但由于缺少/不受支持的编解码器而在设备上失败。测试是您是否可以在设备上播放该文件,例如通过 Safari 或 iPod 应用程序。

The simulator uses regular QuickTime for playback, so it's easy to have media assets which work in the sim, but fail on the device due to missing / unsupported codecs. The test is if you can play the file at all on the device, eg through Safari or the iPod app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文