【代码笔记】iOS-看图听声音
一,效果图。

二,工程图。

三,代码。
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

【代码笔记】iOS-看图听声音的更多相关文章
- 【代码笔记】iOS-看图听故事
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFo ...
- 【代码笔记】iOS-饼图
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @class QuizChartView; @interf ...
- 吴恩达deepLearning.ai循环神经网络RNN学习笔记_看图就懂了!!!(理论篇)
前言 目录: RNN提出的背景 - 一个问题 - 为什么不用标准神经网络 - RNN模型怎么解决这个问题 - RNN模型适用的数据特征 - RNN几种类型 RNN模型结构 - RNN block - ...
- Multimodal —— 看图说话(Image Caption)任务的论文笔记(一)评价指标和NIC模型
看图说话(Image Caption)任务是结合CV和NLP两个领域的一种比较综合的任务,Image Caption模型的输入是一幅图像,输出是对该幅图像进行描述的一段文字.这项任务要求模型可以识别图 ...
- 看图写代码---看图写代码 阅读<<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 ...
- (CV学习笔记)看图说话(Image Captioning)-1
Background 分别使用CNN和LSTM对图像和文字进行处理: 将两个神经网络结合: 应用领域 图像搜索 安全 鉴黄 涉猎知识 数字图像处理 图像读取 图像缩放 图像数据纬度变换 自然语言处理 ...
- 学习笔记TF060:图像语音结合,看图说话
斯坦福大学人工智能实验室李飞飞教授,实现人工智能3要素:语法(syntax).语义(semantics).推理(inference).语言.视觉.通过语法(语言语法解析.视觉三维结构解析)和语义(语言 ...
- xmpp整理笔记:发送图片信息和声音信息
图片和音频文件发送的基本思路就是: 先将图片转化成二进制文件,然后将二进制文件进行base64编码,编码后成字符串.在即将发送的message内添加一个子节点,节点的stringValue(节点的值) ...
- 笔记-iOS 视图控制器转场详解(上)
这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...
随机推荐
- angularjs中的页面访问权限设置
11月在赶一个项目,这阵子比较忙,挤挤时间更一篇博客吧,如标题所述说说在ng中页面访问权限控制的问题,水平有限各位看官见谅: 在以往的项目中,前后端常见的配合方式是前端提供页面和ui加一点DuangD ...
- 使用MVVM-Sidekick开发Universal App(一)
终于要迈进Universal的大坑了,还有点小激动呢. CurrencyExchanger 掌中汇率是我前几年发布在Windows Phone商店中的一个应用,当时是WP7版本,下载链接:http:/ ...
- C# 重载的几种实现
算法重用是非常普遍的需求,在C#中可以使用如下手段实现,非常简单,自己记录一下,方便查询. 以一个小功能为例来说明一下:打印1-5这5个数,和A-E这5个字符. 重载方式 static void Ma ...
- 炉石传说 C# 开发笔记 (初版)
法术资料说明 1.资料的准备 从GitHub上面获得的工程里面,是没有XML卡牌资料配置的,这个是需要你自己生成的. 打开炉边处说的客户端 然后按下 卡牌资料生成 将炉石资料文件设定为 Github ...
- MySQL Workbench gnome-keyring-daemon错误的解决
在Fedora下安装了一个MySQL Workbench,运行,连接数据库,在Store in Keychain时出现了gnome-keyring-daemon错误,不能保存密码,也就连不上数据库. ...
- 似乎都设置了utf-8,为什么出现乱码
解决方法如下: ****************************************************************************************[来自我 ...
- java中的几种取整函数
舍掉小数取整:Math.floor(2)=2 舍掉小数取整:Math.floor(2.1)=2 舍掉小数取整:Math.floor(2.5)=2 舍掉小数取整:Math.floor(2.9)=2 负数 ...
- 用Supervisord管理Python进程
http://feilong.me/2011/03/monitor-processes-with-supervisord Supervisord是用Python实现的一款非常实用的进程管理工具,类似于 ...
- pm2 配置
---恢复内容开始--- 1. ecosystem.json { "apps": [ { "name": "name", // 项目名 &q ...
- socket.io,环境搭建 & Hello world
原文:http://www.cnblogs.com/xiezhengcai/p/3955827.html socket.io 一个与服务器实时通信的工具,它与原生的webSocket相比,具有更可靠. ...