我正在创建一个自定义对象,它目前非常简单。下面你会找到我的.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代码= 1,这是第一行中断
if([myPlayer isPlaying])...
我熟悉c ++内存管理,但完全是Objective-C的新手。有人可以指出我非常明显的错过。
谢谢。
答案 0 :(得分:0)
自己解决了。在Object.h里面我需要:
self = [super init];
.
.
.
return self;
关于投票。我认为在发布之前我已经彻底研究了我的问题。我还以为我提供了所有必要的信息来帮助解决问题。你能否至少评论一下投票的原因,以便我知道下次如何避免它?谢谢。