-1

ここに私の 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

私の 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

「Supporting Files」フォルダには、再生したいサウンド ファイルの名前を含む文字列の配列があり、「Supporting Files」フォルダには、サウンド ファイルを含む「Sounds」という名前のフォルダがあります。

すべてのボタンで同じサウンドが再生されます。誰かが洞察を提供してくれませんか。ありがとう!

4

3 に答える 3

2

問題は次の行にあると思います playSound:

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

これは、「buttonPressed.tag -2」と「buttonPressed.tag -3」を使用して、playSound2 と playSound3 で繰り返されます。

buttonPressed.tags が 1、2、および 3 に設定されている場合、"buttonPressed.tag -X" が 0 に評価されるたびに、配列内の最初のファイルのサウンドが再生されます。

于 2013-06-07T18:45:42.593 に答える
2

同じタスクを実行するためにコードを繰り返しています。

すべてのボタンを 1 つのメソッドに追加します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 に答える