iPhone OS 中的音量控制是通过另一个视图控制器实现的吗?
如果我在一个视图中播放声音,有谁知道是否可以从另一个视图控制音量,如果可以,有人可以解释一下如何吗?我无法弄清楚,我没有可显示该卷的代码。
从一个视图调用声音,而音量滑块在另一个视图上。我已经编码了两者。
声音的代码是
#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];
}
If I am playing a sound in one view, does anyone know if it is possible to control the volume from another, if it is can someone explain how? I can't figure it out, I have no code to show for the volume.
The sound is called from one view and the volume slider is on another. I have coded both.
The code for the sound is
#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
the code for the slider is
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用委托模式
在第二个视图控制器中创建一个名为 MyProtocol 的协议,例如使用一种方法:
同时创建一个委托实例变量来保存委托引用
并且不要忘记在实现中综合它。
当音量值更新时,您会将值发送给委托
返回第一个控制器,采用 MyProtocol 协议,实现
didUpdateVolume
,并在播放器中设置该值。Use the delegate pattern
In the second view controller create a protocol called MyProtocol for example with one method:
Also create a delegate instance variable to hold the delegate reference
And don't forget to synthesize it in the implementation.
When the volume value is updated you would send the value to the delegate
Back in the first controller adopt the MyProtocol protocol, implement
didUpdateVolume
, and set the value in your player.尝试使用通知中心。
http:// blog.grio.com/2009/04/broadcasting-information-how-to-use-the-iphone-notification-center.html
Try using the Notification center.
http://blog.grio.com/2009/04/broadcasting-information-how-to-use-the-iphone-notification-center.html