【代码笔记】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 上附上了 ...
随机推荐
- CSS代码重构与优化之路
作者:@狼狼的蓝胖子 网址:http://www.cnblogs.com/lrzw32/p/5100745.html 写CSS的同学们往往会体会到,随着项目规模的增加,项目中的CSS代码也会越来越多, ...
- C#读取XML文件的基类实现
刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码比较老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很庞大,同时,操作XML的读写操作都是使用SetAttribute和node ...
- webstorage[html5的本地数据处理]
1.webStorage是什么? webStorage是html5中用于本地化存储的一种方式,而在之前呢我们是用cookie的存储方式处理; 2.那它们之间的区别是什么? Ⅰ.cookie存在的问题: ...
- 【Java每日一题】20161115
package Nov2016; import java.io.Serializable; public class Ques1115 implements Serializable{ private ...
- MySQL知识总结
一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...
- Oracle 数据库基础学习 (三) Oracle 四个表结构
Oracle 四个表的 emp dept salgrade bunus 的结构,记住有利于后期SQL语句的学习 雇员表(emp) No. 字段 类型 描述 1 empno NUMBER(4) 表示 ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- 实用的Scala泛函编程
既然谈到实用编程,就应该不单止了解试试一个新的编程语言那么简单了,最好通过实际的开发项目实例来演示如何编程.心目中已经有了一些设想:想用Scala泛函编程搞一个开源的数据平台应用系统,也就是在云平台P ...
- java 本地目录文件删除 ***最爱那水货
/** * @note 删除目录下的所有文件 * @param path * @return */ public static boolean delAllFile(String path){ boo ...
- CI框架源码阅读笔记5 基准测试 BenchMark.php
上一篇博客(CI框架源码阅读笔记4 引导文件CodeIgniter.php)中,我们已经看到:CI中核心流程的核心功能都是由不同的组件来完成的.这些组件类似于一个一个单独的模块,不同的模块完成不同的功 ...