一,效果圖。 二,工程圖。 三,代碼。 RootViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface RootViewController : UIViewControlle ...
一,效果圖。
二,工程圖。
三,代碼。
RootViewController.h
#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
RootViewController.m
#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: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];
//下一首
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];
//標題
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
//播放
-(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];
}
}
//上一首
-(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];
}
}
//下一首
-(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