在音板应用上播放随机声音

时间:2012-11-22 13:15:09

标签: random avfoundation avaudioplayer xcode4.5 audio-player

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

这是我的所有代码:

·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);
}

这是我在图片中出现的错误

enter image description here

enter image description here

我还插入了AVFoundation.Framework和AudioToolbox.Frame

1 个答案:

答案 0 :(得分:1)

你有很多问题。很难回答你的实际问题,因为你有许多小问题正在阻碍。

给你一些帮助:

这是不正确的:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@class AVAudioPlayer;

@interface ViewController : UIViewController

作为您发布的图片,即使Xcode也在努力为您提供帮助。 @class转发声明位置错误。尝试将其放在#import语句之后。因此,你的声音播放器没有正确创建,你会收到其他警告。

您需要修复所有错误,然后修复所有警告并继续执行此操作,直到它完全构建。

相关问题