你的位置:首页 > 软件开发 > 操作系统 > 【代码笔记】看图听声音

【代码笔记】看图听声音

发布时间:2016-04-18 10:00:10
一,效果图。二,工程图。三,代码。RootViewController.h#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@interface RootViewController : ...

【代码笔记】看图听声音

一,效果图。

【代码笔记】看图听声音

二,工程图。

【代码笔记】看图听声音

三,代码。

RootViewController.h

【代码笔记】看图听声音
#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
【代码笔记】看图听声音

 

 


 

原标题:【代码笔记】看图听声音

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录