iOS SDK:后台播放音乐和切换视图

编程入门 行业动态 更新时间:2024-10-24 16:27:51
本文介绍了iOS SDK:后台播放音乐和切换视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在我的应用程序中播放音乐.音乐效果很好,但在切换 viewControllers 并返回主菜单后,我的音乐再次播放!这意味着几个相同的声音一起播放!我该如何解决这个问题?这是我的代码:

I am trying to play music in my application. The music works fine but after switching viewControllers and returning to the main menu, my music plays again! It means several identical sounds play together! How can I solve this? Here is my code :

- (void)viewDidLoad { NSString *music = [[NSBundle mainBundle] pathForResource:@"1music" ofType:@"mp3"]; myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL]; myMusic.delegate = self; myMusic.numberOfLoops = -1; [myMusic play]; } - (IBAction) scoreView { ScoreViewController *scoreView = [[ScoreViewController alloc] initWithNibName:@"ScoreViewController" bundle:nil]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; [self.view addSubview: scoreView.view]; [UIView commitAnimations]; }

编辑代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL]; myMusic.delegate = self; myMusic.numberOfLoops = -1; [myMusic play]; } return self; } //toggle button - (IBAction)MusicPlaying:(id)sender { if ((isPlay = !isPlay)) { UIImage *buttonImageNormal = [UIImage imageNamed:@"play.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [MusicButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; [myMusic pause]; }else { UIImage *buttonImageNormal = [UIImage imageNamed:@"pause.png"]; UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [MusicButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; NSLog(@"Music play"); [myMusic play]; } }

推荐答案

确保将 myMusic 正确声明为保留属性并合成,然后尝试如下操作:

Make sure myMusic is correctly declared as a retained property and synthesized then try something like this:

-(void) commonInit { NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; self.myMusic = [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL] autorelease]; self.myMusic.delegate = self; self.myMusic.numberOfLoops = -1; //You probably don't want to play music on init, rather defer to viewDidLoad. // [self.myMusic play]; } - (id)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { [self commonInit]; } return self; } - (id)init { self = [super init]; if (self) { [self commonInit]; } return self; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { [self commonInit]; } return self; }

然后在您的 viewDidLoad 中试试这个:

Then try this in your viewDidLoad:

- (void)viewDidLoad { ... if (!self.myMusic.playing) { [self.myMusic play]; } ... }

另外,不要忘记在 dealloc 中释放你的播放器!

Also, don't forget to release your player in dealloc!

更多推荐

iOS SDK:后台播放音乐和切换视图

本文发布于:2023-07-11 09:10:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   后台   音乐   iOS   SDK

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!