播放器(仿腾讯微博)

效果图:

代码:

RootViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface RootViewController : UIViewController
<AVAudioPlayerDelegate>
{
    //存放歌曲数组
    NSMutableArray *musicArray;
    //名字所在的label
    UILabel * musicNameLabel;
    //播放器
    AVAudioPlayer * _player;
    //进度后的背景色
    UIView * overLayView;
    //声音控制
    UISlider * volumeSlider;
    //歌曲进度
    UISlider * progressSlider;
    //语音所在的灰色背景
    UIView * functionView;
    //前一首按钮
    UIButton * prevButton;
    //播放按钮
    UIButton * playButton;
    //下一首按钮
    UIButton * nextButton;
    //重播按钮
    UIButton * repeatButton;
    //歌曲播放的时间
    UILabel *startTime;
    //歌曲剩下的时间
    UILabel * currentTime;

    //存放歌曲在数组中的当前位置
    NSInteger currentIndex;
    NSTimer *timer;

    //图片框以及里面的图片
    UIView *transitionView;
    //灰色的图
    UIImageView * borderView;
    //中间的空间图片
    UIView * middleView;

}
@end

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.navigationBarHidden=YES;

    //歌曲所在的数组
    musicArray=[[NSMutableArray alloc] initWithObjects:@"一眼万年",@"中国话", @"夜曲",@"三寸天堂",nil];

    //设置背景色
    UIImage *image=[UIImage imageNamed:@"audio_bg.png"];
    self.view.backgroundColor =[UIColor colorWithPatternImage:image];

    //显示歌曲的名字
    UIView *navView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    navView.backgroundColor=[UIColor clearColor];
    navView.alpha=0.9;
    [self.view  addSubview:navView];

    //歌曲的名字所在的label
    musicNameLabel=[[UILabel alloc] initWithFrame:CGRectMake(80, 0, 160, 44)];
    musicNameLabel.backgroundColor=[UIColor clearColor];
    musicNameLabel.textColor=[UIColor blackColor];
    musicNameLabel.text = [musicArray objectAtIndex:0];
    musicNameLabel.font=[UIFont boldSystemFontOfSize:12];
    musicNameLabel.textAlignment=NSTextAlignmentCenter;

    [navView addSubview:musicNameLabel];

    //歌曲的地址
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[musicArray objectAtIndex:0] ofType:@"mp3"]];

    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    //准备播放,先提前把缓存准备好.
    [_player prepareToPlay];
    //开启变速功能
    _player.enableRate = YES;
    _player.delegate = self;
    [self playerViewInit];

}
#pragma -mark -functions
//设置播放器的画面的排版
-(void)playerViewInit{
    //进度后的背景色
    overLayView=[[UIView alloc] initWithFrame:CGRectMake(0.0f, 44.0f, 320.0f, 80.0f)];
    overLayView.backgroundColor=[UIColor colorWithWhite:0.1f alpha:0.2f];
    overLayView.hidden=NO;
    [self.view addSubview:overLayView];

    //声音调节
    volumeSlider=[[UISlider alloc]initWithFrame:CGRectMake(20.0f, 20.0f, 250.0f, 30.0f)];
    volumeSlider.backgroundColor=[UIColor clearColor];
    [volumeSlider setThumbImage:[UIImage imageNamed:@"com_thumb_max_h.png"] forState:UIControlStateNormal];
    [volumeSlider setMinimumTrackImage:[[UIImage imageNamed:@"com_slider_min_l.png"]stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateNormal];
    volumeSlider.alpha=0.6f;
    volumeSlider.minimumValue=0.0f;
    volumeSlider.maximumValue=1.0f;
    volumeSlider.value=0.2f;
    [volumeSlider addTarget:self action:@selector(doClickChangeVolume) forControlEvents:UIControlEventValueChanged];
    volumeSlider.minimumValueImage=[UIImage imageNamed:@"cell_radio_speaker.png"];

    [overLayView addSubview:volumeSlider];
    _player.volume = volumeSlider.value;

    //语音后的黑色按钮
    functionView=[[UIView alloc]initWithFrame:CGRectMake(0, 460-90, 320, 90)] ;
    functionView.backgroundColor=[UIColor colorWithWhite:0.1f alpha:0.2f];
    [self.view addSubview:functionView];

    //前一首按钮
    prevButton=[UIButton buttonWithType:UIButtonTypeCustom];
    prevButton.frame=CGRectMake(50, 10.0f, 40.0f, 40.0f);
    [prevButton setImage:[UIImage imageNamed:@"audio_btn_pre_h.png"] forState:UIControlStateNormal];
    [prevButton setImage:[UIImage imageNamed:@"audio_btn_pre_n.png"] forState:UIControlStateHighlighted];
    [prevButton addTarget:self action:@selector(doClickPrevButton:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:prevButton];

    //播放按钮
    playButton=[UIButton buttonWithType:UIButtonTypeCustom];
    playButton.frame=CGRectMake(140, 10.0f, 40.0f, 40.0f);
    playButton.tag = 10;
    [playButton setImage:[UIImage imageNamed:@"audio_btn_play_h.png"] forState:UIControlStateNormal];
    [playButton addTarget:self action:@selector(doClickPlayButton:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:playButton];

    //后一首按钮
    nextButton=[UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.frame=CGRectMake(230.0f, 10.0f, 40.0f, 40.0f);
    [nextButton setImage:[UIImage imageNamed:@"audio_btn_next_h.png"] forState:UIControlStateNormal];
    [nextButton setImage:[UIImage imageNamed:@"audio_btn_next_n.png"] forState:UIControlStateHighlighted];
    [nextButton addTarget:self action:@selector(doClickNextButton:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:nextButton];

    //音量控制条
    progressSlider =[[UISlider alloc]initWithFrame:CGRectMake(50.0f, 55.0f, 230.0f, 20.0f)];
    progressSlider.minimumValue=0.0f;
    progressSlider.maximumValue=1.0f;
    progressSlider.backgroundColor=[UIColor clearColor];
    [progressSlider setThumbImage:[UIImage imageNamed:@"com_thumb_max_h.png"] forState:UIControlStateNormal];
    [progressSlider setMinimumTrackImage:[[UIImage imageNamed:@"com_slider_min_l.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0]forState:UIControlStateNormal];
    [progressSlider addTarget:self action:@selector(doClickProgressButton) forControlEvents:UIControlEventValueChanged];
    [functionView addSubview:progressSlider];

    //重复按钮
    repeatButton=[UIButton buttonWithType:UIButtonTypeCustom];
    repeatButton.frame=CGRectMake(5.0f, 20.0f, 30.0, 30.0f);
    repeatButton.tag=20;
    [repeatButton setImage:[UIImage imageNamed:@"audio_btn_random_h.png"] forState:UIControlStateNormal];
    [repeatButton addTarget:self action:@selector(doClickRepeatButton:) forControlEvents:UIControlEventTouchUpInside];
    [functionView addSubview:repeatButton];

    //开始播放时间
    startTime=[[UILabel alloc] initWithFrame:CGRectMake(10.0f, 50.0f, 40.0f, 30.0)];
    startTime.backgroundColor=[UIColor clearColor];
    startTime.text=[NSString stringWithFormat:@"%02d:%02d",0,0];
    startTime.textColor=[UIColor whiteColor];
    startTime.font=[UIFont systemFontOfSize:13.0f];
    [functionView addSubview:startTime];

    //歌曲当前的剩下的时间
    currentTime=[[UILabel alloc]initWithFrame:CGRectMake(285.0f, 50.0f, 40.0f, 30.0f)];
    currentTime.backgroundColor=[UIColor clearColor];
    currentTime.text=[NSString stringWithFormat:@"%02d:%02d",0,0];
    currentTime.font=[UIFont systemFontOfSize:13.0f];
    currentTime.textColor=[UIColor whiteColor];
    [functionView addSubview:currentTime];

    //中间的空间背景图
    transitionView=[[UIView alloc] initWithFrame:CGRectMake(0, 88, 320, 416-90-44)];
    [self.view addSubview:transitionView];

    //边框
    borderView=[[UIImageView alloc]initWithFrame:CGRectMake(60.0f, 90.0f-44.0f, 200.0f, 200.0f)];
    borderView.image=[UIImage imageNamed:@"audio_cover_bg.png"];
    borderView.tag=200;
    transitionView.userInteractionEnabled=YES;
    [transitionView addSubview:borderView];

    //空间图
    middleView=[[UIView alloc]initWithFrame:CGRectMake(15.0f, 15.0f, 170.0f, 170.0f)];
    middleView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"audio_album_default.png"]];
    middleView.alpha=1.0f;
    middleView.tag=201;
    [borderView addSubview:middleView];

    UITapGestureRecognizer *tapGes=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
    [transitionView addGestureRecognizer:tapGes];

}
#pragma -mark -AVAudioPlayerDelegate
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    currentIndex++;
    if (currentIndex == musicArray.count) {
        currentIndex = 0;
    }

    [self loadMusic:[musicArray objectAtIndex:currentIndex] type:@"mp3"];
    musicNameLabel.text = [musicArray objectAtIndex:currentIndex];
    [_player play];
}
#pragma -mark -functions
-(void)loadMusic:(NSString*)name type:(NSString*)type
{
    NSString* path= [[NSBundle mainBundle] pathForResource: name ofType:type];

    NSURL* url = [NSURL fileURLWithPath:path];

    _player= [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    _player.delegate=self;
    _player.volume= volumeSlider.value;
    [_player prepareToPlay];

}
#pragma -mark -doClickActions
//点击背景图的时候,歌曲进度所在的部分隐藏
-(void)tapGes{
    if (overLayView.hidden == YES) {
        overLayView.hidden = NO;
    }else{
        overLayView.hidden = YES;
    }
}
//声音播放的条
-(void)doClickProgressButton{
    _player.volume = volumeSlider.value;

}
//前一首歌曲
-(void)doClickPrevButton:(UIButton *)sender{
    BOOL playFlag;
    if (_player.playing) {
        playFlag = YES;
        [_player stop];
    }else{
        playFlag = NO;
    }
    currentIndex--;
    if (currentIndex < 0) {
        currentIndex =musicArray.count - 1;
    }
    [self loadMusic:[musicArray objectAtIndex:currentIndex] type:@"mp3"];
    musicNameLabel.text = [musicArray objectAtIndex:currentIndex];
    if (playFlag == YES) {
        [_player play];
    }
}
//刷新进度条
-(void)refresh{
    progressSlider.value = _player.currentTime/_player.duration;
    startTime.text =   [NSString stringWithFormat:@"%02d:%02d",(int)_player.currentTime/60,(int)_player.currentTime%60];
    currentTime.text = [NSString stringWithFormat:@"%d:%02d",(int)(_player.duration-_player.currentTime)/60,(int)(_player.duration-_player.currentTime)%60];
}
//播放按钮
-(void)doClickPlayButton:(UIButton *)sender{
    if (_player.playing) {
        [_player pause];
        [playButton setImage:[UIImage imageNamed:@"audio_btn_play_h.png"] forState:UIControlStateNormal];
        [timer invalidate];
    }else{
        [_player play];
        [playButton setImage:[UIImage imageNamed:@"audio_btn_pause_h.png"] forState:UIControlStateNormal];
        //更新下进度条
        timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(refresh) userInfo:nil repeats:YES];
    }
}
//下一首按钮
-(void)doClickNextButton:(UIButton *)sender{
    BOOL playFlag;
    if (_player.playing) {
        playFlag = YES;
        [_player stop];
    }else{
        playFlag = NO;
    }
    currentIndex++;
    if (currentIndex == musicArray.count) {
        currentIndex = 0;
    }
    [self loadMusic:[musicArray objectAtIndex:currentIndex] type:@"mp3"];

    musicNameLabel.text = [musicArray objectAtIndex:currentIndex];

    if (playFlag == YES) {
        [_player play];
    }

}
//重复按钮
-(void)doClickRepeatButton:(UIButton *)sender{
    ;
}
//进度按钮
-(void)doClickChangeVolume{
    _player.currentTime = _player.duration *progressSlider.value;
}

@end
时间: 2024-10-24 04:02:16

播放器(仿腾讯微博)的相关文章

完美拖拽 &amp;&amp;仿腾讯微博效果&amp;&amp; 自定义多级右键菜单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

Js仿腾讯微博效果,无刷新删除微博

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

仿腾讯微博

仿腾讯微博效果,以面向对象的知识重构,该例子来源于 js实例索引 http://js.fgm.cc/learn/lesson6/02.html 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www

ASP.NET 仿腾讯微博提示“还能输入*个字符”的实现

textbox如果设置TextMode="MultiLine"则 它的MaxLength设置的值就无效:为了能达到像腾讯微薄.新浪微薄那样的提示的效果(腾讯和新浪微薄文本框用到的应该是textarea),尝试如果不考虑用鼠标操作粘贴.删除textbox的内容,用jquery的keyup和keydown能实现,下面是实现的一个技巧,用到了js的计时器(当焦点在textbox中则“开启”计时器,失去焦点则“关闭”计时器),很好的解决了鼠标操作粘贴.删除textbox的内容不能改变字符个数的

仿酷狗音乐播放器开发日志二十四 选项设置窗体的实现(附328行xml布局源码)

转载请说明原出处,谢谢~~ 花了两天时间把仿酷狗的选项设置窗体做出来了,当然了只是做了外观.现在开学了,写代码的时间减少,所以整个仿酷狗的工程开发速度减慢了.今天把仿酷狗的选项设置窗体的布局代码分享出来,给学习duilib布局的朋友做个demo.现在编写的仿酷狗选项设置窗体和原酷狗的窗体不细看几乎看不出差别,控件的布局位置和原酷狗最多只有几个像素的位置差别. 先来看一下原酷狗的选项设置窗体的其中一个页面: 如果还不太会布局的朋友可以先看我前些日子写的关于duilib布局的博客<duilib各种布

关于仿酷狗音乐播放器开源:寻求一套音乐播放器素材,让仿酷狗开源

转载请说明原出处,谢谢~~ 距离公布測试版的仿酷狗音乐播放器.已经几个月过去了.期间非常多网友加我QQ来问我开源的问题,我也早有开源意向. 但我也一直没有得到可靠的信息,保证开源后没有不论什么问题. 所以我打算给这个软件全然换一套界面素材.放弃原来的酷狗素材. 这样开源也就没有不论什么问题了,问题是我个人PS水平太烂.仅仅会切切图.如今寻求一套完整的音乐播放器的素材,素材内容要全面,能够用来替代酷狗的素材. 假设有哪位朋友有这种素材,或者有时间能够做出一套素材让我使用,我将感激不尽.素材替换完成

仿酷狗音乐播放器开发日志二十一 开发动态调色板控件(附源代码)

转载请说明原出处,谢谢~~ 上一篇仿酷狗日志结束后,整个换肤功能就仅仅剩下调色板功能没有做了.我本以为会非常easy.可是研究了酷狗的调色板功能后发现不是那么简单的事情.首先看一下酷狗的调色板的样子: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1aG9uZ3NodQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" > waterm

仿酷狗音乐播放器已开源!

转载请说明原出处,谢谢:http://blog.csdn.net/zhuhongshu/article/details/41037875 距离我发布测试版的Redrain音乐盒(仿酷狗播放器),现在正好刚2个月.博客留言和QQ群里也一直有网友关心这个小项目开源的问题.先感谢网友对这个小项目的支持.之前我一直担心版权问题而没有开源这个项目,因为我只是个在校大学生,不想惹麻烦,希望大家体谅! 关于这个Redrain音乐盒的发布程序的说明和使用方法,见<Redrain仿酷狗音乐播放器开发完毕,发布测试

基于jQuery仿QQ音乐播放器网页版代码

基于jQuery仿QQ音乐播放器网页版代码是一款黑色样式风格的网页QQ音乐播放器样式代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="m_player" id="divplayer" role="application" onselectstart="return false" style="left: 0px;"> <div class=&