iOS の Objective-C で、AppDelage で再生中の曲の音量を変更するクラス ビュー コントローラに Slider を設定するにはどうすればよいですか? これが.h AppDelegateの私のコードです
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface JARAppDelegate : UIResponder <UIApplicationDelegate>
{
AVAudioPlayer *musicPlayer;
}
@property (strong, nonatomic) UIWindow *window;
- (void)playMusic;
- (void)setVolume:(float)vol;
@end
そして.mで:
- (void)playMusic
{
NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"The History of the World" ofType:@"mp3"];
NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicPath];
musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[musicPlayer setNumberOfLoops:-1]; // Negative number means loop forever
[musicPlayer setVolume:1.0];
[musicPlayer prepareToPlay];
[musicPlayer play];
NSLog(@"play");
}
- (void)setVolume:(float)vol
{
[musicPlayer setVolume:vol];
}
そして、「didFinishLaunchingWithOptions」を呼び出すと [self playMusic];
、これが機能し、フルボリュームが必要な曲を再生します! 次に、SettingsViewControler という別のクラスで: .h
#import <UIKit/UIKit.h>
#import "JARAppDelegate.h"
@interface JARSettingsViewController : UIViewController
{
}
@property (strong, nonatomic) IBOutlet UISlider *volumeSliderOutlet;
- (IBAction)volumeSliderActoin:(id)sender;
@end
彼ら:
- (IBAction)volumeSliderActoin:(id)sender
{
NSLog(@"Volume Changed");
[JARAppDelegate setVolume:sender];
}
ボリュームの変更は、スライダーを上下に動かすたびに記録されるため、setVolume に 0.0 から 1.0 の間の値を送信する必要があります。しかし、「セレクタ 'setVolume:' の既知のクラス メソッドがありません」というエラーが表示されます。