Ios 在AVAudioplayer中,一次播放多首歌曲。如何修复一次播放一首歌曲的问题
我正在尝试以编程方式创建Ios 在AVAudioplayer中,一次播放多首歌曲。如何修复一次播放一首歌曲的问题,ios,iphone,objective-c,Ios,Iphone,Objective C,我正在尝试以编程方式创建AVAudioplayer。音频播放器已成功播放。但我有一些问题。我正在UITableview中显示12个音频项目。如果我单击第一个应导航音频播放器视图控制器的项目。然后单击播放按钮,歌曲将播放。如果我单击后退按钮,歌曲将持续播放。但是如果我再次单击音频视图控制器的同一项,视图将显示初始状态,如进度条不应移动,当前时间和歌曲持续时间为空。但歌曲将连续播放。 还有一个问题。如果我单击第一项,相应的歌曲将播放。我不应该单击任何“通过”按钮。我单击“上一步”按钮,然后单击“第二
AVAudioplayer
。音频播放器已成功播放。但我有一些问题。我正在UITableview中显示12个音频项目。如果我单击第一个应导航音频播放器视图控制器的项目。然后单击播放按钮,歌曲将播放。如果我单击后退按钮,歌曲将持续播放。但是如果我再次单击音频视图控制器的同一项,视图将显示初始状态,如进度条不应移动,当前时间和歌曲持续时间为空。但歌曲将连续播放。
还有一个问题。如果我单击第一项,相应的歌曲将播放。我不应该单击任何“通过”按钮。我单击“上一步”按钮,然后单击“第二项”。如果我单击“第二项”,音频视图控制器将显示。我单击“播放”按钮,歌曲显示。在后台,第一首歌曲播放,第二首歌曲也播放。这是我的第二个问题。如何解决这两个问题。请任何人都帮我
-(void)playOrPauseButtonPressed:(id)sender
{
if(playing==NO)
{
[playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];
// Here Pause.png is a image showing Pause Button.
NSError *err=nil;
AVAudioSession *audioSession=[AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSLog(@"%@ %d",urlsArray,selectedIndex);
NSString *sourcePath=[urlsArray objectAtIndex:selectedIndex];
NSData *objectData=[NSData dataWithContentsOfURL:[NSURL URLWithString:sourcePath]];
audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&err];
audioPlayer.numberOfLoops = 0;
[audioPlayer prepareToPlay];
audioPlayer.delegate=self;
[audioPlayer play];
playing=YES;
}
else if (playing==YES)
{
[playButton setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[audioPlayer pause];
playing=NO;
}
if (self.audioPlayer)
{
[self updateViewForPlayerInfo];
[self updateViewForPlayerState];
[self.audioPlayer setDelegate:self];
}
}
-(void)updateViewForPlayerInfo
{
self.songDuration.text = [NSString stringWithFormat:@"%d:%02d", (int)self.audioPlayer.duration / 60, (int)self.audioPlayer.duration % 60, nil];
NSLog(@"%f", self.audioPlayer.duration);
self.progressBar.maximumValue = self.audioPlayer.duration;
self.volumeSlider.value = self.audioPlayer.volume;
}
-(void)updateViewForPlayerState
{
[self updateCurrentTime];
if (self.updatedTimer)
{
[self.updatedTimer invalidate];
}
if (self.audioPlayer.playing)
{
self.updatedTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateCurrentTime) userInfo:self.audioPlayer repeats:YES];
}
}
-(void)updateCurrentTime
{
//NSLog(@"self.audioPlayer.currentTime = %f", self.audioPlayer.currentTime);
self.currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)self.audioPlayer.currentTime / 60, (int)self.audioPlayer.currentTime % 60, nil];
self.progressBar.value = self.audioPlayer.currentTime;
}
-(void)volumeSliderMoved:(UISlider *)sender
{
self.audioPlayer.volume=[sender value];
}
简言之,发生的情况如下:
我认为问题就在这里
audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&err];
每次创建audioPlayer对象时。所以我的建议是在创建像这样的对象之前检查audioPlayer
if(audioPlayer)
{
audioPlayer.delegate=nil;
audioPlayer=nil;
}
然后创建对象 在使用AVAudioPlayer时,我使用一个属性来跟踪玩家。通过查看您的代码,我不清楚您是否在保留播放机,或者您是否没有每次都实例化一个新实例。根据您的描述,听起来好像您正在实例化一个新实例—不要认为您需要这样做 所以在您的.h文件中,类似这样的内容:
@property (nonatomic, strong) AVAudioPlayer *myAVAudioPlayer;
然后是.m文件中相应的@synthesis
当你想从一首歌曲移动到下一首歌曲时,只需停止播放当前歌曲,然后使用self.myAVAudioPlayer属性的init重新加载一个新的URL。然后必须再次调用AVAudioPlayer选项的prepareToPlay方法
你可能会通过不管理关于玩或不玩的变量(看起来你是在你的帖子中这样做的),让自己变得更容易。使用您的属性并通过访问setter/getter检查播放器状态的属性:
if(!self.myAVAudioPlayer)
{
// allocate and initialize using the code you have
[self.myAVAudioPlayer prepareToPlay];
} else
if ([self.myAVAudioPlayer isPlaying])
{
//do whatever you need for isPlaying - looks like pause
} else
if (![self.myAVAudioPlayer isPlaying])
{
// do whatever you need to do if the player isn't playing when the button is pressed
// at this point you will likely just re-init with a new URL and reset other items
// don't forget to call -prepareToPlay again
}
这里是一个链接,指向另一种利用AVPlayerItem-->单击的方法。感谢您的回复。是的,我正在使用ARC。但我无法理解您的帖子。请简要告诉我或给出任何示例如何解决我的问题。提前感谢…..将应用程序代理中的播放器作为属性,并始终使用它,而不是self.audioplayer。从技术上讲,这样你就不能再创作和演奏一次了。帮帮我,任何人。我对这个问题感到震惊。帮帮我,谢谢你的回答。我用的是你的逻辑。但仍然有同样的问题。请帮帮我,兄弟。我被困在这个问题中使用这是你同时播放两首歌曲的问题是否解决了?你是否仍在一次又一次地分配AVAudioPlayer对象?没有兄弟。我只创建了一次AVAudioplayer对象,你通过检查播放值来决定吗??我是对的??如果是,您是否检查了它是否进入相同的if循环。尝试调试或打印在NSLog中播放的值。