在音板应用程序上播放随机声音 [英] playing Random sounds on a soundboard app

查看:25
本文介绍了在音板应用程序上播放随机声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个应用程序,当我按下按钮时,它会播放声音,但是当我再次按下同一个按钮时,它会播放不同的声音,我不介意它是否一直完全随机播放,或者如果它一次播放不同的声音但每次播放相同的顺序希望这是有道理的家伙

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.

这篇关于在音板应用程序上播放随机声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆