LoginSignup
6
7

More than 5 years have passed since last update.

サウンドを再生する簡単なアプリ

Posted at

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
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7