0

我正在尝试继承 UIButton。如果单击将播放声音,按比例放大。并且当完成播放声音时,声音会恢复到原始状态。一切正常,除了图像没有显示。标题和背景确实放大了,并且正在播放声音。有人提示吗? 声音按钮.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface soundButton : UIButton <AVAudioRecorderDelegate, AVAudioPlayerDelegate>{
    BOOL                isPlaying;
    AVAudioPlayer       *audioPlayer;
}
@property (nonatomic)BOOL       isPlaying;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
- (id)initWithFrame:(CGRect)frame;
-(void)loadSound:(NSString*)audiofile;
-(void)touchUpInside;
@end

声音按钮.m

#import "soundButton.h"
@implementation soundButton
@synthesize isPlaying;
@synthesize audioPlayer;
void audioRouteChangeListenerCallback (
                                   void *inUserData,
                                   AudioSessionPropertyID inPropertyID,
                                   UInt32 inPropertyValueSize,
                                   id *inPropertyValue);

-(BOOL)isPlaying{
return audioPlayer.isPlaying;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    [self addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
    UIImage *image = [UIImage imageNamed:@"kers.png"];
    [self setBackgroundImage:image forState:UIControlStateNormal];

}
return self;
}
-(void)loadSound:(NSString*)audiofile{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:audiofile
                                     ofType:nil]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",
          [error localizedDescription]);
} else {
    audioPlayer.delegate = self;
    [audioPlayer setVolume:1.0];
    [audioPlayer prepareToPlay];
    audioPlayer.currentTime = 0;
 }
 }

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
   [audioPlayer setCurrentTime:0];
self.transform = CGAffineTransformMakeScale(1.5,1.5);
self.alpha = 1.0f;
[UIView beginAnimations:@"fish" context:nil];
[UIView setAnimationDuration:1];
self.transform = CGAffineTransformMakeScale(1.0,1.0);
self.alpha = 1.0f;
[UIView commitAnimations];

self.isPlaying = NO;
 }

-(void)touchUpInside{
//Check if something is playing If YES=>stop it
if (self.isPlaying) {
    [audioPlayer stop];
    [audioPlayer setCurrentTime:0];
} else {

    self.transform = CGAffineTransformMakeScale(1.0,1.0);
    self.alpha = 1.0f;
    [UIView beginAnimations:@"fish" context:nil];
    [UIView setAnimationDuration:1];
    self.transform = CGAffineTransformMakeScale(1.5,1.5);
    self.alpha = 1.0f;
    [UIView commitAnimations];
}


// Create an asynchronous background queue
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperationWithBlock:
         ^{
                 [audioPlayer play];
        }];
}
@end

在 viewController 中的 ViewdidLoad 中实现:

UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"cherry.png"];
fishButton = [[soundButton alloc] initWithFrame:CGRectMake(323, 312, 123, 123)];

[fishButton setImage:image forState:UIControlStateNormal];
[fishButton loadSound:@"chime1.mp3"];
[fishButton setTitle:@"fish" forState:UIControlStateNormal];
[fishButton setBackgroundColor:[UIColor yellowColor]];
[fishButton setShowsTouchWhenHighlighted:YES];

[self.view addSubview:fishButton];
4

1 回答 1

1

替换这个

UIImage *image = [[UIImage alloc]initWithContentsOfFile:@"cherry.png"];

有了这个

UIImage *image = [UIImage imageNamed:@"cherry.png"];

或者如果你真的需要initWithContentsOfFile使用 URL 链接,比如

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cherry" ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:filePath];
于 2012-09-02T18:52:28.810 回答