问题
I need to play multiple sound files. I have the following code:
My .h
class:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController : UIViewController {
AVAudioPlayer *musicPlayer;
}
- (IBAction)music1:(id)sender;
- (IBAction)music2:(id)sender;
- (IBAction)music3:(id)sender;
- (IBAction)music4:(id)sender;
- (IBAction)music5:(id)sender;
@end
My .m
file
#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)music1:(id)sender {
if(musicPlayer.isPlaying)
{
[musicPlayer pause];
return;
}
if(musicPlayer == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music1" ofType:@"wav" ]];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[musicPlayer play];
}
- (IBAction) music2:(id)sender {
if(musicPlayer.isPlaying)
{
[musicPlayer pause];
return;
}
if(musicPlayer == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music2" ofType:@"wav" ]];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[musicPlayer play];
}
- (IBAction) music3:(id)sender {
if(musicPlayer.isPlaying)
{
[musicPlayer pause];
return;
}
if(musicPlayer == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music3" ofType:@"wav" ]];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[musicPlayer play];
}
- (IBAction) music4:(id)sender {
if(musicPlayer.isPlaying)
{
[musicPlayer pause];
return;
}
if(musicPlayer == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music4" ofType:@"wav" ]];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[musicPlayer play];
}
- (IBAction) music5:(id)sender {
if(musicPlayer.isPlaying)
{
[musicPlayer pause];
return;
}
if(musicPlayer == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music5" ofType:@"wav" ]];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[musicPlayer play];
}
@end
But this code does not work: only one music file is played. Other file is not.
Also, there is an example:
Music1 playing.
I press the Music2 button -> Music1 end, Music2 start.
How do I need to modfy my code?
回答1:
You need AVAudioPlayer for each sound:
.h:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController : UIViewController {
AVAudioPlayer *musicPlayer1;
AVAudioPlayer *musicPlayer2;
AVAudioPlayer *musicPlayer3;
AVAudioPlayer *musicPlayer4;
AVAudioPlayer *musicPlayer5;
NSMutableArray playingMusicArray;
}
- (IBAction)music1:(id)sender;
- (IBAction)music2:(id)sender;
- (IBAction)music3:(id)sender;
- (IBAction)music4:(id)sender;
- (IBAction)music5:(id)sender;
@end
.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
playingMusicArray = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)stopPlayingMusic {
for(AVAudioPlayer *audioPlayer in playingMusicArray)
{
[audioPlayer pause];
[playingMusicArray removeObject:audioPlayer];
}
}
- (IBAction)music1:(id)sender {
[self stopPlayingMusic];
if(musicPlayer1 == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music1" ofType:@"wav" ]];
musicPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[playingMusicArray addObject:musicPlayer1];
[musicPlayer1 play];
}
- (IBAction) music2:(id)sender {
[self stopPlayingMusic];
if(musicPlayer2 == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music2" ofType:@"wav" ]];
musicPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[playingMusicArray addObject:musicPlayer2];
[musicPlayer2 play];
}
- (IBAction) music3:(id)sender {
[self stopPlayingMusic];
if(musicPlayer3 == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music3" ofType:@"wav" ]];
musicPlayer3 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[playingMusicArray addObject:musicPlayer3];
[musicPlayer3 play];
}
- (IBAction) music4:(id)sender {
[self stopPlayingMusic];
if(musicPlayer4 == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music4" ofType:@"wav" ]];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[playingMusicArray addObject:musicPlayer4];
[musicPlayer4 play];
}
- (IBAction) music5:(id)sender {
[self stopPlayingMusic];
if(musicPlayer5 == nil)
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music5" ofType:@"wav" ]];
musicPlayer5 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
}
[playingMusicArray addObject:musicPlayer5];
[musicPlayer5 play];
}
@end
来源:https://stackoverflow.com/questions/14912670/how-to-play-multiple-sound