【发布时间】:2020-05-15 15:37:43
【问题描述】:
我最近用Automatic Reference Counting (ARC) 更新了我的项目,它破坏了我的 playAudio 方法(之前工作完美)。我是ARC 的新手,但我很确定这是由于保留计数或系统立即释放对象,所以你根本听不到声音播放。我创建了AudioPlayer 的 iVar 并尝试保持我自己的个人保留计数,如下面的问题链接所示。我从另一个类中调用 playSystemSound:,首先是 alloc]init;然后[class playSystemSound:1002]; 如果有人能指出我做错了什么,将不胜感激。
AVAudioPlayer not playing any sound
@property (strong, nonatomic) AVAudioPlayer *soundToPlay;
-(void)playsystemSound:(long long)eventType{
NSString *path = [NSString stringWithFormat:@"/User/Library/Preferences/com.idd.iconomaticprefs.plist"];
NSMutableDictionary* savedDict = [NSMutableDictionary dictionary];
[savedDict addEntriesFromDictionary:[NSDictionary dictionaryWithContentsOfFile:path]];
NSMutableArray *activeThemesArray = [savedDict valueForKey:@"ActiveThemes"];
if ([activeThemesArray count]) {
NSString *soundURL;
for (NSString *name in activeThemesArray) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds",name];
if ([[NSFileManager defaultManager]fileExistsAtPath:soundURL]) {
//UISounds avilable in theme determine event type
if (eventType == 1002) {
//respring?
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/lock.caf",name];
}
if (eventType == 1003) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/Anticipate.caf",name];
}
if (eventType == 1004) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/key_press_click.caf",name];
}
if (eventType == 1008) {
soundURL = [NSString stringWithFormat:@"/Library/Themes/%@/UISounds/photoShutter.caf",name];
}
// setup session correctly
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audiosession setActive:YES error:nil];
// play sound
NSURL *url = [NSURL fileURLWithPath:soundURL];
self.soundToPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.soundToPlay prepareToPlay];
[self.soundToPlay play];
break;
}
}
}
}
【问题讨论】:
-
“我很确定这是由于保留计数或系统立即释放对象”可能是,但是您是否使用调试器和变量列表遍历代码以查看发生了什么?如果
initWithContentsOfURL失败,你会得到一个零玩家。无需猜测,使用调试器。
标签: ios objective-c automatic-ref-counting