更新到 ARC Obj-c 后声音不播放
Sound Not Playing after updating to ARC Obj-c
我最近用 Automatic Reference Counting (ARC)
更新了我的项目,它破坏了我的 playAudio 方法(之前工作完美)。我是 ARC
的新手,但我很确定这是由于保留计数或系统立即释放对象,因此您根本听不到声音播放。我创建了一个 AudioPlayer
的 iVar 并尝试保留我自己的个人保留计数,如下面的问题链接所示。我通过第一个 alloc]init 从另一个 class 中调出 playSystemSound:
;然后 [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;
}
}
}
}
如果没有强引用保留它,您的 AudioSession 将在方法 returns 后立即消失。
我最近用 Automatic Reference Counting (ARC)
更新了我的项目,它破坏了我的 playAudio 方法(之前工作完美)。我是 ARC
的新手,但我很确定这是由于保留计数或系统立即释放对象,因此您根本听不到声音播放。我创建了一个 AudioPlayer
的 iVar 并尝试保留我自己的个人保留计数,如下面的问题链接所示。我通过第一个 alloc]init 从另一个 class 中调出 playSystemSound:
;然后 [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;
}
}
}
}
如果没有强引用保留它,您的 AudioSession 将在方法 returns 后立即消失。