objective-c - AudioPlayer 不在第二个 View Controller 上播放

标签 objective-c ios cocoa-touch avaudioplayer

我在从主视图 Controller 按下的按钮上播放了声音,效果很好。在下一个 View Controller 上,我还希望在按下的按钮上播放声音,但我没有收到任何声音。我将两个 .h 和 .m 文件设置为相同。我的问题可能是什么?感谢您的帮助。

我的 .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

我有[玩家2玩];在我准备 segue 和 IBaction 时,就像我在主视图 Controller 上所做的那样。

最佳答案

在第二个 Controller 的 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");
}

关于objective-c - AudioPlayer 不在第二个 View Controller 上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222360/

相关文章:

iphone - 应用程序只对肖像启用,但 UIImagePickerController 在 iOS6 中旋转

ios - 将图像上传到解析

ios - UITableView - 多种编辑样式

objective-c - 如何在您的应用程序中禁用 iOS 通知中心?

objective-c - 将应用程序配置保存在 plist 文件中的最佳实践

ios - 将小的 UIImage 放在大的 UIButton 中

iphone - 如何将 subview 从其父 View 的父 View 带到顶部

ios - 如何从 GameScene 中的 ViewController 调用方法

iphone - transitionWithView 和 animateWithDuration 的问题

iphone - 为什么这两个 UIView 不会使用我的动画淡入和淡出?