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 c开发者_如何学JAVAan I solve this?
-(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];}
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);
精彩评论