0

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:' の既知のクラス メソッドがありません」というエラーが表示されます。

4

1 に答える 1

1

これは、呼び出し時にクラスメソッドを呼び出そうとしているためです。

[JARAppDelegate setVolume:sender];

しかし、これは存在しません。インスタンスメソッドを作成しました。

AVAudioPlayer のシングルトンを作成してみると、次のことができます。

[[AVAudioPlayer sharedInstance] setVolume:vol];
于 2012-08-14T23:30:41.030 に答える