看图听声音_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 看图听声音

看图听声音

 2014/10/22 15:30:22  粉粉色  程序员俱乐部  我要评论(0)
  • 摘要:效果图:点击播放按钮的时候,会播放声音;点击左侧按钮,会转到上一首。点击右侧按钮,会转到下一首。同时,中间会显示所播放内容的画面和字。工程架构:RootViewController.h#import<UIKit/UIKit.h>#import<AVFoundation/AVFoundation.h>@interfaceRootViewController:UIViewController<AVAudioPlayerDelegate>
  • 标签:

 

效果图:

 

点击播放按钮的时候,会播放声音;

点击左侧按钮,会转到上一首。

点击右侧按钮,会转到下一首。

同时,中间会显示所播放内容的画面和字。

 

工程架构

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

 

  • 相关文章
发表评论
用户名: 匿名