设为首页 加入收藏

TOP

【代码笔记】iOS-看图听声音(一)
2017-10-13 10:17:27 】 浏览:7871
Tags:代码 笔记 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:UIButto
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Swift学习】Swift编程之旅---集.. 下一篇IOS开发之代理的设计小技巧

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目