まず、スーパークラスを作成する必要があります: appdelegate.h に以下を追加します。
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface RADAppDelegate : UIResponder <UIApplicationDelegate> {
AVPlayer *myAudioPlayer;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) AVPlayer *myAudioPlayer;
@end
そして、didFinishLaunchingWithOptions の下のappdelegate.mに以下を追加します。
[myAudioPlayer superclass];
これを簡単に試して、コードに再利用してください。
- (IBAction)playStop:(id)sender {
MyAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.myAudioPlayer.rate == 1.0){
UIImage *img = [UIImage imageNamed:@"Play_BT.png"];
[sender setImage:img forState:UIControlStateNormal];
[appDelegate.myAudioPlayer pause]; //here is possible use stop or pause is depend if you use AVAudioPlayer or AVPlayer
} else if (appDelegate.myAudioPlayer.rate == 0.0){
UIImage *img = [UIImage imageNamed:@"Pause_BT.png"];
[sender setImage:img forState:UIControlStateNormal];
[appDelegate.myAudioPlayer play];
}
}
次に、ファイル .h にUIIMage
ボタンを追加します。
UIButton *control;
@property (nonatomic, strong) IBOutlet UIButton *control;
あなたの中に戻ってきてfile.m
これを追加します:
- (void)viewWillAppear:(BOOL)animated {
MyAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.myAudioPlayer.rate == 1.0){
UIImage *img = [UIImage imageNamed:@"Pause_BT.png"];
[control setImage:img forState:UIControlStateNormal];
} else if (appDelegate.myAudioPlayer.rate == 0.0) {
UIImage *img = [UIImage imageNamed:@"Play_BT.png"];
[control setImage:img forState:UIControlStateNormal];
}
これがお役に立てば幸いです ;) はいの場合は評価してください ;)