2012-08-31 107 views
0

我有一個聲音播放按鈕從主視圖控制器工作正常。在下一個視圖控制器上,我還希望在按下的按鈕上播放聲音,但是我沒有收到任何聲音。我將這兩個.h和.m文件設置爲相同。我的問題是什麼?感謝您的任何幫助。AudioPlayer沒有在第二個視圖控制器上播放

我.m文件:

#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]; 
} 

我.h文件中:

#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 

我有[player2播放];在我爲主視圖控制器做準備時,我準備了segue和IBaction。

+0

你開發iOS或OSX? –

+0

我正在爲iOS開發。 – kmb

回答

1

在第二控制器的viewDidLoad方法添加以下代碼:

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"); 
} 
+0

非常感謝。它效果很好。 – kmb

相關問題