まず、 ViewController.hを
に変更してください。
#import <UIKit/UIKit.h>
@class AVAudioPlayer;
@interface ViewController : UIViewController
-(IBAction)PlayRandomSound;
@property (nonatomic, retain) AVAudioPlayer *soundPlayer;
@end
と
#import "ViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
@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);
}
編集するViewController.mの最初の行:私は後でこのコードは、小さな漏れを持っているので、あなたは、ARCを使用していないことに気づきました。しかし、それは開始のために行います。おそらく、ViewControllerの作成時に_soundPlayerをnilに設定して、後でそれをチェックしてください。もしそれがリリースされていなければ、新しいものを作成してください。または、新しいプロジェクトの場合は、ARCに切り替えることを検討することもできます。
あなたはファイルパス/名前のログを記録しようとしましたか、これらの正しさはどのような例外でもあります。 –
%dを%iに変更しようとしました。 – Shubhank
%dは問題ありません。これらは同等です(疑問に思っている場合は%dは "倍"ではありません)。 –