#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface RootViewController : UIViewController
<AVAudioPlayerDelegate>
{
UIImageView *backImageView;
//播放器
AVAudioPlayer *_audioPlayer;
//图片名字数组
NSMutableArray *pictureNameArray;
//音乐名字数组
NSMutableArray *musicNameArray;
//播放到的下标索引
int songIndex;
//左侧按钮
UIButton * leftButton;
//右侧按钮
UIButton * rightButton;
//显示标题的label
UILabel *titleLabel;
}
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"看图听声";
//初始化数据
[self initData];
//初始化背景图
[self initBackGroundView];
}
#pragma -mark -functions
-(void)initData{
pictureNameArray=[[NSMutableArray alloc]initWithObjects:@"chicken",@"bear",@"bird",@"camel",@"cat",@"cattle",@"deer",@"dog",@"dolphin",@"donkey",@"duck",@"elephant",@"fox",@"frog",@"goose",@"horse",@"lion",@"mew",@"monkey",@"pig",@"sheep",@"snake",@"tiger",@"wolf", nil];
musicNameArray=[[NSMutableArray alloc]initWithObjects:@"小鸡",@"小熊",@"小鸟",@"骆驼",@"小猫",@"奶牛",@"梅花鹿",@"小狗",@"海豚",@"毛驴",@"鸭子",@"大象",@"狐狸",@"青蛙",@"白鹅",@"小马",@"狮子",@"海鸟",@"猴子",@"小猪",@"绵羊",@"小蛇",@"老虎",@"小狼", 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];
//播放按钮
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];
//上一首
leftButton= [UIButton buttonWithType:UIButto