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

时间:2011-12-11 21:15:48

标签: iphone ios xcode ipad

我正在尝试在我的应用程序中播放音乐。音乐工作正常,但在切换viewControllers并返回主菜单后,我的音乐再次播放!这意味着几个相同的声音一起播放!我怎么解决这个问题?这是我的代码:

- (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];

    } 

}

5 个答案:

答案 0 :(得分:5)

如果您要在多个地方播放音频,那么实现此目的的一种简单方法是在委托中声明AVAudioPlayer实例并合成属性。如果您想在任何地方播放这样做: -

MyAppDelegate *app_delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];


if(app_delegate.audioPlayer)
{
    [app_delegate.audioPlayer release];
    app_delegate.audioPlayer = nil;
}
app_delegate.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];     

如果你想停止声音,请执行以下操作: -

if(app_delegate.audioPlayer)
{
    if([app_delegate.audioPlayer isPlaying])
    {
        [app_delegate.audioPlayer stop];
        [app_delegate.audioPlayer setCurrentTime:0];
    }
}

并播放新的声音文件,如果已经分配,​​则播放nil同一播放器,或者使用新的声音文件URL再次分配。

答案 1 :(得分:4)

确保myMusic被正确地声明为保留属性并合成,然后尝试这样的事情:

-(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中尝试这个:

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

另外,不要忘记以dealloc发布你的播放器!

答案 2 :(得分:1)

首先,viewDidLoad可能不是初始化AVAudioPlayer的好地方,因为系统可能需要卸载一些组件来回收内存(在viewDidUnload中)。您可能应该在AVAudioPlayer方法或类似方法中创建init

所以当焦点返回到您的应用程序时,您最终会创建第二个AVAudioPlayer,而实际上您正在丢失对第一个的引用。 (因此,泄漏记忆。

如果要在加载视图时开始播放音乐,则应在执行此操作之前另外检查其状态:

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

答案 3 :(得分:1)

它似乎正在开发游戏(来自你的函数名称之后的viewLoad(ScoreView)。;))

嗯,据我所知,如果你想用Game实现,AvAudioplayer不是好选择。

使用cocosdenshion,下载最新的cocos2d文件夹,找到这9个文件,将其放入资源文件夹,

你完成了...... !!!

#import "SimpleAudioEngine.h" // wherever you want to call in head part of the file.

Step1调用短文件。!

[[SimpleAudioEngine sharedEngine] playEffect:@"blast.mp3"];

step2调用后台循环。

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background.mp3" loop:YES];

注意:有可用功能列表http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_simple_audio_engine.html,它非常简单!

整个实施最多需要20分钟!

答案 4 :(得分:1)

在app委托文件中初始化播放器并播放背景音乐。只在app委托中设置一个变量来检查音乐是真还是假......并且在不重新启动音乐的情况下享受你的应用......

在你的代码中你初始化你的播放器在视图中做了加载和初始化方法...这就是你重启音乐的原因.....只需在app delegate applicationdidfinshLaunching()方法中调用它一次....