12344

来源:互联网 发布:凯佰赫战盾数据 编辑:程序博客网 时间:2024/06/11 12:12

PlaySongViewController.h 继承UIViewController

@property (nonatomic,strong)Music *ss;

.m

#import "PlaySongViewController.h"#import "SingerView.h"#import "AppDelegate.h"#import "CollectViewController.h"{    SingerView *ppSong;    AppDelegate *app;    NSArray *arr;    BOOL flag;    NSInteger theSliderValue,theCurrentTime;    NSTimer *theTimer;}- (void)viewWillDisappear:(BOOL)animated{    //停止音乐播放    [app.appPlayer pause];}- (void)viewDidLoad {    [super viewDidLoad];    self.title=[NSString stringWithFormat:@"%@:%@",self.ss.theName,self.ss.theSinger];    ppSong=[[SingerView alloc]initWithFrame:self.view.frame];    self.view=ppSong;    [ppSong.btnPlay addTarget:self action:@selector(btnPlay:) forControlEvents:UIControlEventTouchUpInside];    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];    app.appPlayer = [[AVAudioPlayer alloc]initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.ss.theName ofType:@"mp3"]] error:nil];    [app.appPlayer play];    theTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:YES];    flag=0;    UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"qq1@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(clickRight)];    self.navigationItem.rightBarButtonItem = right;}-(void)clickRight{    CollectViewController * collect = [[CollectViewController alloc]init];    [self.navigationController pushViewController:collect animated:YES];}-(void)sliderClick{    [theTimer invalidate];    flag=1;    theSliderValue=ppSong.slider.value;    [self sliderTimerMethod];}-(void)sliderTimerMethod{    if (flag) {        app.appPlayer.currentTime=theSliderValue * app.appPlayer.duration;        theCurrentTime=app.appPlayer.currentTime;        [theTimer fire];    }    else    {        ppSong.slider.value=app.appPlayer.currentTime/app.appPlayer.duration;    }}-(void)btnPlay:(UIButton *)sender{    if(sender.isSelected==NO)    {        sender.selected=YES;        //停止播放        [app.appPlayer stop];    }    else    {        sender.selected=NO;        //开始播放        [app.appPlayer play];    }}-(void)downClick{}-(void)timer{    flag=0;    [self sliderTimerMethod];}

NnameView.h

@interface NnameView : UIView@property(nonatomic,strong)UITableView *theTableView;

.m
//单例方法

-(instancetype)initWithFrame:(CGRect)frame{    if (self=[super initWithFrame:frame])    {        [self addSubview:self.theTableView];    }    return self;}-(UITableView *)theTableView{    if (!_theTableView)    {        _theTableView=[[UITableView alloc]initWithFrame:self.frame];    }    return _theTableView;}

SingerView.h

@interface SingerView : UIView@property(nonatomic,strong)UIImageView *imgView;@property(nonatomic,strong)UIView *smallView;@property(nonatomic,strong)UISlider *slider;@property(nonatomic,strong)UIButton *btnDownLoad,*btnLike,*btnRound,*btnUp,*btnPlay,*btnDown;

.m

//单例方法-(instancetype)initWithFrame:(CGRect)frame{    if (self=[super initWithFrame:frame]) {        [self addSubview:self.imgView];        [self addSubview:self.smallView];    }    return self;}-(UIImageView *)imgView{    if (!_imgView) {        _imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.frame.size.width, self.frame.size.height-64-150)];        _imgView.image=[UIImage imageNamed:@"IMG_0037.JPG"];    }    return _imgView;}-(UIView *)smallView{    if (!_smallView) {        _smallView=[[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-150, self.frame.size.width, 150)];        _smallView.backgroundColor=[UIColor brownColor];        [_smallView addSubview:self.slider];        [_smallView addSubview:self.btnDown];        [_smallView addSubview:self.btnDownLoad];        [_smallView addSubview:self.btnLike];        [_smallView addSubview:self.btnPlay];        [_smallView addSubview:self.btnRound];        [_smallView addSubview:self.btnUp];    }    return _smallView;}-(UIButton *)btnDown{    if (!_btnDown) {        _btnDown=[UIButton buttonWithType:UIButtonTypeCustom];        _btnDown.frame=CGRectMake(self.frame.size.width/2-30-30+100, 90, 60, 60);        [_btnDown setImage:[UIImage imageNamed:@"bianping-yuanicon-07"] forState:UIControlStateNormal];        _btnDown.tag = 12 ;    }    return _btnDown;}-(UIButton *)btnDownLoad{    if (!_btnDownLoad) {        _btnDownLoad=[UIButton buttonWithType:UIButtonTypeCustom];        _btnDownLoad.frame=CGRectMake(300, 10, 40, 40);        [_btnDownLoad setImage:[UIImage imageNamed:@"bianping-yuanicon-08"] forState:UIControlStateNormal];    }    return _btnDownLoad;}-(UIButton *)btnLike{    if (!_btnLike) {        _btnLike=[UIButton buttonWithType:UIButtonTypeCustom];        _btnLike.frame=CGRectMake(340, 10, 40, 40);        [_btnLike setImage:[UIImage imageNamed:@"bianping-yuanicon-09"] forState:UIControlStateNormal];    }    return _btnLike;}-(UIButton *)btnRound{    if (!_btnRound) {        _btnRound=[UIButton buttonWithType:UIButtonTypeCustom];        _btnRound.frame=CGRectMake(50, 90, 40, 40);        [_btnRound setImage:[UIImage imageNamed:@"bianping-yuanicon-10"] forState:UIControlStateNormal];    }    return _btnRound;}-(UIButton *)btnUp{    if (!_btnUp) {        _btnUp=[UIButton buttonWithType:UIButtonTypeCustom];        _btnUp.frame=CGRectMake(self.frame.size.width/2-30-80, 90, 60, 60);        [_btnUp setImage:[UIImage imageNamed:@"bianping-yuanicon-11"] forState:UIControlStateNormal];        _btnUp.tag = 11 ;    }    return _btnUp;}-(UIButton *)btnPlay{    if (!_btnPlay) {        _btnPlay=[UIButton buttonWithType:UIButtonTypeCustom];        _btnPlay.frame=CGRectMake(self.frame.size.width/2-30, 90, 60, 60);        [_btnPlay setImage:[UIImage imageNamed:@"bianping-yuanicon-12"] forState:UIControlStateNormal];        [_btnPlay setImage:[UIImage imageNamed:@"bianping-yuanicon-12"] forState:UIControlStateSelected] ;    }    return _btnPlay;}-(UISlider *)slider{    if (!_slider)    {        _slider=[[UISlider alloc]initWithFrame:CGRectMake(20, 60, self.frame.size.width-40, 30)];        _slider.minimumValue=0;        _slider.maximumValue=1;        _slider.continuous=NO;    }    return _slider;}

CollectViewController

#import "Model.h"#import "Loaddata.h"@interface CollectViewController (){    UITextField *_text1,*_text2,*_text3;    UILabel * _Lab1,*_Lab2,*_Lab3;}@end@implementation CollectViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor grayColor];#pragma mark --------------- UILable -------------------------    _Lab1 = [[UILabel alloc]initWithFrame:CGRectMake(70, 100, 70, 40)];    _Lab1.text =@"歌名:";    _Lab1.tintColor = [UIColor blackColor];    [self.view addSubview:_Lab1];    _Lab2 = [[UILabel alloc]initWithFrame:CGRectMake(70, 180, 70, 40)];    _Lab2.text =@"歌手:";    _Lab2.tintColor = [UIColor blackColor];    [self.view addSubview:_Lab2];#pragma mark --------------- UITextFiled -----------------------    _text1 = [[UITextField alloc]initWithFrame:CGRectMake(130, 100, 135, 40)];    _text1.borderStyle = UITextBorderStyleRoundedRect;    _text1.backgroundColor = [UIColor orangeColor];    [self.view addSubview:_text1];    _text2 = [[UITextField alloc]initWithFrame:CGRectMake(130, 180, 135, 40)];    _text2.borderStyle = UITextBorderStyleRoundedRect;    _text2.backgroundColor = [UIColor orangeColor];    [self.view addSubview:_text2];    //保存按钮    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];    saveBtn.frame = CGRectMake(160,320,70,40);    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];    saveBtn.backgroundColor = [UIColor blueColor];    [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:saveBtn];}//添加-(void)saveClick{    Model * mod = [[Model alloc]init];    mod.name = _text1.text;    mod.singer = _text2.text;    [[Loaddata shareData]insertData:mod];    //弹出提示框    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"保存成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];    [alert show];    [self.navigationController popViewControllerAnimated:YES];}

AppDelegate.h

#import <CoreData/CoreData.h>#import <AVFoundation/AVFoundation.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property(nonatomic,strong)AVAudioPlayer *appPlayer;@property (strong, nonatomic) UIWindow *window;@property (readonly, strong) NSPersistentContainer *persistentContainer;- (void)saveContext;

.m

ViewController * view = [[ViewController alloc]init];    UINavigationController *thenav = [[UINavigationController alloc]initWithRootViewController:view];    self.window.rootViewController = thenav;

ViewController.m

#import "Music.h"#import "NnameView.h"#import "SingerView.h"#import "PlaySongViewController.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UITabBarDelegate>{    NnameView *sList;    NSMutableArray *arr;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.title= @"全部歌曲";    arr = [NSMutableArray array];    sList = [[NnameView alloc]initWithFrame:self.view.frame];    self.view = sList;    sList.theTableView.dataSource=self;    sList.theTableView.delegate=self;//    sList.theTableView.backgroundColor = [UIColor grayColor] ;    Music *dd=[[Music alloc]init];    arr = [dd getSongArr];    // arr = [self getArr];#pragma mark ---------- Left and Right Button -------------    UIBarButtonItem * left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"001"] style:UIBarButtonItemStyleDone target:self action:@selector(clickLeft)];    self.navigationItem.leftBarButtonItem = left;    UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"002"] style:UIBarButtonItemStyleDone target:self action:@selector(clickRight)];    self.navigationItem.rightBarButtonItem = right;#pragma mark --------- UITabbar -----------    // 创建tabBar对象    UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, 44)];    tabBar.barTintColor = [UIColor whiteColor];    // 设置代理    tabBar.delegate = self;    [self.view addSubview:tabBar];    // 设置tabBar上的按钮    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"单曲" image:[UIImage imageNamed:@"recentPlay@2x"] tag:10];    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"歌手" image:[UIImage imageNamed:@"tab_find_selected@2x"] tag:11];    UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"专辑" image:[UIImage imageNamed:@"yinyueguan_click@2x"] tag:12];    tabBar.items = @[item,item2,item3];}//  Button 方法-(void)clickLeft{}-(void)clickRight{}//展示出来#pragma mark - UITableViewDataSource-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return arr.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellid"];    if (!cell) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellid"];    }    cell.textLabel.text=[arr[indexPath.row]objectForKey:@"name"];    cell.detailTextLabel.text=[arr[indexPath.row]objectForKey:@"singer"];    cell.backgroundColor = [UIColor clearColor] ;    return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    PlaySongViewController *theVc =[[PlaySongViewController alloc]init];    [self.navigationController pushViewController:theVc animated:YES];    theVc.ss=[[Music alloc]init];    theVc.ss.theName=[arr[indexPath.row]objectForKey:@"name"];    theVc.ss.theSinger=[arr[indexPath.row]objectForKey:@"singer"];}//表格每一行高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row == 0) {        return 90;    }    if (indexPath.row == 1) {        return 90;    }    if (indexPath.row == 2) {        return 90;    }    else{        return 90;    }    return 0;}

Loaddata.h

#import "Model.h"@interface Loaddata : NSObject+ (Loaddata *)shareData;- (void)insertData:(Model *)insert;

.m

#import "FMDatabase.h"static Loaddata *ld =nil;FMDatabase *base;@implementation Loaddata+ (Loaddata *)shareData {    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        ld = [[Loaddata alloc] init];        [ld initData];    });    return ld;}+ (instancetype)allocWithZone:(struct _NSZone *)zone {    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        ld = [super allocWithZone:zone];    });    return ld;}- (id)copy {    return self;}- (id)mutableCopy {    return self;}// 创建数据库- (void)initData {    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];    NSString *newPath = [path stringByAppendingPathComponent:@"book.sqlite"];    NSLog(@"===== %@",newPath);    base = [[FMDatabase alloc] initWithPath:newPath];    if ([base open]) {        [base executeUpdate:@"create table class (ID integer primary key, name text, singer text)"];    }    else {        NSLog(@"创建失败 !");    }    [base close];}- (void)insertData:(Model *)insert {    if ([base open]) {        [base executeUpdate:[NSString stringWithFormat:@"insert into class values (null,'%@','%@')",insert.name,insert.singer]];    }    else {        NSLog(@"添加失败 !");    }    //BOOL值接受    BOOL ii = [base executeUpdate:[NSString stringWithFormat:@"insert into class values (null,'%@','%@')",insert.name,insert.singer]];    //判断    if (ii)    {        NSLog(@"成功");    }    else    {        NSLog(@"失败");    }    [base close];}

Model.h

@property(nonatomic,strong)NSString *name,*singer;@property(nonatomic,assign)NSInteger *ID;