ersin
ersin

Reputation: 11

Play sounds with displayed images one by one

I have ten image files and sound files, and when I display "a.png" I want to play "a.mp3". Then when I click a "Next" button, display "b.png" and play "b.mp3". How can I solve this?

Upvotes: 0

Views: 199

Answers (2)

ersin
ersin

Reputation: 11

-(void) viewDidLoad;{


sounds = [[NSArray alloc]initWithObjects:@"a.mp3", @"b.mp3",@"c.mp3", nil];  


imageArray = [[NSArray arrayWithObjects: 
               [UIImage imageNamed:@"a.jpg"], 
               [UIImage imageNamed:@"b.jpg"], 
               [UIImage imageNamed:@"c.jpg"],
               nil] retain];
[super viewDidLoad];}
-(IBAction) next; {
currentSound++;
if (currentSound >= sounds.count) currentSound = 0;
//what kind of code play this sound////images work perpectly but sound not//


currentImage++;
if (currentImage >= imageArray.count) currentImage = 0;
UIImage *img = [imageArray objectAtIndex:currentImage];
[imageView setImage:img];} 

Upvotes: 1

Andrew
Andrew

Reputation: 708

To play the sound, use this type this in your .h file: AVAudioPlayer *theaudio;

and this goes in your .m, when you want to play the music NSString *path = [[NSBundle mainBundle] pathForResource:@"nameofmusicfile" ofType:@"mp3"]; theaudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [path release]; [theaudio play]; to stop it, use [theaudio stop];

to display the image use UIImageView *i;

i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];

to put it on - [view addSubview: i]; to move it i.center = CGPointMake(0.0,0.0);

Upvotes: 0

Related Questions