Как уже упоминалось в вопросе, я добавил несколько изображений и звуковой файл в папку ресурсов. Навигация по изображениям работает нормально с помощью кнопки «Следующая» и «Предыдущая». Теперь мне нужно воспроизвести звук, связанный с изображением. Рабочий код представлен ниже. Мне нужно знать, что мне нужно сделать, чтобы звук воспроизводился с изображением.
Я добавил audioToolbox.framework и AVFoundation.framework. Я прочитал о NSSound
на сайте разработчиков Apple.
#import "next_previousViewController.h"
@implementation next_previousViewController
@synthesize image1, next1, next2;
NSMutableArray *list;
NSMutableArray *sound;
int state1 = 0;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
list = [[NSMutableArray alloc] init];
[list addObject:@"A.png"];
[list addObject:@"B.png"];
[list addObject:@"C.png"];
[list addObject:@"D.png"];
//[image1 setImage:[UIImage imageNamed:@"A.png"]];
sound = [[NSMutableArray alloc] init];
[sound addObject:@"A.mp3"];
[sound addObject:@"B.mp3"];
[sound addObject:@"C.mp3"];
[sound addObject:@"D.mp3"];
[sound addObject:@"E.mp3"];
[super viewDidLoad];
}
-(IBAction) doNext
{
int i, count;
//state = 1;
count = [list count];
for(i = state1; i < count; i++)
{
[image1 setImage:[UIImage imageNamed:[list objectAtIndex:state1]]];
//--------------------For Sound-----------------------------------
//-----------------------------END--------------------------------
//exit(0);
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hello" message:[NSString stringWithFormat: @"%d", state1] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
//[alert show];
state1 = state1 + 1;
break;
}
}
-(IBAction) doPrev
{
int i, count;
count = [list count];
for(i = state1; i < count; i++)
{
state1 = state1 - 2;
[image1 setImage:[UIImage imageNamed:[list objectAtIndex:state1]]];
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hello" message:[NSString stringWithFormat: @"%d", state1] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
//[alert show];
state1 = state1 + 1;
break;
}
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
Извините за длинный вопрос.