1

あるビューでサウンドを再生している場合、別のビューから音量を制御できるかどうか、誰かがその方法を説明できるかどうかを知っていますか? 理解できません。ボリュームに表示するコードがありません。

サウンドは 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

デリゲート パターンを使用する

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 に答える