gpt4 book ai didi

ios - 如何从开始播放AVAudioplayer?

转载 作者:行者123 更新时间:2023-12-01 17:07:01 27 4
gpt4 key购买 nike

我是创建了AVAudio Player的,其播放完美,单击播放按钮的歌曲正在播放,并且其显示暂停按钮,单击暂停按钮的歌曲已暂停,并且其显示播放按钮,但是单击播放按钮从头开始播放。我要继续播放歌曲。如何控制这个问题。这是我的代码,请检查一次。

-(void)playAudio
{
// [progreetimer invalidate];
// if([audioPlayer isPlaying]==YES)`enter code here`
if(self.isPlaying)
{
[self.play setBackgroundImage:[UIImage imageNamed:@"audioplayer_play.png"]
forState:UIControlStateNormal];
NSLog(@"Pausing Music");
if (self.progreetimer) {
[self.progreetimer invalidate];
progreetimer =nil;
}
[audioPlayer pause];
self.isPlaying = NO;
}

else {

// Init audio with playback capability
[self.play setBackgroundImage:[UIImage imageNamed:@"audioplayer_pause.png"] forState:UIControlStateNormal];

// NSString *urlstr = @"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3";

NSString *urlstr =@"http://www.abstractpath.com/files/audiosamples/sample.mp3";
urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
audioPlayer.volume = 1.9f;
// [audioPlayer prepareToPlay];
SongProgressBar.maximumValue = [audioPlayer duration];
SongProgressBar.value = 0.0;
NSLog(@"Playing music");

//start a timer to update the time label display
self.progreetimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(updateTime:) userInfo:nil repeats:YES];

self.progreetimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(updateSlider) userInfo:nil repeats:YES];
[progreetimer fire];
audioPlayer.delegate=self;
[audioPlayer play];
self.isPlaying = YES;

}
}
- (void)viewDidLoad
{
[super viewDidLoad];


self.SongProgressBar.minimumValue = 30;

self.SongProgressBar.maximumValue = 30;

[self.SongProgressBar setThumbImage:[UIImage imageNamed:@"thumbslider.png"]

forState:UIControlStateNormal];

// [self.SongProgressBar setThumbImage:[UIImage imageNamed:@"slider_icon.png"]

// forState:UIControlStateHighlighted];

[self.SongProgressBar setMinimumTrackImage:[UIImage imageNamed:@"slider_max.png"]

forState:UIControlStateNormal];

[self.SongProgressBar setMaximumTrackImage:[UIImage imageNamed:@"slider_icon.png"]

forState:UIControlStateNormal];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[audioPlayer pause]; // Or pause
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[audioPlayer play];
}



- (IBAction)songprogressbar:(id)sender {
// Fast skip the music when user scroll the UISlider
[audioPlayer stop];
[audioPlayer setCurrentTime:SongProgressBar.value];
[audioPlayer prepareToPlay];
[audioPlayer play];
}

- (IBAction)backword:(id)sender {
if ([self.audioPlayer isPlaying]) {
NSTimeInterval desiredTime = audioPlayer.currentTime -15.0f;
if (desiredTime < 0) {
audioPlayer.currentTime =0.0f;
} else {
audioPlayer.currentTime = desiredTime;
}
}
}

- (IBAction)play:(id)sender {

[self playAudio];
}
- (void)updateSlider {
// Update the slider about the music time
SongProgressBar.value = audioPlayer.currentTime;
}

- (void)setCurrentAudioTime:(float)value {
[self.audioPlayer setCurrentTime:value];
}
//Stops the timer when the music is finished
- (void)audioPlayerDidFinishPlaying : (AVAudioPlayer *)player successfully : (BOOL)flag {
// Music completed
if (flag) {
[progreetimer invalidate];

NSLog(@"Finished playing the song");
}
}

- (IBAction)forword:(id)sender {

if ([audioPlayer isPlaying]) {
NSTimeInterval desiredTime = audioPlayer.currentTime +15.0f;
if (desiredTime < audioPlayer.duration) {
audioPlayer.currentTime = desiredTime;
}
}
}

- (IBAction)volume:(id)sender {
audioPlayer.volume=volume.value;
}

-(NSString*)timeFormat:(float)value{

float minutes = floor(lroundf(value)/60);
float seconds = lroundf((value) - (minutes * 60));

int roundedSeconds = lroundf(seconds);
int roundedMinutes = lroundf(minutes);

NSString *time = [[NSString alloc]
initWithFormat:@"%d:%02d",
roundedMinutes, roundedSeconds];
return time;

}
- (void)updateTime:(NSTimer *)timer {

self.starttimer.text = [NSString stringWithFormat:@"%@",
[self timeFormat: ceilf(audioPlayer.currentTime)]];


self.endtimer.text = [NSString stringWithFormat:@"-%@", [self timeFormat: (audioPlayer.duration - ceilf(audioPlayer.currentTime))]];
}

最佳答案

您应该尝试一下。参见,否则方法的一部分

-(void)playAudio
{
// [progreetimer invalidate];
// if([audioPlayer isPlaying]==YES)`enter code here`
if(self.isPlaying)
{
//Code to pause audioPlayer
[audioPlayer pause];
self.isPlaying = NO;
}

else
{
if(audioPlayer.url != nil && audioPlayer.duration != 0)
{
[audioPlayer play];
}
else
{
//Code to add new audioPlayer
}
}
}

关于ios - 如何从开始播放AVAudioplayer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29252924/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com