简体   繁体   English

播放音乐和切换视图时出现问题[iPhone SDK]

[英]problem with playing music and switch views [iPhone SDK]

iam using AVFoundation Framework to play mp3 file ,everything works great ، i have 2 buttons and i put they together , [Play and resume with .hidden function],when user play music [play button going to hide and resume button un hide], so if the user change view , (music is playing) and back to previews view, the buttons don't work normally it means just play music and resume doesn't work , i put my codes to the viewDidLoad , and i know it's because of that but how can i solve this problem ? 我使用AVFoundation Framework播放mp3文件,一切正常,我有2个按钮,我将它们放在一起[使用.hidden功能播放和继续播放],当用户播放音乐时[播放按钮将隐藏并继续播放按钮隐藏],因此,如果用户更改view,(正在播放音乐)并返回到预览视图,则按钮将无法正常工作,这意味着仅播放音乐而无法继续播放,我将代码放入了viewDidLoad中,我知道这是因为其中,但我该如何解决这个问题?

   NSString * musicSonati = [[NSBundle mainBundle] pathForResource:@"music"      ofType:@"mp3"];
myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicSonati] error:NULL];
myMusic.delegate = self;
myMusic.numberOfLoops = -1; 
pauseM.hidden = YES;
play.hidden = NO;

My guess is that a big part of your problem is that your "previews view" is handling all the responsibility for playing the sounds through the AVAudioPlayer framework. 我的猜测是问题的很大一部分是您的“预览视图”正在处理通过AVAudioPlayer框架播放声音的所有责任。 What you want to do instead is redesign your app, so there is a proxy object handling AVAudioPlayer . 相反,您要做的是重新设计应用程序,因此有一个处理AVAudioPlayer代理对象 Create and initalize this proxy object in your app delegate, and then pass it along to the root view controller, and through to any other view controllers that need to handle sound playing. 在您的应用程序委托中创建并初始化此代理对象,然后将其传递给根视图控制器,再传递给需要处理声音播放的任何其他视图控制器。

With that redesigned, your "previews view" merely has to ask (in viewDidLoad ) your sound proxy object if it's playing a sound (whether by interrogation, or by registering to receive NSNotification s from the object) and set its own state (ie buttons) accordingly. 经过重新设计,您的“预览视图”只需询问(在viewDidLoad )声音代理对象是否正在播放声音(无论是通过审讯还是通过注册以接收来自对象的NSNotification )并设置其自己的状态(即按钮)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM