//////////AVAudioPlayer////////////
#import <AVFoundation/AVFoundation.h>
#import "ViewController.h"
@interface ViewController ()
{
AVAudioPlayer *audioPlayer;
}
@property (weak, nonatomic) IBOutlet UISlider *volumeSlider;
@property (weak, nonatomic) IBOutlet UISlider *currentSlider;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
- (IBAction)volumeSlider:(UISlider *)sender;
- (IBAction)currentSlider:(UISlider *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//找到本地音频文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"天涯—张利鹏" ofType:@"mp3"];
//包装成url,调用本地文件使用fileURLWithPath方法 远程文件调用使用URLWithString
NSURL *url = [NSURL fileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
//准备播放 也可以不写
[audioPlayer prepareToPlay];
//遇到play就播放
// if ([audioPlayer play]) {
// NSLog(@"开始播放音乐了");
// }
//设置滑块的区间范围
_volumeSlider.minimumValue = 0;
_volumeSlider.maximumValue = 1;
_currentSlider.minimumValue = 0;
_currentSlider.maximumValue = audioPlayer.duration; //总时长
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (void)timerAction:(NSTimer *)timer {
if (audioPlayer.playing) {
NSString *currentTime = [NSString stringWithFormat:@"%.0f",audioPlayer.currentTime];
_timeLabel.text = currentTime;
_currentSlider.value = audioPlayer.currentTime;
}
}
- (IBAction)currentSlider:(UISlider *)sender {
//设置当前的播放进度时间
audioPlayer.currentTime = sender.value;
}
- (IBAction)volumeSlider:(UISlider *)sender {
//设置当前的播放进度时间
audioPlayer.currentTime = sender.value;
}
- (IBAction)playAction:(UIButton *)sender {
//播放器是否正在播放
BOOL playing = audioPlayer.playing;
if (playing) { //正在播放
//暂停
[audioPlayer pause];
[sender setTitle:@"播放" forState:UIControlStateNormal];
} else {
[audioPlayer play];
[sender setTitle:@"暂停" forState:UIControlStateNormal];
}
}
@end
//////////AVPlayer////////////
修改- (void)viewDidLoad代码如下
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error;
//设置音频会话支持后台播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
//播放远程地址
NSString *mp3Url = @"http://mp3a.9ku.com:1234/qzonemp3/chenchusheng001.mp3";
NSURL *url = [NSURL URLWithString:mp3Url];
player = [[AVPlayer alloc] initWithURL:url];
[player play];//获取流媒体数据播放
}
运行
将
//播放远程地址
NSString *mp3Url = @"http://music.baidutt.com/up/kwcawcca/ydsspc.mp3";
NSURL *url = [NSURL URLWithString:mp3Url];
player = [[AVPlayer alloc] initWithURL:url];
[player play];//获取流媒体数据播放
注释掉 在其下加入
//播放本地文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"第一夫人" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];
player = [[AVPlayer alloc] initWithURL:url];
[player play];
运行
//先导入AudioToolbox.framework框架
- (IBAction)playAction:(UIButton *)sender{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"44th Street Medium" ofType:@"caf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
//unsigned long soundId;
SystemSoundID soundId;
//为url地址注册系统声音
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundId);
//播放系统声音
AudioServicesPlaySystemSound(soundId);
***********播放震动 (模拟器无法模拟震动)*************
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}