【发布时间】:2014-07-23 20:13:05
【问题描述】:
我正在创建一个自定义对象,目前非常简单。您将在下面找到该对象的 .h。
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface LAZMessagePlayer : NSObject <AVAudioPlayerDelegate>
- (void)play:(UIView*)viewThatWasPressed;
- (void)pause;
- (BOOL)isPlaying;
@end
以及对象对应的.m
#import "LAZMessagePlayer.h"
@implementation LAZMessagePlayer {
AVAudioPlayer* myPlayer;
UIView* currentViewPlaying;
}
-(id)init{
NSLog(@"init audio");
NSURL* src = [NSURL fileURLWithPath:[NSString stringWithFormat:@"/Users/Developer/Downloads/317.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError* error;
myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:src error:&error];
}
-(void)play:(UIView*)viewThatWasPressed {
NSLog(@"play");
currentViewPlaying = viewThatWasPressed;
currentViewPlaying.hidden = YES;
[myPlayer play];
}
-(void)pause {
NSLog(@"pause");
currentViewPlaying.hidden = NO;
[myPlayer pause];
}
-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"done");
currentViewPlaying.hidden = NO;
}
-(BOOL)isPlaying {
return myPlayer.playing;
}
@end
在我的 ViewController 内部,我已经在 Controller.m 的顶部声明了一个私有变量,就像这样,
#import "LAZMessagesViewController.h"
@interface LAZMessagesViewController () {
LAZMessagePlayer* myPlayer;
}
在我的 viewDidLoad 中,我分配/初始化 myPlayer。
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBarExtensionViewDelegate = self;
// Do any additional setup after loading the view.
myPlayer = [[LAZMessagePlayer alloc] init];
}
稍后在我的 Tap 处理程序中,我第一次尝试引用 myPlayer 时收到 BAD ACCESS code = 1,这是中断的第一行
if([myPlayer isPlaying])...
我熟悉 c++ 内存管理,但对 Objective-C 完全陌生。谁能指出我明显的失误。
谢谢。
【问题讨论】:
标签: ios objective-c memory-management