Objective c 播放第二声
我试图做一个简单的基于AVAudioPlayer的应用程序,在按下按钮时播放2种不同的音乐,有2个视图,第一个是home view包含2个按钮,每个按钮设置一首歌曲,命名为integer(1.mp3,2.mp3…等),下面是代码Objective c 播放第二声,objective-c,ios,xcode,ios4,Objective C,Ios,Xcode,Ios4,我试图做一个简单的基于AVAudioPlayer的应用程序,在按下按钮时播放2种不同的音乐,有2个视图,第一个是home view包含2个按钮,每个按钮设置一首歌曲,命名为integer(1.mp3,2.mp3…等),下面是代码 #import "podHome.h" #import "podMusic.h" #import "ArabAppDelegate.h" @implementation podHome @synthesize song1; @synthesize tabi;
#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];
}
这两个(iActions)链接到两个按钮,当按下其中一个按钮时,它将切换到另一个视图并开始播放歌曲
- (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;
}
但问题是,当歌曲播放时,当我回到主视图并按下另一首歌曲的按钮时,它同时开始播放第一首歌曲,我认为第一首歌曲应该停止播放,开始播放另一首歌曲,我应该释放什么来做呢?我猜你有两个AVAudioPlayer实例,当你让他们都玩的时候,他们都玩 一个解决方案是在你激活一个新的玩家时告诉其他玩家停止,但随着玩家数量的增加,这会很快变得麻烦
而是你最好只设置一个播放器,并根据按下的按钮更改它的歌曲。这样,就不可能同时播放两首音乐曲目。在您的第一个按钮操作中,这样写
if(player2.isPlaying)
{
[player2 stop];
}
if(player1.isPlaying)
{
[player1 stop];
}
以同样的方式在第二个按钮动作中做同样的事情,就像这样
if(player2.isPlaying)
{
[player2 stop];
}
if(player1.isPlaying)
{
[player1 stop];
}
有趣的函数名…:)