0

As the title describes I've been able to add sound when button is pressed, but I was wondering if there was a way to also add an image change to the main UIImageView when the same button is pressed. Is there any way to stick the picture code in where the "playFaceOfAudioType" method is presented?

Here's my code so far:

#import "FourthViewController.h"

@interface FourthViewController ()

@end

@implementation FourthViewController

@synthesize audioPlayer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)facesAudioAction:(id)sender {
    UIButton *btn=(UIButton *)sender;
    [self playFaceAudioOfType:btn.tag];//Tag for button is set on the xib..

}




-(void)playFaceAudioOfType:(int)type{

    [self stopAudio];


    NSString *sound=@"";

    switch (type) {
        case 1:
            sound=@"1";
            break;

        case 2:
            sound=@"2";
            break;

        case 3:
            sound=@"3";
            break;

        case 4:
            sound=@"4";
            break;

        case 5:
            sound=@"5";
            break;

        default:
            break;
    }


    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:sound
                                         ofType:@"mp3"]];



    NSError *error;
    if(url){
        audioPlayer = [[AVAudioPlayer alloc]
                       initWithContentsOfURL:url
                       error:&error];



        if (error)
        {
            NSLog(@"Error in audioPlayer: %@",
                  [error localizedDescription]);
        } else {
            audioPlayer.delegate = self;
            [audioPlayer prepareToPlay];
        }

        [audioPlayer play];
    }


}




-(void)stopAudio{

    if(audioPlayer && [audioPlayer isPlaying]){
        [audioPlayer stop];
        audioPlayer=nil;
    }


}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I understand from looking around that usually this is done by doing an if / else if type of approach but was wondering if it was possible to include it along with the switch statement bit.

1
  • Not an Xcode question. Also, that switch should be changed to sound = [NSString stringWithFormat:@"%d.mp3", type]...
    – user529758
    Jul 22, 2013 at 7:13

2 Answers 2

1

You can use either facesAudioAction or playFaceOfAudioType method for changing image.

Also, you can get rid off switch statement with;

NSString *sound = [NSString stringWithFormat:@"%d", type];

You can set image like this;

NSString *imageName = [NSString stringWithFormat:@"%d.jpg", type];
yourImageView.image = [UIImage imageNamed:imageName];
7
  • Ah thanks. How could I code this if I have images named 0.jpg, 1.jpg, etc.? Edit I set tags on the 5 different buttons, but don't know how to call each of the 5 images based on which button has been pressed.
    – Steee
    Jul 22, 2013 at 7:22
  • Altered my answer accordingly. And your edited question's answer is in your code. You are already getting the sender's tag with type variable. Use it to find which button has been pressed.
    – Samet DEDE
    Jul 22, 2013 at 7:26
  • hmm. I get this error. Sorry. I'm still new to iOS. Should I be putting this bit under playFaceAudioOfType? snag.gy/teG63.jpg
    – Steee
    Jul 22, 2013 at 7:37
  • 1
    type variable is in playFaceAudioOfType so you should move this to playFaceAudioOfType method. Also you should have an UIImageView to set its image. replace UIImageView.image with your UIImageView's instance name.
    – Samet DEDE
    Jul 22, 2013 at 7:59
  • awesome! I got it working, but get this error now: "Unknown class imageView in Interface Builder file." I had to do: -(void)playFaceAudioOfType:(int)type{ NSString *imageName = [NSString stringWithFormat:@"%d.jpg", type]; _imageView.image = [UIImage imageNamed:imageName]; My .h file has: @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    – Steee
    Jul 22, 2013 at 8:08
-1

if you use UIControl's tag, you define NSString *sound = [NSString stringWithFormat:@"%d",type]; in playFaceAudioOfType: function.then you can avoid the switch statement.

And Solution2,you can use setImage:forState: method display button's style, and use button.titleLabel.text save it's song's information(eg. name).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.