音频播放:

音频播放:_第1张图片


//使用系统提供的音频播放器先要导入系统

#import

@interfaceFirstViewController :UIViewController

{

AVAudioPlayer*player;

BOOLisPlay;

UISlider*silder;

NSTimer*timer;

}

@end



//第一步先在工程中加入一个音频文件

slider = [[UISlider  alloc]initWithFrame: CGRectMake(100,100,200,100)];

[self.view  addSubView :silder ];

UIButton *btn=[UIButton  buttonWithType: UIButtonTypeSystem];

btn.bounds = CGRectMake (160, 240 );

[btn  setTitle :@"播放"  forState : UIControlStateNomal ];

[btn  addTarget: self  action: @selector (btnClick:)  forControlEvents:UIControlEventTouchupInside  ];

[self .view  addSubview : btn ];

-(void )btnClick: (UIButton *)btn

{
   if (isPlay )

  {      [btn  setTitle :@"暂停"   forState:UIControlstateNormal ];

[player  pause  ] ;  暂停;

isPlay = NO;

[timer  invalidate];

timer = nil;

    }else{

if (!player){   player = [[AVAudioPlayer  alloc]initWithData:[self  findFileData]error : nil];

//设置音量:0.0----1.0;

player .volume = 1.0;

player .enableRate= YES;  // 允许修改播放速度;

player .rate = 1.0;

//设置音频声道   ( -1.0左声道1.0右声道0.0立体声)

player .rate = 0.0;

//控制当前播放时间

//player.currentTime =40;

//当前音频总时间   //  player.duration ;

}

[btnsetTitle:@"播放"forState:UIControlStateNormal];

timer= [NSTimerscheduledTimerWithTimeInterval:0.1

target:selfselector:@selector(onTimer)userInfo:nilrepeats:YES];

[playerplay];

isPlay=YES;

}

}


音频播放:_第2张图片

音频播放:_第3张图片

你可能感兴趣的:(音频播放:)