一,效果图。

二,工程图。

三,代码。

RootViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h> @interface RootViewController : UIViewController
<AVAudioPlayerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView * backImageView;
AVAudioPlayer *_audioPlayer;
NSMutableArray *musicArray;
NSMutableArray *titleArray; int songIndex;
UIButton * leftButton;
UIButton * rightButton; UILabel *titleLabel;
UISlider *Slider;
UISlider *volumeSlider;
NSTimer * processTimer;
NSTimer * timer1; }
@end

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
[super viewDidLoad]; self.title = @"故事"; //初始化数据
[self initData];
//初始化背景图
[self initBackgroundView]; }
#pragma -mark -functions
//初始化数据
-(void)initData
{
musicArray=[[NSMutableArray alloc]initWithObjects:@"7",@"11",@"9",@"17",@"5",@"6",@"1",@"8",@"15",@"10",@"2",@"12",@"13",@"14",@"4",@"16",@"3",@"18", nil];
titleArray=[[NSMutableArray alloc]initWithObjects:@"三头公牛和狮子",@"小红帽",@"天女散花",@"朋友再见",@"女娲造人",@"天神的哑水",@"小青蛙听故事",@"淘淘的愿望",@"讲礼貌",@"丑小鸭",@"老鼠,小鸟和香肠",@"两头驴子",@"驴子和主人",@"四个朋友",@"奖品",@"没法通过",@"五颗豌豆",@"彼得*潘", nil]; }
//初始化背景图
-(void)initBackgroundView
{ self.navigationController.navigationBar.tintColor =[UIColor orangeColor];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:69.0/255 green:161.0/255 blue:241.0/255 alpha:1]; //背景
backImageView= [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 460)];
backImageView.image= [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[musicArray objectAtIndex:songIndex]]];
[self.view addSubview:backImageView]; //播放
UIButton* button= [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(130, 260, 60, 50);
button.tag=100;
[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(50, 260, 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(210, 260, 60, 50);
[rightButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
[rightButton setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];
[self.view addSubview:rightButton]; //音量按钮
UIButton *volumebutton= [UIButton buttonWithType:UIButtonTypeCustom];
volumebutton.frame=CGRectMake(10, 330, 40, 40);
[volumebutton setImage:[UIImage imageNamed:@"labalan.png"] forState:UIControlStateNormal];
[volumebutton addTarget:self action:@selector(showVolume) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:volumebutton]; //背景
UIButton *backButton= [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame=CGRectMake(270, 330, 40, 40);
[backButton setImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(setBackground) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton]; //音乐logo图片
UIImageView* musicImageView= [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"musicLogo.png"]];
musicImageView.frame= CGRectMake(80, 40, 160, 140);
[self.view addSubview:musicImageView]; //标题
titleLabel= [[UILabel alloc] initWithFrame:CGRectMake(0, 195, 320, 30)];
titleLabel.font= [UIFont systemFontOfSize:25];
titleLabel.textAlignment= NSTextAlignmentCenter;
titleLabel.textColor= [UIColor blueColor];
titleLabel.numberOfLines=0;
titleLabel.backgroundColor= [UIColor clearColor];
titleLabel.text= [titleArray objectAtIndex:0];
[self.view addSubview:titleLabel]; //进度控制
Slider= [[UISlider alloc] initWithFrame:CGRectMake(50, 230, 220, 5)];
Slider.maximumValue=100;
Slider.minimumValue=0;
Slider.value=0;
Slider.continuous=NO;
[Slider addTarget:self action:@selector(processSet:) forControlEvents:UIControlEventValueChanged];
[Slider addTarget:self action:@selector(processTimerStop) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:Slider]; //音量控制
volumeSlider= [[UISlider alloc] initWithFrame:CGRectMake(-85, 280, 220, 5)];
volumeSlider.maximumValue=1;
volumeSlider.minimumValue=0;
volumeSlider.value= 0.5;
volumeSlider.hidden=YES;
[volumeSlider addTarget:self action:@selector(volumeSet:) forControlEvents:UIControlEventValueChanged];
volumeSlider.transform= CGAffineTransformMakeRotation(-90* M_PI/180);
[self.view addSubview:volumeSlider]; processTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(process) userInfo:nil repeats:YES]; [self loadMusic:[musicArray objectAtIndex:0] type:@"mp3"]; UILongPressGestureRecognizer* rightLongPress= [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(rightLongPress:)];
[rightButton addGestureRecognizer:rightLongPress]; UILongPressGestureRecognizer* leftLongPress= [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(leftLongPress:)];
[leftButton addGestureRecognizer:leftLongPress]; }
//进度控制
-(void)processSet:(UISlider*)slider
{
processTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(process) userInfo:nil repeats:YES];
_audioPlayer.currentTime=slider.value/100*_audioPlayer.duration;
if(_audioPlayer.playing==YES)
[_audioPlayer playAtTime:_audioPlayer.currentTime];
}
-(void)processTimerStop
{
[processTimer invalidate];
}
//背景
-(void)setBackground
{
UIImagePickerController* picker= [[UIImagePickerController alloc] init];
picker.sourceType= UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate=self;
[self presentViewController:picker animated:NO completion:nil];
} #pragma -mark -doClickActions
//右侧按钮手势
-(void)rightLongPress:(UILongPressGestureRecognizer*)longPress
{
if (longPress.state != UIGestureRecognizerStateBegan)
return; if(_audioPlayer.playing)
{
if(_audioPlayer.currentTime>_audioPlayer.duration-5)
_audioPlayer.currentTime=_audioPlayer.currentTime;
else
_audioPlayer.currentTime+=5;
[_audioPlayer playAtTime:_audioPlayer.currentTime]; }
}
//左侧按钮手势
-(void)leftLongPress:(UILongPressGestureRecognizer*)longPress
{
if (longPress.state != UIGestureRecognizerStateBegan)
return;
if(_audioPlayer.playing)
{
if(_audioPlayer.currentTime<5)
_audioPlayer.currentTime=0;
else
_audioPlayer.currentTime-=5; [_audioPlayer playAtTime:_audioPlayer.currentTime]; printf("left\n");
}
}
//雪花函数
-(void)snow
{
int startX= random()%320;
int endX= random()%320;
int width= random()%25;
CGFloat time= (random()%100)/10+5;
CGFloat alp= (random()%9)/10.0+0.1; UIImage* image= [UIImage imageNamed:@"snow.png"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame= CGRectMake(startX,-1*width,width,width);
imageView.alpha=alp;
[self.view addSubview:imageView]; [UIView beginAnimations:nil context:(__bridge void *)(imageView)];
[UIView setAnimationDuration:time];
if(endX>50&&endX<270)
{
imageView.frame= CGRectMake(endX, 270-width/2, width, width); }
else if((endX>10&&endX<50)||(endX>270&&endX<310))
imageView.frame= CGRectMake(endX, 400-width/2, width, width);
else
imageView.frame= CGRectMake(endX, 480, width, width);
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
-(void)onAnimationComplete:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context
{
UIImageView* snowView=(__bridge UIImageView *)(context);
[snowView removeFromSuperview]; }
//封装系统加载函数
-(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)volumeSet:(UISlider*)slider
{
_audioPlayer.volume= slider.value;
}
-(void)showVolume
{
volumeSlider.hidden=NO;
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(hideVolume) userInfo:nil repeats:NO];
}
-(void)hideVolume
{
volumeSlider.hidden=YES;
}
//歌曲进度
-(void)process
{
Slider.value= 100*_audioPlayer.currentTime/_audioPlayer.duration;
}
#pragma -mark -doClickActions
//播放
-(void)play:(UIButton*)button
{
if(_audioPlayer.playing)
{
[button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[_audioPlayer pause];
[timer1 invalidate];
}
else
{
[button setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
timer1=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(snow) userInfo:nil repeats:YES];
[_audioPlayer play];
} }
//上一首
-(void)prier
{
BOOL playFlag;
if(_audioPlayer.playing)
{
playFlag=YES;
[_audioPlayer stop];
}
else
{
playFlag=NO;
}
songIndex--;
if(songIndex<0)
songIndex= musicArray.count-1
;
[self loadMusic:[musicArray objectAtIndex:songIndex] type:@"mp3"]; UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[musicArray objectAtIndex:songIndex]]]; [self setBackground:image];
titleLabel.text= [titleArray objectAtIndex:songIndex];
if(playFlag==YES)
{
[_audioPlayer play];
} }
//下一首
-(void)next
{
BOOL playFlag;
if(_audioPlayer.playing)
{
playFlag=YES;
[_audioPlayer stop];
}
else{
playFlag=NO;
}
songIndex++;
if(songIndex==musicArray.count)
{
songIndex= 0;
} [self loadMusic:[musicArray objectAtIndex:songIndex] type:@"mp3"]; UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[musicArray objectAtIndex:songIndex]]]; [self setBackground:image]; titleLabel.text= [titleArray objectAtIndex:songIndex];
if(playFlag==YES)
{
[_audioPlayer play]; }
} #pragma -mark -AVAudioPlayerDelegate
//播放完成自动切换
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
songIndex++;
if(songIndex==musicArray.count)
songIndex= 0;
[self loadMusic:[musicArray objectAtIndex:songIndex] type:@"mp3"];
titleLabel.text= [titleArray objectAtIndex:songIndex];
[_audioPlayer play]; }
#pragma -mark -UIImagePickerControllerDelegate
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
backImageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)setBackground:(UIImage *)image
{
backImageView.image = image; }
@end
 
 

【代码笔记】iOS-看图听故事的更多相关文章

  1. 【代码笔记】iOS-看图听声音

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFo ...

  2. 【代码笔记】iOS-饼图

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @class QuizChartView; @interf ...

  3. 吴恩达deepLearning.ai循环神经网络RNN学习笔记_看图就懂了!!!(理论篇)

    前言 目录: RNN提出的背景 - 一个问题 - 为什么不用标准神经网络 - RNN模型怎么解决这个问题 - RNN模型适用的数据特征 - RNN几种类型 RNN模型结构 - RNN block - ...

  4. Multimodal —— 看图说话(Image Caption)任务的论文笔记(一)评价指标和NIC模型

    看图说话(Image Caption)任务是结合CV和NLP两个领域的一种比较综合的任务,Image Caption模型的输入是一幅图像,输出是对该幅图像进行描述的一段文字.这项任务要求模型可以识别图 ...

  5. 看图写代码---看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >>

    看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >> 1.S ...

  6. (CV学习笔记)看图说话(Image Captioning)-1

    Background 分别使用CNN和LSTM对图像和文字进行处理: 将两个神经网络结合: 应用领域 图像搜索 安全 鉴黄 涉猎知识 数字图像处理 图像读取 图像缩放 图像数据纬度变换 自然语言处理 ...

  7. 学习笔记TF060:图像语音结合,看图说话

    斯坦福大学人工智能实验室李飞飞教授,实现人工智能3要素:语法(syntax).语义(semantics).推理(inference).语言.视觉.通过语法(语言语法解析.视觉三维结构解析)和语义(语言 ...

  8. 笔记-iOS 视图控制器转场详解(上)

    这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...

  9. 2.bootstrap练习笔记-轮播图

    bootstrap练习笔记-轮播图 1.要使用轮播图,首先要将其放在一个主div里面 设置id为myCaroysel class为carousel slide 设置id是标识这个div是轮播图,等到l ...

随机推荐

  1. 如果根据键盘的frame始终让一个控件始终在键盘的顶部

    我们发现很多时候系统提供的键盘功能有限 有些功能无法实现,所以我们通常的做法就是自定义一个工具条放在键盘的顶部. 那么我们如何知道键盘的frame呢? 这个时候就需要监听键盘发出的通知,在ios中当键 ...

  2. C#中方法的声明

    C#中方法的声明(四要素) 访问修饰符 :public,private(方法的默认访问修饰符) 返回值类型:void 和 非void 方法名称    : 规范是方法名称取动词,每个单词的首字母大写. ...

  3. Entity Framework 实体框架的形成之旅--基类接口的统一和异步操作的实现(3)

    在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...

  4. Mssql中一些常用数据类型的说明和区别

    Mssql中一些常用数据类型的说明和区别 1.bigint 占用8个字节的存储空间,取值范围在-2^63 (-9,223,372,036,854,775,808) 到 2^63-1 (9,223,37 ...

  5. 使用MongoDB作为后台数据库的尝试

    MongoDB作为一个阶层型数据库,在很短的时间里面是不可能被大面积推广使用的, 本文作为一个实验性的课题,探讨一下MongoDB作为网站数据库的可能性. 1.MongoDB作为代替关系型数据库的可能 ...

  6. Mysql存储过程(四)——异常处理

    http://blog.csdn.net/crazylaa/article/details/5368421 有时候,不希望存储过程抛出错误中止执行,而是希望返回一个错误码. MySQL 支持异常处理, ...

  7. php中的常用数组函数(四)(数组中是否有某个键名或索引)

    /***********array_key_exists(检查键名或索引是否在数组中)*****************/ $arr1 = array('name' => 'Sheldon', ...

  8. java枚举与.net中的枚举区别

    通过一段时间的项目实践,发现java中的枚举与.net中的枚举有很大的差别,初期造成了我对java中的枚举一些错误理解及部分有缺陷的应用,其实追其原因还是因为我会习惯性的认为java的枚举在作用以及定 ...

  9. Hibernate(八)__级联操作、struts+hibernate+接口编程架构

    级联操作 所谓级联操作就是说,当你进行主对象某个操作时,从对象hibernate自动完成相应操作. 比如: Department <---->Student 对象关系,我希望当我删除一个d ...

  10. 解决MVC4发布在IIS7后,路径无法访问.apk文件的解决方法

    随着智能手机的普及,越来越多的人使用手机上网,很多网站也应手机上网的需要推出了网站客户端,.apk文件就是安卓(Android)的应用程序后缀名,默认情况下,使用IIS作为Web服务器的无法下载此文件 ...