我有十个图像文件和声音文件,当我显示“a.png”时,我想播放“a.mp3”。然后当我点击“下一步”按钮时,显示“b.png”并播放“b.mp3”。我该如何解决这个问题?
答案 0 :(得分:1)
-(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];}
答案 1 :(得分:0)
要播放声音,请使用此功能 在.h文件中输入以下内容: AVAudioPlayer * theaudio;
当你想播放音乐时,这就在你的.m中 NSString * path = [[NSBundle mainBundle] pathForResource:@“nameofmusicfile”ofType:@“mp3”]; theaudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [路径发布]; [theaudio play]; 停止它,使用[theaudio stop];
显示图像使用UIImageView * i;
i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@“image.png”]];
把它穿上 - [查看addSubview:i]; 移动它i.center = CGPointMake(0.0,0.0);