我的应用程序在模拟器中完美打开,但是当我按下按钮进行播放时,没有声音播放。按钮似乎被按下,但没有播放音频。是我的代码或模拟器的问题吗?
这是我的ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate> {
}
- (IBAction)Whaddup;
- (IBAction)Speak;
- (IBAction)Hail;
@end
和我的ViewController.m
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)Whaddup {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Whaddup" ofType:@"wav"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (IBAction)Hail {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Hail" ofType:@"wav"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (IBAction)Speak {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Speak" ofType:@"wav"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
有没有人知道为什么这不起作用?我将我的三个按钮与我的急救人员联系在一起,每个按钮使用“Touch Up Inside”。
答案 0 :(得分:0)
尝试执行以下操作: 您在项目中创建一个名为“Resources”的文件夹,并添加文件音乐“test.mp3”,例如: 并在.h文件中:
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@property(retain,nonatomic) AVAudioPlayer * audioPlayer;// you add this variable
和.m文件
@synthesize audioPlayer;
-(void) readMP3{
if (![audioPlayer isPlaying]) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath],@"test.mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
}
}
和
-(void) stopReading{
[audioPlayer stop];
}
我希望能帮到你