2012-08-14 32 views
0

在Objective-C for iOS中,如何在類視圖控制器中使用Slider來更改在AppDelage中播放的歌曲的音量? 這是我在.H代碼的AppDelegate如何在類視圖控制器中有一個Slider,用於更改Objective-C for iOS中AppDelage中播放歌曲的音量?

#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 

的.M:

- (IBAction)volumeSliderActoin:(id)sender 
{ 
    NSLog(@"Volume Changed"); 
    [JARAppDelegate setVolume:sender]; 
} 

量改變的是記錄每次您移動滑塊上下,所以它應該被髮送setVolume介於0.0和1.0之間的值。但我得到的,上面寫着「爲選擇沒有已知的類方法‘setVolume:’錯誤

回答

1

這是因爲你嘗試調用方法,當你撥打:

[JARAppDelegate setVolume:sender]; 

,但是這並未」牛逼存在您已經創建了一個實例方法

拉昇AVAudioPlayer創建一個單身,那麼你可以做到以下幾點:。

[[AVAudioPlayer sharedInstance] setVolume:vol]; 
+0

什麼是單身人士? – Joseph800 2012-08-15 03:24:27

+0

這完全是一個完全不同的問題。閱讀http://en.wikipedia.org/wiki/Singleton_pattern,然後搜索特定的Objective-C實現。 – Alex 2012-08-15 21:59:03

相關問題