一,效果图。

二,工程图。

三,代码。

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-看图听声音的更多相关文章

  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. xmpp整理笔记:发送图片信息和声音信息

    图片和音频文件发送的基本思路就是: 先将图片转化成二进制文件,然后将二进制文件进行base64编码,编码后成字符串.在即将发送的message内添加一个子节点,节点的stringValue(节点的值) ...

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

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

随机推荐

  1. Ubuntu14.04安装postgresql9.4

    安装前的检查 首先查看是否已经安装了旧版本: dpkg -l |grep postgresql 如果已经安装了某个版本的postgresql,请先卸载. 安装postgresql 添加postgres ...

  2. Auto Mapper04(MVC中的配置)

    学习如何在MVC项目中配置AutoMapper. 一:首先在MVC项目中引用AutoMapper的DLL文件,接着创建一个接口,这里面我们需要定义两个方法,接口里面的方法只能定义不能实现,也没有什么修 ...

  3. C# 文件 文件夹

    //判断文件夹(路径)是否存在 if (Directory.Exists(Path)) { } //获取文件大小 FileInfo file = new FileInfo(labOfPath); si ...

  4. C++宏和枚举

    宏 我们的计算器程序,用1234对应加减乘除,对于人阅读很产生一点障碍.隔一个月后再看此代码可能想不起是0123还是1234了,还得去代码中查找,如果能为代表四则运算的四个数取个有意义的别名就好了,一 ...

  5. Java动态程序设计:反射介绍

    使用运行的类的信息使你的程序设计更加灵活 反射授予了你的代码访问装载进JVM内的Java类的内部信息的权限,并且允许你编写在程序执行期间与所选择的类的一同工作的代码,而不是在源代码中.这种机制使得反射 ...

  6. 分析.Net里线程同步机制

    我 们知道并行编程模型两种:一种是基于消息式的,第二种是基于共享内存式的. 前段时间项目中遇到了第二种 使用多线程开发并行程序共享资源的问题 ,今天以实际案例出发对.net里的共享内存式的线程同步机制 ...

  7. Android Volley框架的使用(5)

    6. 设置超时时间 可以为请求设置超时时间.最大重试次数.重试时间增长因子等.其中new DefaultRetryPolicy()的第一个参数是超时时间,第二个参数是最大重试次数.第三个参数是重试时间 ...

  8. mysql---ENCODE警告

    'ENCODE' is deprecated and will be removed in a future release. Please use AES_ENCRYPT instead ***** ...

  9. ios源码-ios游戏源码-ios源码下载

    游戏源码   一款休闲类的音乐小游戏源码 该源码实现了一款休闲类的音乐小游戏源码,该游戏的源码很简单,而且游戏的玩法也很容易学会,只要我们点击视图中的grid,就可以 人气:2943运行环境:/Xco ...

  10. jquery常见知识点 总结

    1. jquery特点 2. jquery中css选择器用法 jQuery使用了一套css选择器,共有5种,即标签选择器,ID选择器,类选择器,通用选择器和群组选择器,现分述如下:   标签选择器 用 ...