iPhone OS中的音量控制来自另一个视图控制器?

时间:2010-01-24 21:37:38

标签: iphone volume

如果我在一个视图中播放声音,是否有人知道是否可以控制另一个音量,如果有人可以解释如何?我无法理解,我没有代码显示音量。

从一个视图调用声音,在另一个视图中调用音量滑块。我已经编码了两个。

声音的代码是

 #import `<AVFoundation/AVAudioPlayer.h`>  
 #import "LeftViewController.h"


@implementation LeftViewController




- (IBAction)buttonrm:(id)sender
{
 [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)playl {

 [theAudio play];



}

- (IBAction)pausel {

 [theAudio pause];

}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

 NSString *path =[[NSBundle mainBundle] pathForResource:@"The Noisettes - Never Forget You" ofType:@"mp3"];
 theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
 theAudio.delegate = self;
 //[theAudio play];




    [super viewDidLoad];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}



- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

滑块的代码是

 - (void)viewDidLoad {
    [super viewDidLoad];

 CGRect sliderRect = CGRectMake(46,124,169,0);
 UISlider *VolumeL = [[UISlider alloc] initWithFrame:sliderRect];
 VolumeL.minimumValue = 0;
 VolumeL.maximumValue = 100;
 VolumeL.continuous = YES;


 UIImage *sliderctrl = [UIImage imageNamed:@"VolumeL.png"];
 //UIImage *stetchLeftTrack = [[UIImage imageNamed:@"volumel12.png"]
 //stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];


 [VolumeL setThumbImage:sliderctrl forState:UIControlStateNormal];
 //[VolumeL setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];

 [VolumeL addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

 VolumeL.transform = CGAffineTransformRotate(VolumeL.transform, 270.0/180*M_PI);

 [self.view addSubview:VolumeL];

 [VolumeL release];


 }

2 个答案:

答案 0 :(得分:1)

使用委托模式

在第二个视图控制器中,创建一个名为MyProtocol的协议,例如使用一种方法:

- (void)didUpdateVolume:(NSUInteger)volume;

还创建一个委托实例变量来保存委托引用

@property (nonatomic, assign) id<MyProtocol> delegate;

不要忘记在实施中合成它。

更新音量值后,您会将值发送给代理

[self.delegate didUpdateVolume:newValue];

回到第一个控制器,采用MyProtocol协议,实现didUpdateVolume,并在播放器中设置值。

答案 1 :(得分:0)