Итак, у меня есть фоновая музыка, играющая в одном представлении, затем я нажимаю кнопку и перехожу к другому виду, и фоновая музыка в другом представлении работает также. Тогда у меня есть две фоновой музыки. Я хочу остановить музыку из предыдущего представления.Как остановить фоновую музыку в другом представлении в Xcode?
Так вот .h код первого взгляда:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController<AVAudioPlayerDelegate>{
AVAudioPlayer *startingMusic;
}
-(IBAction)StartGame:(id)sender;
Так вот .m код первого взгляда:
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
-(IBAction)StartGame:(id)sender{
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *music = [[NSBundle mainBundle] pathForResource:@"Morning_Walk" ofType:@"mp3"];
startingMusic=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
startingMusic.delegate=self;
startingMusic.numberOfLoops=-1;
[startingMusic play];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
вот .h код для второй вид:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface StageOne : UIViewController<AVAudioPlayerDelegate>
AVAudioPlayer *gameMusic;
}
вот .m код второй точки зрения:
#import "StageOne.h"
@interface StageOne()
@end
@implementation StageOne
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *music = [[NSBundle mainBundle] pathForResource:@"Green_Hills" ofType:@"mp3"];
gameMusic=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
gameMusic.delegate=self;
gameMusic.numberOfLoops=-1;
[gameMusic play];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Спасибо вам большое!
перед выходом на новый взгляд вы можете остановить музыку [startingMusic остановить]; – Shubhendu