CALayers和音频

时间:2012-07-04 01:49:59

标签: ios calayer avaudioplayer

我有一个带有图像的CALayer,当我触摸它时,我有动画效果,它很有效。我现在要做的还有一个声音片段,当你按下相同的图像时播放。我发现这很棘手,因为CALayer来自名为BounceView的UIView类。我在MainViewController上创建了一个BounceView实例。如果我没有使用正确的术语,我道歉。我是否需要将我的音频代码放在MainViewController中,然后使用委托允许BounceView在MainViewController上触发方法?非常感谢任何帮助或方向。

MainViewController.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>


@interface MainViewController : UIViewController <AVAudioPlayerDelegate>


@property (nonatomic, retain) IBOutlet UIView *BounceView;
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;


- (IBAction)playAudio:(id)sender;

@end

MainViewController.m

#import "MainViewController.h"
#import "BounceView.h"

@interface MainViewController ()

@end

@implementation MainViewController 
@synthesize BounceView;
@synthesize audioPlayer;

- (void)viewDidLoad
{
    [super viewDidLoad];

  //Setup the audio player
  NSURL *noSoundFileURL=[NSURL fileURLWithPath:
                       [[NSBundle mainBundle] 
                        pathForResource:@"InTheMood" ofType:@"mp3"]];
  self.audioPlayer =  [[AVAudioPlayer alloc] 
                     initWithContentsOfURL:noSoundFileURL error:nil];

  // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{  
  [super viewDidUnload];
  // Release any retained subviews of the main view.
}

- (void)loadView
{
  NSLog(@"loadView");
  // Create a view
  CGRect frame = [[UIScreen mainScreen] bounds];
  BounceView *v = [[BounceView alloc] initWithFrame:frame];

  // Set it as *the* view of this view controller
  [self setView:v];    
}

- (IBAction)playAudio:(id)sender {
  //    self.audioPlayer.delegate=self;
  [self.audioPlayer play];
}


@end

BounceView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import "MainViewController.h"

@interface BounceView : UIView
{
   CALayer *myLayer;  
}

@end

BounceView.m 如果需要,我将提供此代码,但我不想重载代码。在这里,我创建新的图层打开(alloc init),给它大小,位置,创建UIImage,获取底层的CGImage&amp;把它放在图层上然后制作视图图层的子图层。

再次,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

由于BounceView是一个UIView类而不是UIViewController,我会让播放音频到MainViewController。