首先你需要创建超类:在你的 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];
}
}
然后在您的文件 .hUIIMage
中为您的按钮添加:
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];
}
希望这对您有所帮助;)如果是,请评价;)