iOSで音を鳴らすシンプルなアプリ.
StoryBoardを貼らないとあまり意味ないかもしれないけどメモ.
画像ファイルやサウンドファイルは
Supporting Filesに入っている前提.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UIImageView *myImageView;
}
@property AVAudioPlayer *sound;
-(IBAction)tapBtn;
@end
ViewController.m
#include <AVFoundation/AVFoundation.h>
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)tapBtn {
myImageView.image = [UIImage imageNamed:@"IMG_0976.jpg"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"moon_river" ofType:@"m4r"];
NSURL *url = [NSURL fileURLWithPath:path];
self.sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[self.sound play];
}
@end