标签:
视频音频播放
添加框架AVFoundation.framework
引入#import <AVFoundation/AVFoundation.h>
一:音乐播放
创建音频播放器
AVAudioPlayer *_audio
//获取本地音乐
NSData *audioData=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"]]; _audio=[[AVAudioPlayer alloc]initWithData:audioData error:nil]; //音量 _audio.volume=1; //设置播放速率 _audio.enableRate=YES; //1.0正常 0.5减半 2.0两倍 _audio.rate=1; //设置声道 //-1.0左声道,0.0双声道,1.0右声道 _audio.pan=0.0; //如果音频没有播放,currentTime指从30秒位置开始播放 //如果音频正在播放,currentTime指调到30秒的位置播放 //currentTime可以理解为当前播放时间,获取播放时间时也是currentTime _audio.currentTime=30; //播放 [_audio play];
//获取在线音乐
NSURL *url=[NSURL URLWithString:@"http://localhost:8080/downloadSrver/1.mp3"]; NSURLRequest *request=[NSURLRequest requestWithURL:url]; NSData *data=(NSData *)[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; _audio=[[AVAudioPlayer alloc]initWithData:data error:nil];
//根据播放状态暂停
if (_audio.playing==YES) { [_audio pause]; [button setTitle:@“continue” forState:UIControlStateNormal]; } else { [_audio play]; [button setTitle:@"pause" forState:UIControlStateNormal]; }
//停止播放
[_audio stop];
标签:
原文地址:http://www.cnblogs.com/kyuubee/p/4825682.html