0

ボタンを押すと音が鳴るアプリを作成しようとしていますが、同じボタンをもう一度押すと別の音が鳴ります毎回違う音だけど順番はいつも同じ

ここに私が持っているすべてのコードがあります:

.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 も挿入しました。

4

1 に答える 1

1

これには多くの問題があります。邪魔になっている小さな問題がたくさんあるので、実際の質問に答えるのは難しいです。

あなたにいくつかの助けを与えるために:

これは正しくありません:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@class AVAudioPlayer;

@interface ViewController : UIViewController

あなたが投稿した画像のように、Xcode でさえあなたを助けようとしています。@class前方宣言が間違った場所にあります。#importステートメントの直後に置いてみてください。このため、サウンドプレーヤーが適切に作成されておらず、他の警告が表示されています。

すべてのエラーを修正してから、すべての警告を修正し、正常にビルドされるまでこれを続ける必要があります。

于 2012-11-28T16:58:07.867 に答える