0

I have a sound playing on buttons pushed from the main view controller which works fine. On the next view controller I also want a sound to play on buttons pushed but I'm not getting any sound. I set the two .h and .m files the same. What could my problem be? Thank you for any help.

my .m file:

#import "AboutView.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>

@interface AboutView ()

@end

@implementation AboutView
@synthesize support;
@synthesize facebook;
@synthesize kmbdev;
@synthesize back;
@synthesize player2;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNi{
if (self) {
    // Custom initialization

}
return self;
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"sound1" ofType: @"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

self.player2 = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: NULL];
player2.volume = .5;

[player2 prepareToPlay];
}

My .h file:

#import "ViewController.h"
#import <MessageUI/MessageUI.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>

@class ViewController;

@interface AboutView : UIViewController <MFMailComposeViewControllerDelegate,        AVAudioPlayerDelegate>{
AVAudioPlayer *player2;
}

@property (strong, nonatomic) IBOutlet UIButton *back;
- (IBAction)back:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *support;
@property (strong, nonatomic) IBOutlet UIButton *facebook;
@property (strong, nonatomic) IBOutlet UIButton *kmbdev;
- (IBAction)email:(id)sender;
@property (strong, nonatomic) AVAudioPlayer *player2;

@end

I have [player2 play]; at my prepare for segue and IBaction as I do on the main view controller.

4

1 回答 1

1

Add this code in ViewDidLoad method of 2nd controller:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"sound1" ofType: @"wav"];
if([[NSFileManager defaultManager] fileExistsAtPath:soundFilePath)
{
  NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
  if(fileURL)
  {
   self.player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
   self.player2.delegate = self;
   self.player2.volume = 0.5f;
   [self.player2. play];
  }
}
else {
  NSLog(@"File DoesNot Exists");
}
于 2012-09-01T04:35:26.007 回答