[英]Expected identifier error
因此,如果有人可以幫助我,我只會在- (IBAction)playAudio:(id)sender
下收到一個錯誤,我將只在按下按鈕時嘗試播放聲音。
#import "SecondViewController.h"
@interface SecondViewController ()
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"wav"];
NSURL *audioPath = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
@end
@implementation SecondViewController{
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)ButtonPressed:(UIButton *)sender forEvent:(UIEvent *)event {
}
- (IBAction)Button {AudioServicesPlaySystemSound(soundID);
}
- (IBAction)playAudio:(id)sender {
}
@end
方法定義不應在@interface
部分中; 只是聲明 :
@interface SecondViewController ()
- (IBAction)playAudio:(id)sender;
@end
然后通過以下更改將定義移至@implementation
部分:
您將標識符(變量名) audioPath
兩次用於不同的對象。 將第二個更改為audioURL
或諸如此類:
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"wav"];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath]; // HERE
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
您是否要在頭文件(.h)中實現(IBAction)playAudio:(id)sender {}?
您可以在.h中聲明playAudio IBAction,但在.m中實現它
您的文件應如下所示:
@interface SecondViewController ()
- (IBAction)playAudio:(id)sender;
@end
@implementation SecondViewController{
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)ButtonPressed:(UIButton *)sender forEvent:(UIEvent *)event {
}
- (IBAction)Button {AudioServicesPlaySystemSound(soundID);
}
- (IBAction)playAudio:(id)sender {
AVAudioPlayer *audioPlayer;
NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"ding" ofType:@"wav"];
NSURL *audioPath = [NSURL fileURLWithPath:audioPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
[audioPlayer play];
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.