我一直在尝试创建一个应用程序,当我按下按钮时它会播放声音,但是当我再次按下同一个按钮时,它会播放不同的声音我不介意它是否一直完全随机播放或播放一次不同的声音,但每次都相同的顺序希望这有意义伙计们
这是我拥有的所有代码:
。H
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@class AVAudioPlayer;
@interface ViewController : UIViewController
-(IBAction)PlayRandomSound;
@property (nonatomic, retain) AVAudioPlayer *soundPlayer;
@end
.m
#import "ViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize soundPlayer = _soundPlayer;
-(IBAction)PlayRandomSound{
int randomNumber = arc4random() % 8 + 1;
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Sound%02d", randomNumber] ofType:@"mp3"]];
_soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
[_soundPlayer prepareToPlay];
[_soundPlayer play];
NSLog(@"randomNumber is %d", randomNumber);
NSLog(@"tmpFilename is %@", soundURL);
}
这是我在图像中的错误
我还插入了 AVFoundation.Framework 和 AudioToolbox.Frame