[Code notes] view pictures, listen to sounds, and take Code notes.
I ,.
2. Engineering Drawing.
3. Code.
RootViewController. h
# Import <UIKit/UIKit. h> # import <AVFoundation/AVFoundation. h> @ interface RootViewController: UIViewController <AVAudioPlayerDelegate> {UIImageView * backImageView; // player AVAudioPlayer * _ audioPlayer; // array of image names NSMutableArray * pictureNameArray; // music name array NSMutableArray * musicNameArray; // The index int songIndex of the subscript to be played; // The left button UIButton * leftButton; // the right button UIButton * rightButton; // display the title label UILabel * titleLabel;} @ end
RootViewController. m
# Import "RootViewController. h "@ interface RootViewController () @ end @ implementation RootViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; self. title = @ "view pictures and audios"; // initialize data [self initData]; // initialize the background image [self initBackGroundView];} # pragma-mark-functions-(void) initData {pictureNameArray = [[NSMutableArray alloc] initWithObjects: @ "chicken", @ "bear", @ "bird", @ "camel", @ "cat ", @ "cattle", @ "deer", @ "dog", @ "doldolphin", @ "donkey", @ "duck", @ "elephant", @ "fox ", @ "frog", @ "goose", @ "horse", @ "lion", @ "mew", @ "monkey", @ "pig", @ "sheep ", @ "snake", @ "tiger", @ "wolf", nil]; musicNameArray = [[NSMutableArray alloc] initWithObjects: @ "chicken", @ "bear ", @ "bird", @ "Camel", @ "kitten", @ "cow", @ "sika deer", @ "puppy", @ "Dolphin", @ "Mao ", @ "Duck", @ "elephant", @ "Fox", @ "frog", @ "White Goose", @ "Pony", @ "lion", @ "sea bird ", @ "monkey", @ "", @ "Sheep", @ "snake", @ "tiger", @ "wolf", nil];}-(void) loadMusic :( NSString *) name type :( NSString *) type {NSString * path = [[NSBundle mainBundle] pathForResource: name ofType: type]; NSURL * url = [NSURL fileURLWithPath: path]; _ audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil]; _ audioPlayer. delegate = self; _ audioPlayer. volume = 0.5; [_ audioPlayer prepareToPlay];}-(void) initBackGroundView {backImageView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0,320,460)]; backImageView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "%@.jpg", [pictureNameArray objectAtIndex: songIndex]; [self. view addSubview: backImageView]; // play button UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; button. tag = 100; button. frame = CGRectMake (130,310, 60, 50); [button addTarget: self action: @ selector (play :) forControlEvents: UIControlEventTouchUpInside]; [button setImage: [UIImage imageNamed: @ "play.png"] forState: UIControlStateNormal]; [self. view addSubview: button]; // previous leftButton = [UIButton buttonWithType: UIButtonTypeCustom]; leftButton. frame = CGRectMake (30,310, 60, 50); [leftButton addTarget: self action: @ selector (prier) forControlEvents: UIControlEventTouchUpInside]; [leftButton setImage: [UIImage imageNamed: @ "Left.png"] forState: UIControlStateNormal]; [self. view addSubview: leftButton]; // next rightButton = [UIButton buttonWithType: UIButtonTypeCustom]; rightButton. frame = CGRectMake (230,310, 60, 50); [rightButton addTarget: self action: @ selector (next) forControlEvents: UIControlEventTouchUpInside]; [rightButton setImage: [UIImage imageNamed: @ "right.png"] forState: UIControlStateNormal]; [self. view addSubview: rightButton]; // Title titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (0,290,320, 30)]; titleLabel. font = [UIFont systemFontOfSize: 25]; titleLabel. textAlignment = NSTextAlignmentCenter; titleLabel. textColor = [UIColor blueColor]; titleLabel. numberOfLines = 0; titleLabel. text = [musicNameArray objectAtIndex: 0]; [self. view addSubview: titleLabel]; [self loadMusic: [pictureNameArray objectAtIndex: 0] type: @ "mp3"];}-(void) setBackground :( UIImage *) image {backImageView. image = image ;}# pragma-mark-doClickActions // play-(void) play :( UIButton *) button {if (_ audioPlayer. playing) {[button setImage: [UIImage imageNamed: @ "play.png"] forState: UIControlStateNormal]; [_ audioPlayer pause];} else {[button setImage: [UIImage imageNamed: @ "stop.png"] forState: UIControlStateNormal]; [_ audioPlayer play] ;}// previous-(void) prier {BOOL playFlag; if (_ audioPlayer. playing) {playFlag = YES; [_ audioPlayer stop];} else {playFlag = NO;} songIndex --; if (songIndex <0) songIndex = pictureNameArray. count-1; UIButton * button = (UIButton *) [self. view viewWithTag: 100]; [button setImage: [UIImage imageNamed: @ "play.png"] forState: UIControlStateNormal]; [self loadMusic: [pictureNameArray objectAtIndex: songIndex] type: @ "mp3"]; UIImage * image = [UIImage imageNamed: [NSString stringWithFormat: @ "#@.jpg", [pictureNameArray objectAtIndex: songIndex]; [self setBackground: image]; titleLabel. text = [musicNameArray objectAtIndex: songIndex]; if (playFlag = YES) {[_ audioPlayer play] ;}// next-(void) next {BOOL playFlag; if (_ audioPlayer. playing) {playFlag = YES; [_ audioPlayer stop];} else {playFlag = NO;} songIndex ++; if (songIndex = pictureNameArray. count) {songIndex = 0;} UIButton * button = (UIButton *) [self. view viewWithTag: 100]; [button setImage: [UIImage imageNamed: @ "play.png"] forState: UIControlStateNormal]; [self loadMusic: [pictureNameArray objectAtIndex: songIndex] type: @ "mp3"]; UIImage * image = [UIImage imageNamed: [NSString stringWithFormat: @ "#@.jpg", [pictureNameArray objectAtIndex: songIndex]; [self setBackground: image]; titleLabel. text = [musicNameArray objectAtIndex: songIndex]; if (playFlag = YES) {[_ audioPlayer play] ;}}@ end