更新到ARC Obj-c后未播放声音

问题描述 投票:0回答:1

我最近用Automatic Reference Counting (ARC)更新了我的项目,它破坏了我的playAudio方法(以前工作得很完美)。我是ARC的新手,但可以肯定的是,这是由于保留计数或系统立即释放对象所致,所以您根本听不到声音播放。我创建了一个AudioPlayer的iVar,并尝试保留我自己的个人保留数,如下所示。我通过另一个alloc] init从另一个类中调用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;
        }
    }
}

}
ios objective-c automatic-ref-counting
1个回答
1
投票

如果没有强引用保留它,则该方法返回后,您的AudioSession将消失。

© www.soinside.com 2019 - 2024. All rights reserved.