-1

Here is my MainViewController.m

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController 

@synthesize audioPlayer;
@synthesize soundsArray;

-(void)prepareSounds
{
    NSString *filepath= [[NSBundle mainBundle] pathForResource:@"Sounds" ofType:@"plist"];

    self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];

}

- (IBAction)playSound:(id)sender {

    UIButton *buttonPressed = (UIButton *)sender;

    NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -1)];

    NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:file error:nil];

    self.audioPlayer = p;

    [self.audioPlayer play];
}

- (IBAction)playSound2:(id)sender {

    UIButton *buttonPressed = (UIButton *)sender;

    NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -2)];

    NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:file error:nil];

    self.audioPlayer = p;

    [self.audioPlayer play];
}

- (IBAction)playSound3:(id)sender {

    UIButton *buttonPressed = (UIButton *)sender;

    NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -3)];

    NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:file error:nil];

    self.audioPlayer = p;

    [self.audioPlayer play];
}

- (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.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)showInfo:(id)sender
{    
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:controller animated:YES completion:nil];
}

@end

My MainViewController.h

    #import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>


@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {

    AVAudioPlayer *audioPlayer;
    NSArray *soundsArray;
}

@property(nonatomic, retain) AVAudioPlayer *audioPlayer;
@property(nonatomic, retain) NSArray *soundsArray;

-(void)prepareSounds;
- (IBAction)playSound:(id)sender;
- (IBAction)playSound2:(id)sender;
- (IBAction)playSound3:(id)sender;

@end

In the 'Supporting Files' folder I have an array of strings with the name of the sound files I want to play, and in the 'Supporting Files' folder I have a folder named 'Sounds', which contains the sound files.

All of my buttons play the same sound. Can someone please provide some insight. Thanks!

4

3 回答 3

2

我认为问题可能出在playSound这一行:

NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -1)]

在 playSound2 和 playSound3 中重复使用“buttonPressed.tag -2”和“buttonPressed.tag -3”。

如果您的 buttonPressed.tags 设置为 1、2 和 3,那么每次“buttonPressed.tag -X”可能评估为 0,并播放数组中第一个文件的声音。

于 2013-06-07T18:45:42.593 回答
2

您正在重复代码以执行相同的任务。

将所有按钮添加IBAction到单个方法(将其称为playSound

实现如下方法:

- (IBAction)playSound:(UIButton *)sender
{

    NSString *soundName = [soundsArray objectAtIndex:(sender.tag -1)];

    NSString *path      = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file         = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p    = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];

    self.audioPlayer    = p;

    [self.audioPlayer play];
}

无需为每个单独的按钮编写相同的代码。

于 2013-06-07T18:58:01.673 回答
1

已解决:prepareSounds() 从未被调用过。这是工作代码:

    #import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController 

@synthesize audioPlayer;
@synthesize soundsArray;

-(void)prepareSounds
{    
    NSString *filepath= [[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"plist"];

    self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];

}

- (void)stopAudio
{
    if (audioPlayer!= nil) {
        [audioPlayer stop];
        //do some task for changing the Image i.e setting the default image
    }

}

- (IBAction)playSound:(UIButton *)sender
{
    UIButton *btn = (UIButton*)sender;

    NSString *soundName = [soundsArray objectAtIndex:(btn.tag - 1)];

    NSString *path      = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
    NSURL *file         = [[NSURL alloc] initFileURLWithPath:path];

    AVAudioPlayer *p    = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];

    self.audioPlayer    = p;

    if([audioPlayer isPlaying])
    {
        [self stopAudio];
    }

    [self.audioPlayer play];


}


- (void)viewDidLoad
{
    [self prepareSounds];
    [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.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)showInfo:(id)sender
{    
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:controller animated:YES completion:nil];
}

@end
于 2013-06-10T00:18:49.107 回答