【发布时间】:2012-11-22 13:15:09
【问题描述】:
我一直在尝试创建一个应用程序,当我按下一个按钮时它会播放声音,但是当我再次按下同一个按钮时它会播放不同的声音我不介意它是否一直完全随机播放,或者如果它每次播放不同的声音但每次都播放相同的顺序希望这是有道理的家伙
这是我拥有的所有代码:
.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
【问题讨论】:
-
拜托我似乎找不到问题
标签: random avfoundation avaudioplayer xcode4.5 audio-player