我正在尝试在我的应用程序中播放音乐。 音乐工作正常,但在切换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];

    } 

}

===============>>#1 票数: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];
    }
}

如果已经分配或者使用新的声音文件URL再次分配它,则播放新的声音文件释放和nil相同的播放器。

===============>>#2 票数: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发布你的播放器!

===============>>#3 票数:1

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

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

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

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

===============>>#4 票数: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分钟!

===============>>#5 票数:1

在app delegate文件中初始化播放器并播放背景音乐。 只在app delegate中设置一个变量来检查音乐是真还是假......并且无需重新启动音乐就可以享受你的应用......

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

  ask by Mc.Lover translate from so

未解决问题?本站智能推荐: