我想通过使用代表来阻止我的音乐从另一个视图播放。我已经有了这个设置,我知道有这个选项......
- (void)viewWillDisappear:(BOOL)animated{
[player stop]; ///(my players name here and command to stop the player)
}
但是我不想使用该功能,因为我整个都在播放音乐。我已添加代码以确保我的播放器不重叠。但是,当我想要停止我的音乐时,我想以不同的视角来这样做。我在调用委托时尝试了'[player stop];'
函数,但它不起作用。
有人可以帮忙吗? 谢谢你提前......
修改
这是我的选项代码页面.H文件:
@protocol onHandlerDelegate <NSObject>
- (void)off:(BOOL)success;
- (void)on:(BOOL)success;
-(void)volumeValueChange:(UISlider *)sender;
@end
#import <UIKit/UIKit.h>
@interface OptionsPage : UIViewController{
IBOutlet UISlider *VolumeSlider;
NSTimer *VolumeTimer;
IBOutlet UISwitch *onoroffmusic;
}
-(IBAction) UIBarButtonItem:(id)sender;
-(IBAction) SwitchMusic:(id)sender;
-(IBAction)changevolume:(id)sender;
@property (nonatomic, retain) UISwitch *onoroffmusic;
@property (nonatomic, strong) id<onHandlerDelegate> delegatePpty1;
@end
这是我的选项页面.M文件:
#import "OptionsPage.h"
#import "ViewController.h"
@interface OptionsPage ()
@end
@implementation OptionsPage
@synthesize onoroffmusic;
@synthesize delegatePpty1;
- (IBAction)changevolume:(id)sender{
int volume = VolumeSlider.value;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:volume] forKey:@"volume"];
[defaults synchronize];
[[self delegatePpty1]volumeValueChange:(VolumeSlider)];
}
-(IBAction) SwitchMusic:(id)sender{
if (onoroffmusic.on) {
[[self delegatePpty1] on:YES];
printf("done");
onoroffmusic.on = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"off"];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:@"on"];
[defaults synchronize];
}
else {
/// do nothing
[[self delegatePpty1] off:YES];
printf("off");
onoroffmusic.on = NO;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"on"];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:@"off"];
[defaults synchronize];
ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
[home.player stop];
}
}
-(IBAction) UIBarButtonItem:(id)sender{
ViewController *Backbutton = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:Backbutton animated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
VolumeSlider.value = 0.3;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object = [prefs valueForKey:@"volume"];
if(object == nil){
VolumeSlider.value = 0.3;
}
else{
VolumeSlider.value = [[[NSUserDefaults standardUserDefaults] objectForKey:@"volume"] intValue];
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
BOOL restoredBoolValue2 = [[defaults2 objectForKey:@"off"] boolValue];
BOOL restoredBoolValue1 = [[defaults2 objectForKey:@"on"] boolValue];
if (restoredBoolValue2) {
////donothing
onoroffmusic.on = FALSE;
}
if (restoredBoolValue1) {
////donothing
onoroffmusic.on = TRUE;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在这两个文件中,我声明了一个委托和我的开关。现在只是在我的视图控制器中接收该委托:
MY VIEWCONNTROLLER .H文件:
#import "OptionsPage.h"
@interface ViewController : UIViewController <onHandlerDelegate>{
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
@end
这是我的.M文件:
首先我
@synthesize player;
然后使用以下方法检查我的代表是否已被调用:
-(void)volumeValueChange:(UISlider *)sender
{
if (sender.value > 0.3) {
player.volume = sender.value;
}
else{
player.volume = 0.3;
}
}
-(void)on:(BOOL)success{
NSLog(@"Delegate Method Called");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:@"mykey1"];
[self play];
[player play];
}
-(void)off:(BOOL)success{
NSLog(@"Delegate Method Called");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"mykey1"];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:@"mykey"];
[player stop];
player = nil;
[defaults removeObjectForKey:@"music"];
}
然后我使用以下内容加载bool
nsuserdefults
的值:
- (void)viewDidLoad
{
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"mykey"] == nil ) {
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) {
[self play];
}
}
}
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
BOOL restoredBoolValue2 = [[defaults2 objectForKey:@"mykey"] boolValue];
if (restoredBoolValue2) {
////donothing
[player stop];
}
BOOL restoredBoolValue1 = [[defaults2 objectForKey:@"mykey1"] boolValue];
if (restoredBoolValue1){
[self play];
}
我有我的游戏void
功能
-(void)play{
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Music.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.numberOfLoops = -1;
[player play];
player.volume = 0.3;
player.currentTime = 0.0;
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"];
}
else {
[player stop];
}
}
最后在我的app delegate.m文件中:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"];
}
答案 0 :(得分:1)
让玩家成为属性并将其停止:
[anotherView.player stop];
答案 1 :(得分:-1)
首先,您需要将onHandlerDelegate
从strong
更改为weak
,因为它是代表。
其次,您需要在第二个视图控制器上使用OptionsPage
的实例,并且您需要实现该委托,并在此之后执行optionPage.delegate = self
。