音乐播放器

音乐播放器,第1张

概述音乐播放器

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#import "ZJVIEwController.h"#import "ZjMusic.h"@interface ZJVIEwController ()<AVAudioPlayerDelegate,UITabbarDelegate,UItableVIEwDataSource>@end#define kBtnHeight 50#define kBtnWIDth 60@implementation ZJVIEwController- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    [self initVIEw];    [self initData];              // NSLog(@"%@",self.lrcDict);}-(voID)initData{    ZjMusic *music1 = [[ZjMusic alloc] initWithname:@"Right Here Waiting(此情可待)" andType:@"mp3"];    ZjMusic *music2 = [[ZjMusic alloc] initWithname:@"Beyond-真的爱你" andType:@"mp3"];    ZjMusic *music3 = [[ZjMusic alloc] initWithname:@"刘德华-爱你一万年" andType:@"mp3"];    ZjMusic *music4 = [[ZjMusic alloc] initWithname:@"毛宁-涛声依旧" andType:@"mp3"];    ZjMusic *music5 = [[ZjMusic alloc] initWithname:@"你是我的眼" andType:@"mp3"];    ZjMusic *music6 = [[ZjMusic alloc] initWithname:@"星星" andType:@"mp3"];    ZjMusic *music7 = [[ZjMusic alloc] initWithname:@"月光爱人" andType:@"mp3"];            self.musicdata = [[NSMutableArray alloc] init];        [self.musicdata addobject:music1];    [self.musicdata addobject:music2];    [self.musicdata addobject:music3];    [self.musicdata addobject:music4];    [self.musicdata addobject:music5];    [self.musicdata addobject:music6];    [self.musicdata addobject:music7];    [self loadMusic:music5];    [self initLrc:music5];        self.musicnameLabel.text = music5.name;            }#pragma mark 加载Music-(voID)loadMusic:(ZjMusic*)music{    Nsstring *path = [[NSBundle mainBundle] pathForResource:music.name ofType:music.type];    NSURL *URL = [NSURL fileURLWithPath:path];        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:URL error:nil];    self.audioPlayer.delegate = self;    self.audioPlayer.volume = 0.5;    self.volumeSlIDer.value = self.audioPlayer.volume;    [self.audioPlayer preparetoPlay];        [self returnTotalTime];}#pragma mark 音量slIDer自动移动,currentTime自动变换-(voID)currentTimeChange{    [self returnCurrentTime];    self.durationlSlIDer.value = self.audioPlayer.currentTime/self.audioPlayer.duration;    NSLog(@"curtime ---->%d",(int)self.audioPlayer.currentTime);           // static int index = 0;        Nsstring *key = [Nsstring stringWithFormat:@"%d",(int)self.audioPlayer.currentTime];    Nsstring *lrc = [self.lrcDict objectForKey:key];                if(lrc){//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];//        [self.lrctableVIEw selectRowAtIndexPath:indexPath animated:YES scrollposition:UItableVIEwScrollpositionMIDdle];//        index++;        self.musicLrcLable.text = lrc;        NSLog(@"index--->%@",lrc);    }            }#pragma mark 上一首-(voID)prevIoUsSound{    BOol playFlag;    if(self.audioPlayer.playing){        playFlag = YES;        [self.audioPlayer stop];    }else{        playFlag = NO;    }    _soundindex --;    if(self.soundindex<0){        self.soundindex  = self.musicdata.count-1;    }    ZjMusic *music = self.musicdata[_soundindex];    [self initLrc:music];    self.musicnameLabel.text = music.name;    [self loadMusic:music];        if(playFlag){        [self.audioPlayer play];    }}#pragma mark 下一曲-(voID)nextSound{    BOol playFlag;    if(self.audioPlayer.playing){        playFlag = YES;        [self.audioPlayer stop];    }else{        playFlag = NO;    }    _soundindex ++;    if(self.soundindex>self.musicdata.count-1){        self.soundindex = 0;    }    ZjMusic *music = self.musicdata[_soundindex];    [self initLrc:music];    self.musicnameLabel.text = music.name;    [self loadMusic:music];        if(playFlag){        [self.audioPlayer play];    }}#pragma mark 音量显示-(voID)showVolumeSlIDer{    if(self.volumeSlIDer.hIDden == NO){        self.volumeSlIDer.hIDden = YES;    }else{        self.volumeSlIDer.hIDden = NO;    }    [self performSelector:@selector(hIDdenVolum) withObject:nil afterDelay:5];}#pragma mark 音量隐藏-(voID)hIDdenVolum{    self.volumeSlIDer.hIDden = YES;    }#pragma mark 播放暂停-(voID)palyAndPause:(UIbutton*)button{    Nsstring *imagename = @"play";    if(self.audioPlayer.playing){        [self.audioPlayer pause];        imagename = @"play";                [self.timer invalIDate];           }else{       [self.audioPlayer play];        imagename = @"stop";                self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(currentTimeChange) userInfo:nil repeats:YES];    }        [button setimage:[UIImage imagenamed:imagename] forState:UIControlStatenormal];        [self returnTotalTime];}-(voID)returnTotalTime{    Nsstring *totalTime = [Nsstring stringWithFormat:@"%d:%d",(int)self.audioPlayer.duration/60,(int)self.audioPlayer.duration%60];    self.totalTime.text = totalTime;}#pragma mark 调节音量-(voID)volumeChange{    self.audioPlayer.volume = self.volumeSlIDer.value;    [self returnCurrentTime];}-(voID)returnCurrentTime{    Nsstring *currentTime = [Nsstring stringWithFormat:@"%d:%d",(int)self.audioPlayer.currentTime/60,(int)self.audioPlayer.currentTime%60];    if((int)self.audioPlayer.currentTime%60<10){        currentTime = [Nsstring stringWithFormat:@"%d:0%d",(int)self.audioPlayer.currentTime%60];    }    self.currentTimeLabel.text = currentTime;        }#pragma mark 调节时间-(voID)durationChange{    self.audioPlayer.currentTime = self.durationlSlIDer.value*self.audioPlayer.duration;}- (voID)dealloc {    [_audioPlayer release];    [_durationlSlIDer release];    [_volumeSlIDer release];    [_musicdata release];    [_currentTimeLabel release];    [_musicnameLabel release];    [_totalTime release];    [_lrcDict release];    [_timeArray release];    [_lrctableVIEw release];    [super dealloc];}#pragma mark 初始化数据-(voID)initVIEw{    //当前时间    self.currentTimeLabel  = [[UILabel alloc] initWithFrame:CGRectMake(20,35,40,20)];    self.currentTimeLabel.text = @"0:00";    self.currentTimeLabel.backgroundcolor = [UIcolor clearcolor];    [self.vIEw addSubvIEw:self.currentTimeLabel];	    //持续时间    self.durationlSlIDer = [[UiSlider alloc] initWithFrame:CGRectMake(65,30,190,30)];    [self.durationlSlIDer addTarget:self action:@selector(durationChange) forControlEvents:UIControlEventValueChanged];    [self.vIEw addSubvIEw:self.durationlSlIDer];        //总时间    self.totalTime  = [[UILabel alloc] initWithFrame:CGRectMake(265,20)];    self.totalTime.text = @"0:00";    self.totalTime.backgroundcolor = [UIcolor clearcolor];    [self.vIEw addSubvIEw:self.totalTime];        UIVIEw *subVIEw = [[UIVIEw alloc] initWithFrame:CGRectMake(50,250,220,60)];    [self.vIEw addSubvIEw:subVIEw];    UIbutton *button = [UIbutton buttonWithType:UIbuttonTypeCustom];    //上一曲    button.frame = CGRectMake(0,kBtnWIDth,kBtnHeight);    [button setimage:[UIImage imagenamed:@"left"] forState:UIControlStatenormal];    [button addTarget:self action:@selector(prevIoUsSound) forControlEvents:UIControlEventtouchUpInsIDe];    [subVIEw addSubvIEw:button];        //播放暂停    button = [UIbutton buttonWithType:UIbuttonTypeCustom];    button.frame = CGRectMake(80,kBtnHeight);    [button setimage:[UIImage imagenamed:@"play"] forState:UIControlStatenormal];    [button addTarget:self action:@selector(palyAndPause:) forControlEvents:UIControlEventtouchUpInsIDe];    [subVIEw addSubvIEw:button];        //下一曲    button = [UIbutton buttonWithType:UIbuttonTypeCustom];    button.frame = CGRectMake(160,kBtnHeight);    [button addTarget:self action:@selector(nextSound) forControlEvents:UIControlEventtouchUpInsIDe];    [button setimage:[UIImage imagenamed:@"right"] forState:UIControlStatenormal];    [subVIEw addSubvIEw:button];        //显示歌ci    self.musicLrcLable = [[UILabel alloc] initWithFrame:CGRectMake(0,400,320,44)];    self.musicLrcLable.textAlignment = NSTextAlignmentCenter;    self.musicLrcLable.backgroundcolor = [UIcolor greencolor];    [self.vIEw addSubvIEw:self.musicLrcLable];        //显示歌名    self.musicnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,200,40)];    self.musicnameLabel.backgroundcolor = [UIcolor clearcolor];    self.musicnameLabel.textAlignment = NSTextAlignmentCenter;    [self.vIEw addSubvIEw:self.musicnameLabel];          //音量    button = [UIbutton buttonWithType:UIbuttonTypeCustom];    button.frame = CGRectMake(10,390,kBtnHeight);    [button addTarget:self action:@selector(showVolumeSlIDer) forControlEvents:UIControlEventtouchUpInsIDe];    [button setimage:[UIImage imagenamed:@"labalan"] forState:UIControlStatenormal];    [button setimage:[UIImage imagenamed:@"laba"] forState:UIControlStateHighlighted];    [self.vIEw addSubvIEw:button];        self.volumeSlIDer = [[UiSlider alloc] initWithFrame:CGRectMake(-80,280,5)];    self.volumeSlIDer.minimumValue = 0;    self.volumeSlIDer.maximumValue = 1;    self.volumeSlIDer.hIDden = YES;    self.volumeSlIDer.transform = CGAffinetransformMakeRotation(-95* M_PI/180);    [self.vIEw addSubvIEw:self.volumeSlIDer];    [self.volumeSlIDer addTarget:self action:@selector(volumeChange) forControlEvents:UIControlEventValueChanged];            }-(voID) initLrc:(ZjMusic*)music{    NSLog(@"%@,%@",music.name,music.type);    self.timeArray = [[NSMutableArray alloc] init];    self.lrcDict = [[NSMutableDictionary alloc] init];        Nsstring *lrcPath = [[NSBundle mainBundle] pathForResource:music.name ofType:@"lrc"];    Nsstring *contentStr = [Nsstring stringWithContentsOffile:lrcPath enCoding:NSUTF8StringEnCoding error:nil];            NSArray *array = [contentStr componentsSeparatedByString:@"\n"];    for (int i= 0; i<array.count; i ++) {        Nsstring *linestr = array[i];        NSArray *lineArray = [linestr componentsSeparatedByString:@"]"];        Nsstring *lrcStr = [lineArray objectAtIndex:1];        if([lineArray[0] length]>5){                        Nsstring *timeStr = [lineArray[0] substringWithRange:NSMakeRange(1,5)];                        NSArray *timeArray = [timeStr componentsSeparatedByString:@":"];                                  NSInteger timeInt = [timeArray[0] intValue]*60 + [timeArray[1] intValue];            Nsstring *timetostr = [Nsstring stringWithFormat:@"%d",timeInt];                        [self.lrcDict setobject:lrcStr forKey:timetostr];            [self.timeArray addobject:timetostr];                    }                    }   // NSLog(@"%d",self.timeArray.count);    }@end

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的音乐播放器全部内容,希望文章能够帮你解决音乐播放器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1107851.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存