zoukankan      html  css  js  c++  java
  • ios 音乐播放

    #import "ViewController.h"

    #import <AVFoundation/AVFoundation.h>

     

    @interface ViewController ()

     

    @property(nonatomic,strong)AVAudioPlayer*player;

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        //添加两个按钮

        UIButton *playBtn=[UIButton buttonWithType:UIButtonTypeCustom];

        playBtn.backgroundColor=[UIColor grayColor];

        playBtn.frame=CGRectMake(10, 10, 100, 50);

        [self.view addSubview:playBtn];

        [playBtn addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside];

        

        UIButton *pauseBtn=[UIButton buttonWithType:UIButtonTypeCustom];

        pauseBtn.backgroundColor=[UIColor grayColor];

        pauseBtn.frame=CGRectMake(10, 70, 100, 50);

        [self.view addSubview:pauseBtn];

        [pauseBtn addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];

        

        

    }

     

    //懒加载初始化音乐播放类

    -(AVAudioPlayer *)player

    {

        if (!_player) {

            NSString *path=[[NSBundle mainBundle]pathForResource:@"我爱你你却爱着她.mp3" ofType:nil];

            NSURL *url=[NSURL fileURLWithPath:path];

            //创建播放器对象

            AVAudioPlayer *player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];

            //记录

            _player=player;

            //缓存准备

            [_player prepareToPlay];

        }

        return _player;

    }

    //开始播放

    -(void)playMusic

    {

        [self.player play];

    }

    //暂停

    -(void)pause

    {

        [self.player pause];

    }

     

     

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

    }

     

    @end

  • 相关阅读:
    剑指offer-二叉树的深度
    剑指offer-二叉树中和为某一值的路径
    剑指offer-数组中只出现一次的数字
    剑指offer-反转单词顺序列
    剑指offer-数字在排序数组中出现的次数
    剑指offer-第一个只出现一次的字符
    剑指offer-连续子数组的最大和
    剑指offer-数组中的逆序对
    CSS3滚动条美化,CSS3滚动条皮肤
    mouseover事件与mouseenter事件的区别
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4662541.html
Copyright © 2011-2022 走看看