我正在尝试做一个简单的基于AVAudioPlayer的应用程序,按下按钮时播放2个不同的音乐,有2个视图,第一个是主视图包含2个按钮,每个按钮设置一首名为整数的歌曲(1.mp3) ,2.mp3 .....等)这里是代码
#import "podHome.h"
#import "podMusic.h"
#import "ArabAppDelegate.h"
@implementation podHome
@synthesize song1;
@synthesize tabi;
int CurrentPlay;
NSString *Currenttxt;
-(IBAction)uae{
CurrentPlay=1;
Currenttxt=@"uae";
podMusic *newContro=[[podMusic alloc] init];
[newContro setCurrentPlay1:CurrentPlay setCurrentText:Currenttxt];
ArabAppDelegate *theDelegate = (ArabAppDelegate*)[[UIApplication sharedApplication] delegate];
tabi = theDelegate.tabcontrolPod;
tabi.selectedIndex = 1;
[newContro release];
}
-(IBAction)libya{
CurrentPlay=2;
Currenttxt=@"uae";
podMusic *newContro=[[podMusic alloc] init];
[newContro setCurrentPlay1:CurrentPlay setCurrentText:Currenttxt];
ArabAppDelegate *theDelegate = (ArabAppDelegate*)[[UIApplication sharedApplication] delegate];
tabi = theDelegate.tabcontrolPod;
tabi.selectedIndex = 1;
[newContro release];
}
这两个按钮(IBActions)连接到两个按钮,当按下其中一个按钮时,它将改变到另一个视图并开始播放该歌曲
- (void)viewWillAppear:(BOOL)animated {
if((played == 1)&&(isBacked==FALSE)){
NSString *filePath = [[NSBundle mainBundle] pathForResource:texting
ofType:@"txt"];
NSString *filenameString = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:nil];
CurrentTex.text = filenameString;
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(YES);
playBtnBG = [[UIImage imageNamed:@"play-pod.png"] retain];
pauseBtnBG = [[UIImage imageNamed:@"pause-pod.png"] retain];
[playButton setBackgroundImage:pauseBtnBG forState:UIControlStateNormal];
[self registerForBackgroundNotifications];
updateTimer = nil;
duration.adjustsFontSizeToFitWidth = YES;
currentTime.adjustsFontSizeToFitWidth = YES;
progressBar.minimumValue = 0.0;
NSString *path=[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",CurrentPlay] ofType:@"mp3"];
self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[player stop];
//self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
if (self.player)
{
[self updateViewForPlayerInfo:player];
[self updateViewForPlayerState:player];
player.numberOfLoops = 0;
player.delegate = self;
}
[self startPlaybackForPlayer:player];
fileName.text= [[NSString alloc]initWithFormat:@"%@", songName];
// [fileURL release];
// CurrentPlay = 0;
isBacked = TRUE;
}
[super viewWillAppear:animated];
}
- (void)setCurrentPlay1:(int)varP setCurrentText:(NSString *)varT{
CurrentPlay = varP;
texting = varT;
played = 1;
isBacked = FALSE;
}
但问题是,当歌曲正在播放并回到主视图并按下另一首歌曲的按钮时,它会同时开始播放第一首歌曲,我认为第一首应该停止开始另一首歌曲,我应该释放什么呢?
答案 0 :(得分:0)
我的猜测是你有两个AVAudioPlayer实例,当你把它们都设置为播放时,它们都会这样做!
一种解决方案是告诉其他玩家在激活新玩家时停止,但随着玩家数量的增加,这很快就会变得麻烦。
相反,你最好只设置一个播放器,并根据按下的按钮更改它的歌曲。这样,两个音乐曲目就不可能同时播放。
答案 1 :(得分:0)
在你的第一个按钮动作中写这样的。
if(player2.isPlaying)
{
[player2 stop];
}
以同样的方式在第二个按钮动作中做同样的事情。
if(player1.isPlaying)
{
[player1 stop];
}