如何在iOS中暂停音频及其计时器滑块?
我正在使用AVAudioPlayer类来播放音频。我已经实现了一个计时器滑块,随着音乐的播放而进行 这是我的密码:如何在iOS中暂停音频及其计时器滑块?,ios,objective-c,Ios,Objective C,我正在使用AVAudioPlayer类来播放音频。我已经实现了一个计时器滑块,随着音乐的播放而进行 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. AudioBool = YES; } - (IBAction)play:(id)sender { // Code t
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AudioBool = YES;
}
- (IBAction)play:(id)sender
{
// Code to read the file from resource folder and sets it in the AVAudioPlayer
// Sets the audio timer in 1 sec intervals
sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
// Sets the slider maximum value
slider.maximumValue = player.duration;
// Sets the valueChanged target
[slider addTarget:self action:@selector(sliderChanged : ) forControlEvents : UIControlEventValueChanged];
// Play the audio
// [player prepareToPlay];
[player play];
if(AudioBool == YES)
{
[player play];
AudioBool = NO;
}
else
{
[player pause];
AudioBool = YES;
}
}
- (void)updateTime
{
// Updates the slider about the music time
slider.value = player.currentTime;
NSString *time = [self timeFormatted:slider.value];
self.timerLabe.text = time;
}
- (NSString *)timeFormatted:(int)totalSeconds
{
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
//int hours = totalSeconds / 3600;
//return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
- (IBAction)sliderChanged : (UISlider *)sender
{
// skips music with slider changged
[player pause];
[player setCurrentTime:slider.value];
// [player prepareToPlay];
[player play];
}
// Stops the timer when audio finishes
- (void)audioPlayerDidFinishPlaying : (AVAudioPlayer *)player successfully :
(BOOL)flag
{
// Music completed
if (flag)
{
[sliderTimer invalidate];
}
}
我有两个问题:
感谢您尝试此解决方案,您基本上需要更改播放方法。在viewDidLoad中移动滑块初始化,同时根据ISPLAY属性(代码中的AudioBool属性)播放/暂停
#导入
@界面视图控制器()
@属性(非原子,强)AVAudioPlayer*audioPlayer;
@属性(非原子)布尔显示;
@属性(非原子,强)NSTimer*sliderTimer;
@属性(弱、非原子)IBUISLIDER*滑块;
@属性(弱,非原子)IBUILabel*timerLabel;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
NSBundle*mainBundle=[NSBundle mainBundle];
NSString*文件路径=[mainBundle pathForResource:@“10101”类型:@“mp3”];
NSURL*fileURL=[NSURL fileURLWithPath:filePath];
self.isplay=否;
n错误*错误;
self.audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:fileURL错误:&error];
[self.audioPlayer prepareToPlay];
[self.slider addTarget:self action:@selector(sliderChanged:)for controlEvents:UIControlEventValueChanged];
self.slider.minimumValue=0;
self.slider.maximumValue=self.audioPlayer.duration;
}
-(iAction)播放:(id)发送方{
if(自显示)
{
//音乐正在播放
[self.audioPlayer暂停];
self.isplay=!self.isplay;
}
其他的
{
//音乐当前已暂停/停止
[self.audioPlayer play];
self.isplay=!self.isplay;
self.sliderTimer=[NSTimer scheduledTimerWithTimeInterval:1.0目标:自选择器:@selector(updateTime)userInfo:nil repeats:YES];
}
}
-(void)滑块更改:(UISlider*)发送器
{
//使用滑块切换跳过音乐
[self.audioPlayer暂停];
[self.audioPlayer setCurrentTime:self.slider.value];
[self.audioPlayer play];
}
-(void)更新时间
{
//更新有关音乐时间的滑块
self.slider.value=self.audioPlayer.currentTime;
NSString*time=[self timeFormatted:self.slider.value];
self.timerLabel.text=时间;
}
-(NSString*)时间格式:(int)总秒数
{
整数秒=总秒数%60;
整数分钟=(总秒/60)%60;
返回[NSString stringWithFormat:@“%02d:%02d”,分,秒];
}
//音频结束时停止计时器
-(无效)AudioPlayerDifinishPlaying:(AVAudioPlayer*)玩家成功:(BOOL)标志
{
//音乐完成
国际单项体育联合会(旗)
{
[self.slider无效];
}
}
谢谢,我稍后会尝试,顺便问一下,您为什么需要准备播放?我在代码中注释了它,它仍然有效。
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic) BOOL isPlaying;
@property (nonatomic, strong) NSTimer *sliderTimer;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"10101" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
self.isPlaying = NO;
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
[self.audioPlayer prepareToPlay];
[self.slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
self.slider.minimumValue = 0;
self.slider.maximumValue = self.audioPlayer.duration;
}
- (IBAction)play:(id)sender {
if (self.isPlaying)
{
// Music is currently playing
[self.audioPlayer pause];
self.isPlaying = !self.isPlaying;
}
else
{
// Music is currenty paused/stopped
[self.audioPlayer play];
self.isPlaying = !self.isPlaying;
self.sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
}
}
- (void)sliderChanged:(UISlider *)sender
{
// skips music with slider changged
[self.audioPlayer pause];
[self.audioPlayer setCurrentTime:self.slider.value];
[self.audioPlayer play];
}
- (void)updateTime
{
// Updates the slider about the music time
self.slider.value = self.audioPlayer.currentTime;
NSString *time = [self timeFormatted:self.slider.value];
self.timerLabel.text = time;
}
- (NSString *)timeFormatted:(int)totalSeconds
{
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
// Stops the timer when audio finishes
- (void)audioPlayerDidFinishPlaying : (AVAudioPlayer *)player successfully:(BOOL)flag
{
// Music completed
if (flag)
{
[self.sliderTimer invalidate];
}
}