iOS版 - 如何变化的看法时,停止背景音乐 [英] iOS - How to stop background music when changing views

查看:164
本文介绍了iOS版 - 如何变化的看法时,停止背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

改变视图时如何停止的背景音乐?我没有任何线索。如果我preSS一个按钮,带我到一个新的观点,有新的背景音乐。但旧的背景音乐(它出现在一个无限循环)不断持续。请帮忙!也品尝一些code拜托,这里是我的:

How to stop background music when changing views? I have no clue. If i press a button which takes me to a new view, there is new background music. But the old background music (which goes in an infinite loop) keeps on going. Please help! also sample some code please, here is mine:

- (void)viewDidLoad {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MathMusic2" ofType:@"wav"];
    AVAudioPlayer* theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
    theAudio.numberOfLoops = -1;

    [super viewDidLoad];
}

我只需要知道如何使背景音乐来自新视图停止播放。反之当我preSS后退按钮从新视图

I just need to know how to make the background music from the new view stop playing. And vice versa when i press the back button from the new view

推荐答案

创建一个属性 AVAudioPlayer * theAudio 这样你就可以在你的类中的任何一点进入audioPlayer 。

Create a property for the AVAudioPlayer *theAudio so you can access the audioPlayer from any point in your class.

的viewController的头文件

... 
AVAudioPlayer *theAudio;
...
@property (nonatomic, retain) AVAudioPlayer *theAudio;

的viewController的Implentation文件

...
@synthesize theAudio;
...

- (void)viewDidLoad {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MathMusic2" ofType:@"wav"];
    self.theAudio= [[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]] autorelease];
    theAudio.delegate = self;
    [theAudio play];
    theAudio.numberOfLoops = -1;

    [super viewDidLoad];
}

如果viewWillDisappear被称为那么你可以停止与音频

If viewWillDisappear is called you can then just stop the audio with

- (void)viewWillDisappear
{
    [theAudio stop];
}

这篇关于iOS版 - 如何变化的看法时,停止背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆