1

如果我在一个视图中播放声音,有谁知道是否可以从另一个视图控制音量,如果可以,有人可以解释一下吗?我想不通,我没有代码可以显示音量。

从一个视图调用声音,而音量滑块在另一个视图上。我已经对两者进行了编码。

声音的代码是

 #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];


 }
4

2 回答 2

1

使用委托模式

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

- (void)didUpdateVolume:(NSUInteger)volume;

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

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

并且不要忘记在实现中对其进行综合。

更新音量值时,您会将值发送给委托人

[self.delegate didUpdateVolume:newValue];

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

于 2013-03-11T14:19:32.377 回答
0

尝试使用通知中心。

http://blog.grio.com/2009/04/broadcasting-information-how-to-use-the-iphone-notification-center.html

于 2010-01-25T06:22:33.430 回答