开发者

simple example, but I don't understand why there are no leaks

- (IBAction) btnFire:(id)sender {

    NSString *path = 开发者_运维问答[NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath],   @"/gunShot.wav"];

    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

    AVPlayer *shotSound = [[AVPlayer alloc] initWithURL:filePath];

    [shotSound play];
}

If I release shotSound here, it will not not play. At the same time there are no leaks.

Why does Instruments indicate no leaks? How to release shotSound?


As you will need sound again and again when button is pressed so rather than initiliazing it in button each time you should declare it at class level and either you can initialize it in viewDidLoad method, or you can do this also

- (IBAction) btnFire:(id)sender {

 if(shotSound==nil)
{
        NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath],   @"/gunShot.wav"];

        NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

       shotSound = [[AVPlayer alloc] initWithURL:filePath];
}

        [shotSound play];

}

release it in dealloc. If you just wish to have sound on this action than you can use AVAudioPlayer.

Sometimes instrument's leak is not able to detect it, in those cases you can use build and analyse tool. Also, as you said if you release player object then app crashes may be cause it audio is still playing and you release the player(not sure).


You can try

AVPlayer *shotSound = [AVPlayer playerWithURL:filePath];

Note: This returns a new player to play a single audiovisual resource referenced by a given URL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜