每次加载应用程序时,它都会停止,就好像我在此行上设置了一个断点:
self.audioPlayer =
[[[AVAudioPlayer alloc] initWithData:[dataPersister loadData:self.fileName]
error:&outError] autorelease];
Run Code Online (Sandbox Code Playgroud)
这条线上方或附近没有断点.它只在我在调试模式下运行应用程序时发生,并且在断点之后没有任何崩溃.当我点击"继续执行程序"时,该应用程序无效.
这是loadData方法,使用initWithData
以下方法调用:
-(NSData*)loadData:(NSString*)fileName
{
NSString *dataPath = [self.path stringByAppendingPathComponent:fileName];
dataPath = [dataPath stringByStandardizingPath];
NSData *data = [[[NSData alloc] initWithContentsOfFile:dataPath]autorelease ];
return data;
}
Run Code Online (Sandbox Code Playgroud)
loadData函数似乎工作正常.在断点之后加载并播放请求的mp3文件没有任何问题.
你知道我做错了什么吗?
编辑: 当它在断点处停止时我跑了回溯.这是输出:
(lldb) bt * thread #1: tid = 0x1c03, 0x30df1724 libc++abi.dylib`__cxa_throw, stop reason = breakpoint 1.2 frame #0: 0x30df1724 libc++abi.dylib`__cxa_throw frame #1: 0x36403a24 AudioToolbox`ID3ParserHandle::ID3ParserHandle(void*, long (*)(void*, unsigned long, unsigned long, unsigned long, void**, unsigned long*)) + 452 frame #2: 0x36403b0e AudioToolbox`ID3ParserOpen + 142 …
我正试图播放声音,AVAudioPlayer
但它无法正常工作.
编辑1:仍然无法正常工作.
编辑2:此代码有效.我的设备处于静音模式.
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button-09", ofType: "wav"))
println(alertSound)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}
Run Code Online (Sandbox Code Playgroud) 为iPhone开发应用程序,其中包含需要通过耳机收听的音频文件.
如何检查耳机是否未插入,以便告诉用户插入耳机.
我有来自另一个线程的以下代码,但不推荐使用audioSessionGetProperty方法.任何人都知道如何更改以下代码以使其工作或拥有自己的代码/解决方案.
谢谢.
- (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
//Maybe changing it to something like the following would work for iOS7?
//AVAudioSession* session = [AVAudioSession sharedInstance];
//OSStatus error = [session setCategory:kAudioSessionProperty_AudioRoute...?
//the line below is whats giving me the warning
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
/* Known values of route:
* "Headset"
* "Headphone"
* "Speaker"
* "SpeakerAndMicrophone"
* "HeadphonesAndMicrophone"
* "HeadsetInOut"
* "ReceiverAndMicrophone"
* "Lineout"
*/
if (!error && (route != NULL)) …
Run Code Online (Sandbox Code Playgroud) 我想创建一个应用程序,当iPhone处于静音模式时,它会创建声音,音乐或系统声音.在静音模式下,音乐或系统音调是否可以播放任何类型的声音?
我有一个AVAudioPlayer播放一些音频(呃!)
当用户按下按钮时启动音频.当他们发布它时,我希望音频淡出.
我正在使用界面构建器...所以我试图在"内部触摸"上连接一个功能,使音频在1秒内消失,然后停止.
有任何想法吗?
谢谢
许多报警应用程序都设法做到这一点我不认为他们正在使用静音音频来保持应用程序运行,因为如果你真的退出家用应用程序它们不会发出声音.
...他们是否在锁定某些通知(无法以静音播放声音)之后保持循环运行,启动应用程序备份以播放音频或其他方法?
AVAudioPlayer使用:
AudioSessionInitialize(nil, nil, nil, nil);
AudioSessionSetActive(YES);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof(sessionCategory),&sessionCategory);
Run Code Online (Sandbox Code Playgroud)
并将Info.plist设置为:
所需的后台模式(UIBackGroundModes) - 应用播放音频(音频)
即使在应用程序运行和屏幕上的静音中,我也可以播放音频.如果音频已在运行,则可以使用主页按钮关闭应用程序并运行音频.但是,如果应用程序没有播放音频,并且屏幕被锁定,则所有线程都将被终止并且音频永远不会播放.这些应用程序如何解决这个问题?
A.使用'beginBackgroundTaskWithExpirationHandler:'使用无限循环来保持应用程序无限期运行.
优点:
缺点:
评论:
我几乎已经开始工作了,可能需要一些调整.这似乎不是所有这些其他警报正在做的事情,因为如果你按回家BTN他们不会继续运行.这表明他们使用了一些方法,允许他们在锁定时运行但不在BG中运行.(这就是我需要的)
此外,当你问你剩下多少时间跑步时,你会得到大约10分钟.通过在那里放置一个无限循环,数字实际上会降低到0,然后进入负数几个小时.(测试)我不确定这将在现实世界中或在应用程序接受方面如何表现.
B.使用静音音频循环构成连续音频播放媒体中心
优点:
缺点:
评论:
这可以在很多情况下起作用,但到目前为止还不理想.而且就像我再说一遍,必须有另一种没有记录的方法.
使用列出的APP进行测试表明他们没有使用我刚才描述的两种方法中的任何一种.方法'A'似乎更接近,但如果实施则不会表现出这些应用程序的行为方式.
我使用苹果开发者票来获取更多信息,我也会发布任何新的发现.
我们非常感谢您的参与,并感谢您的参与.
即使应用程序进入后台,我也想播放音乐.我检查了所有stackoverflow链接,但没有一个工作.请帮助今天做.
我使用了以下代码: -
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Day At The Beach" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *error;
playerTemp = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
playerTemp.numberOfLoops = 0;
AudioSessionSetActive(true);
[playerTemp play];
Run Code Online (Sandbox Code Playgroud) 我试图通过iPhone上的AVAudioPlayer播放(非常短 - 少于2秒)的音频文件来消除启动延迟.
一,代码:
NSString *audioFile = [NSString stringWithFormat:@"%@/%@.caf", [[NSBundle mainBundle] resourcePath], @"audiofile"];
NSData *audioData = [NSData dataWithContentsOfMappedFile:audioFile];
NSError *err;
AVAudioPlayer *audioPlayer = [(AVAudioPlayer*)[AVAudioPlayer alloc] initWithData:audioData error:&err];
audioPlayer.delegate = self;
[audioPlayer play];
Run Code Online (Sandbox Code Playgroud)
一旦完成,我还实现了audioPlayerDidFinishPlaying方法来释放AVAudioPlayer.
我第一次播放音频时滞后是显而易见的 - 至少2秒.然而,之后立即播放声音.我怀疑那个罪魁祸首是[NSData dataWithContentsOfMappedFile]最初从闪存中读取了很长时间,但后来读取速度很快.不过,我不确定如何测试.
是这样的吗?如果是这样,我是否应该预先缓存NSData对象并积极地在低内存条件下清除它们?
我正在尝试在我的应用程序中实现一个小功能.我目前正在播放声音作为AVAudioPlayers并且工作正常.我想补充的是用UISlider控制声音的位置(currentTime):有一个简单的方法吗?
我查看了一个Apple项目,但它非常混乱....你有样品或建议吗?
感谢大家提前
我尝试使用这些方法试图检测Ring/Silent开关是否处于活动状态:
但是在我的iPhone 4上,"状态"值始终是"扬声器"(并且CFStringGetLength(状态)返回的长度值始终为7).有没有人成功使用过这种方法?如果是这样,关于什么样的设备和SDK版本?
我这样称呼它:
- (BOOL)deviceIsSilenced {
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
OSStatus audioStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (audioStatus == kAudioSessionNoError) {
NSLog(@"audio route: %@", state) // "Speaker" regardless of silent switch setting, but "Headphone" when my headphones are plugged in
return (CFStringGetLength(state) <= 0);
}
return NO;
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioSession setActive:YES error:nil];
NSLog(@"muted? %i", [self deviceIsSilenced]);
...
}
Run Code Online (Sandbox Code Playgroud)
我想当手机上的物理开关被切换时,可能会触发其他一些(更准确的)kAudioSessionProperty事件.有人有主意吗?
顺便说一句,我正在使用AVAudioSessionCategoryAmbient类别和我的[AVAudioSession …
avaudioplayer ×10
iphone ×8
ios ×4
objective-c ×4
audio ×2
ipad ×2
background ×1
caching ×1
cocoa-touch ×1
headphones ×1
ios4 ×1
ios7 ×1
nsdata ×1
silent ×1
swift ×1
uislider ×1
volume ×1