2011-08-28 52 views
0

我有10个图像文件和声音文件,当我显示“a.png”时,我想播放“a.mp3”。然后当我点击“下一步”按钮时,显示“b.png”并播放“b.mp3”。我该如何解决这个问题?用显示的图像逐个播放声音

回答

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停止];

显示图像使用UIImageView * i;

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

把它放在 - [view addSubview:i]; 移动它i.center = CGPointMake(0.0,0.0);

+0

感谢您的回答,但我认为这是一组工作... – ersin

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];}