リソースファイルから音楽を再生。
「再生」「一時停止」「停止」ができればOK。生のオーディオデータを加工したりはしない。という条件ならば、AVFoundationフレームワークのAVAudioPlayerを用いると簡便。
まずは、SingleApplicationでプロジェクトを作成。プロジェクト名はSampleAVAudioPlayerにしてみた。


ViewController.hにAVFoundationフレームワークをimportする。

ボタンとViewController.m間に、Actionを作成してつなげる。

オーディオプレイヤーを現すpropertyをViewController.mに加える。

オーディオプレイヤーにオーディオファイルをセットするコード。

各ボタンのアクションに、「再生」「一時停止」「停止」機能を加える。

Playボタンを押すと、オーディオファイルの再生。Pauseボタンで一時停止。Stopボタンで停止し、オーディファイルの再生位置を冒頭に戻します。
手を加えたソースコードは以下のようになります。
ViewController.h
// // ViewController.h // sampleAVAudioPlayer #import#import @interface ViewController : UIViewController @end
ViewController.m
//
// ViewController.m
// sampleAVAudioPlayer
#import "ViewController.h"
@interface ViewController ()
@property AVAudioPlayer* player;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// リソースファイルをAVAudioPlayerにセット。
NSString *path = [[NSBundle mainBundle] pathForResource:@"testSound" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath: path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:nil];
}
- (IBAction)pushPlay:(id)sender {
if(!self.player.isPlaying){
[self.player play];
}
}
- (IBAction)pushPause:(id)sender {
if(self.player.isPlaying){
[self.player pause];
}
}
- (IBAction)pushStop:(id)sender {
if(self.player.isPlaying){
[self.player stop];
self.player.currentTime = 0.0;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end


