【发布时间】:2011-02-22 18:31:47
【问题描述】:
我想使用 IBAction 和 AVAudioPlayer 播放多个音频文件 (.WAV)。不幸的是,声音会播放,但如果我多次播放声音,我的应用程序就会崩溃。你能帮帮我吗?
这是我的代码。
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface ViewController : UIViewController <AVAudioPlayerDelegate>
{
NSString *Path;
}
- (IBAction)Sound1;
- (IBAction)Sound2;
- (IBAction)Sound3;
- (IBAction)Sound4;
@end
ViewController.m
#import <AVFoundation/AVAudioPlayer.h>
#import "ViewController.h"
@implementation ViewController
AVAudioPlayer *Media;
- (IBAction)Sound1
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (IBAction)Sound2
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound2" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (IBAction)Sound3
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound3" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (IBAction)Sound4
{
Path = [[NSBundle mainBundle] pathForResource:@"Sound4" ofType:@"wav"];
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[player release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[Media Release];
[super dealloc];
}
@end
【问题讨论】:
标签: iphone objective-c xcode memory-management avaudioplayer