Кнопка остановки аудиоконтроллера - PullRequest
0 голосов
/ 01 августа 2011

Я пытаюсь создать аудиоконтроллер, но кнопка остановки не работает. Но я не знаю почему. Как это вызвано и как я могу решить это?

.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface myprojectViewController : UIViewController {

    AVAudioPlayer* theAudio;

}
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;

@end

.m

- (IBAction)start:(id)sender{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"LE" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

- (IBAction)stop:(id)sender{
    [theAudio stop];
}

1 Ответ

0 голосов
/ 01 августа 2011

'theAudio' - это локальная переменная в вашем методе start ().Вы объявили ее как локальную переменную, поэтому она не входит в область действия вашего метода stop (), и у вас есть недопустимая ссылка.Вам нужно использовать переменную класса 'theAudio.Изменить:

AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

на

self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...