博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
音乐播放器
阅读量:5975 次
发布时间:2019-06-20

本文共 5892 字,大约阅读时间需要 19 分钟。

#import "RootViewController.h"@interface RootViewController (){    UITableView * table;    NSMutableArray * dataSource;}@end@implementation RootViewController- (void)viewDidLoad{    [super viewDidLoad];    dataSource = [[NSMutableArray alloc]init];    NSString * path = [[NSBundle mainBundle]pathForResource:@"songsName" ofType:@"plist"];    NSArray * arr = [NSArray arrayWithContentsOfFile:path];    [dataSource addObjectsFromArray:arr];        table = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, 320, 480 - 30) style:UITableViewStylePlain];    table.delegate =self;    table.dataSource = self;    [self.view addSubview:table];    self.automaticallyAdjustsScrollViewInsets = NO;    //为表格视图添加背景图片    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];    imageView.image = [UIImage imageNamed:@"56.jpg"];    table.backgroundView = imageView;    }-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [dataSource count];}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString * string = @"string";    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];    if(cell == nil)    {        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string]autorelease];    }    cell.textLabel.text = [dataSource objectAtIndex:indexPath.row];    cell.backgroundColor = [UIColor clearColor];    return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSString * songName = [dataSource objectAtIndex:indexPath.row];        //向上一个界面发送通知传递歌曲名称    [[NSNotificationCenter defaultCenter] postNotificationName:@"songName" object:songName];        [self dismissViewControllerAnimated:YES completion:nil];}
//  MainViewController.h#import 
#import
@interface MainViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *songLabel;- (IBAction)pressPlay:(id)sender;- (IBAction)pressStop:(id)sender;@property (retain, nonatomic) IBOutlet UILabel *timeLabel;- (IBAction)progressSlider:(id)sender;- (IBAction)voiceSlider:(id)sender;- (IBAction)changSong:(id)sender;@property (retain, nonatomic) IBOutlet UISlider *progress;@property (retain, nonatomic) IBOutlet UISlider *voice;@end
////  MainViewController.m#import "MainViewController.h"#import "RootViewController.h"@interface MainViewController (){    RootViewController * root;}@property (nonatomic,retain) NSMutableArray * dataSource;//接收歌曲的名称@property (nonatomic,retain) NSString * songName;@property (nonatomic,retain) AVAudioPlayer * player;@end@implementation MainViewController- (void)viewDidLoad{    [super viewDidLoad];    //存放所有的歌曲名称    self.dataSource = [[NSMutableArray alloc]init];    //读取plist文件中的内容    NSString * path = [[NSBundle mainBundle]pathForResource:@"songsName" ofType:@"plist"];    NSArray * arr = [NSArray arrayWithContentsOfFile:path];    [self.dataSource addObjectsFromArray:arr];        //判断此时歌曲名称是否为空    if(self.songName.length == 0)    {        self.songName = @"蓝莲花";    }    self.songLabel.text = self.songName;        //准备播放音乐    //<1>找到要播放的音乐的路径    NSString * songPath = [[NSBundle mainBundle]pathForResource:@"蓝莲花" ofType:@"mp3"];    //设置了第一首歌    //<2>将本地路径转化成NSURL    NSURL * url = [NSURL fileURLWithPath:songPath];    //<3>初始化音乐播放器对象    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];    //<4>设置代理    self.player.delegate = self;    //<5>准备播放音乐    //在播放音乐之前 先将音乐存放在缓存区中 防止播放的过程中出现卡顿    [self.player prepareToPlay];        root = [[RootViewController alloc]init];        //为通知中心添加观察者    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSongPlay:) name:@"songName" object:nil];    }//接收到通知触发的方法-(void)changeSongPlay:(NSNotification *)notification{    self.songName = [notification object];    self.songLabel.text = self.songName;    if(self.player != nil)//如果当前音乐正在播放 不设置的话会两首歌一起    {        [self.player stop];        self.player = nil;    }    NSString *path = [[NSBundle mainBundle] pathForResource:self.songName ofType:@"mp3"];    NSURL * url = [NSURL fileURLWithPath:path];    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];    self.player.delegate = self;        [self.player prepareToPlay];            [self pressPlay:nil];}- (IBAction)pressPlay:(id)sender{    [self.player play];        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];}-(void)timeChange{    //获取当前音乐的总时间  按秒    NSTimeInterval allTime = self.player.duration;    int MM = (int)allTime / 60;    int SS = (int)allTime % 60;    NSString * allStr = [NSString stringWithFormat:@"%.2d:%.2d",MM,SS];    //.2  一位数时前面会保留一个0        //获取当前音乐播放到的时间    NSTimeInterval currentTime = self.player.currentTime;    int mm = (int)currentTime / 60;    int ss = (int)currentTime % 60;    NSString * currentStr = [NSString stringWithFormat:@"%.2d:%.2d",mm,ss];        NSString * string = [NSString stringWithFormat:@"%@/%@",currentStr,allStr];        self.timeLabel.text = string;    //设置进度    self.progress.value = currentTime / allTime;}- (IBAction)pressStop:(id)sender{    if([self.player isPlaying])    {        [self.player stop];        //[self.player pause];    }}- (IBAction)progressSlider:(id)sender{    //<1>暂停音乐    [self pressStop:nil];        UISlider * slider = (UISlider *)sender;        self.player.currentTime = slider.value * self.player.duration;    //播放音乐    [self pressPlay:nil];}- (IBAction)voiceSlider:(id)sender{    UISlider * slider = (UISlider *)sender;    [self.player setVolume:slider.value * 20];}- (IBAction)changSong:(id)sender{    [self presentViewController:root animated:YES completion:nil];}@end

转载于:https://www.cnblogs.com/sayimba/p/5708748.html

你可能感兴趣的文章
Linux备份ifcfg-eth0文件导致的网络故障问题
查看>>
2018年尾总结——稳中成长
查看>>
通过jsp请求Servlet来操作HBASE
查看>>
Shell编程基础
查看>>
Shell之Sed常用用法
查看>>
Centos下基于Hadoop安装Spark(分布式)
查看>>
mysql开启binlog
查看>>
设置Eclipse编码方式
查看>>
分布式系统唯一ID生成方案汇总【转】
查看>>
并查集hdu1232
查看>>
Mysql 监视工具
查看>>
Linux Namespace系列(09):利用Namespace创建一个简单可用的容器
查看>>
博客搬家了
查看>>
Python中使用ElementTree解析xml
查看>>
linux的日志服务器关于屏蔽一些关键字的方法
查看>>
mysql多实例实例化数据库
查看>>
javascript 操作DOM元素样式
查看>>
HBase 笔记3
查看>>
【Linux】Linux 在线安装yum
查看>>
Atom 编辑器系列视频课程
查看>>