ページ

2011年5月29日日曜日

iPhoneシミュレータでSoundを再生する with Xcode





mySoundPlayerViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface mySoundPlayerViewController : UIViewController {
 AVAudioPlayer *theSound;
}

@property(nonatomic,retain) AVAudioPlayer *theSound;

-(IBAction) sound1;
-(IBAction) sound2;
-(IBAction) sound3;
-(IBAction) start;
-(IBAction) stop;
-(IBAction) pause;

@end


mySoundPlayerViewController.m
#import "mySoundPlayerViewController.h"

@implementation mySoundPlayerViewController
@synthesize theSound;

-(IBAction) sound1{
 NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
 if(theSound) [ theSound release];
 theSound = [[AVAudioPlayer alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
 [theSound play];
}

-(IBAction) sound2{
 NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"mp3"];
 if(theSound) [ theSound release];
 theSound = [[AVAudioPlayer alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
 [theSound play];
}

-(IBAction) sound3{
 NSString *path = [[NSBundle mainBundle] pathForResource:@"sound3" ofType:@"mp3"];
 if(theSound) [ theSound release];
 theSound = [[AVAudioPlayer alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
 [theSound play];
}

-(IBAction) start{
 [theSound play];
}

-(IBAction) stop{
 [theSound stop];
}

-(IBAction) pause{
 [theSound pause];
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

0 件のコメント:

コメントを投稿