Objective c 音频播放器未在第二视图控制器上播放
我有一个声音播放按钮按下从主视图控制器,这工作良好。在下一个视图控制器上,我还希望在按下的按钮上播放声音,但我没有听到任何声音。我将两个.h和.m文件设置为相同。我的问题是什么?谢谢你的帮助 我的.m文件:Objective c 音频播放器未在第二视图控制器上播放,objective-c,ios,cocoa-touch,avaudioplayer,Objective C,Ios,Cocoa Touch,Avaudioplayer,我有一个声音播放按钮按下从主视图控制器,这工作良好。在下一个视图控制器上,我还希望在按下的按钮上播放声音,但我没有听到任何声音。我将两个.h和.m文件设置为相同。我的问题是什么?谢谢你的帮助 我的.m文件: #import "AboutView.h" #import <AVFoundation/AVFoundation.h> #import <CoreAudio/CoreAudioTypes.h> @interface AboutView () @end @impl
#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];
}
#导入“AboutView.h”
#进口
#进口
@关于视图()的接口
@结束
@关于视图的实现
@综合保障;
@综合facebook;
@合成kmbdev;
@合成背部;
@合成player2;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNi{
如果(自我){
//自定义初始化
}
回归自我;
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
NSString*soundFilePath=[[NSBundle mainBundle]pathForResource:@“sound1”类型:@“wav”];
NSURL*fileURL=[[NSURL alloc]initFileURLWithPath:soundFilePath];
self.player2=[[AVAudioPlayer alloc]initWithContentsOfURL:fileURL错误:NULL];
player2.volume=0.5;
[播放者2准备播放];
}
我的.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
#导入“ViewController.h”
#进口
#进口
#进口
@类视图控制器;
@关于视图的界面:UIViewController{
AVAudioPlayer*播放器2;
}
@属性(强,非原子)IBUIButton*返回;
-(iAction)返回:(id)发送方;
@属性(强、非原子)IBUIButton*支持;
@属性(强,非原子)IBUIButton*facebook;
@属性(强,非原子)ibuibutton*kmbdev;
-(iAction)电子邮件:(id)发件人;
@属性(强,非原子)AVAudioPlayer*player2;
@结束
我有[玩家2玩];在我准备segue和IBaction时,就像我在主视图控制器上所做的那样。在第二个控制器的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");
}
您是为iOS还是OSX开发的?