在Soundboard应用程序上播放随机声音 [英] playing Random sounds on a soundboard app
问题描述
我一直在尝试创建一个应用程序,当我按下一个按钮时它会播放声音,但是当我再次按下相同的按钮时它会播放不同的声音,我不介意它是否一直在随机播放,或者如果一次播放的声音不同但每次的顺序相同,希望这对大家有意义
I have been trying to create a app that when I press a button it plays a sound but then when I press the same button again it plays a different sound I don't mind if it plays it totally random all the time or if it plays a different sound a time but the same order every time hope this make sense guys
这是我拥有的所有代码:
Here's all the code I have:
.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);
}
这是我在图片中出现的错误
Here the errors I have in images
我还插入了AVFoundation.Framework和AudioToolbox.Frame
I have also inserted the AVFoundation.Framework and AudioToolbox.Frame
推荐答案
与此相关的问题很多.很难回答您的实际问题,因为您遇到了很多小问题.
You have many problems with this. It's hard to answer your actual question because you have lots of little problems that are getting in the way.
为您提供帮助:
这是不正确的:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@class AVAudioPlayer;
@interface ViewController : UIViewController
作为您发布的图像,即使Xcode也在尝试为您提供帮助. @class
正向声明在错误的位置.尝试将其放在#import
语句之后.因此,您的声音播放器无法正确创建,并且您收到其他警告.
As the image you have posted, even Xcode is trying to help you. The @class
forward declaration is in the wrong place. Try putting it just after the #import
statements. Because of this your soundplayer isn't being created properly and you are getting other warnings.
您需要先修复所有错误,然后修复所有警告,并继续进行操作,直到其构建干净为止.
You need to fix all the errors and then all the warnings and keep doing this until it builds cleanly.
这篇关于在Soundboard应用程序上播放随机声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!