1

好的,这是我的问题。我有一个应用程序在启动时播放音乐,我想在设置页面中实现一个简单的 uibutton 来停止播放音频。

标题:

int musicof;
IBOutlet UIButton *music;
@property (nonatomic, strong) AVAudioPlayer *theAudio;

方法:

- (IBAction)music:(id)sender{
if (musicof == 1){
    musicof = 2;
[music setBackgroundImage:[UIImage imageNamed:@"moff.png"] forState:UIControlStateNormal];
[music setImage:[UIImage imageNamed:@"moff.png"] forState:UIControlStateNormal];
    [self.theAudio stop];
}
else{
    musicof = 1;
    [music setBackgroundImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];
    [music setImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];
    [self.theAudio play];
}

}

如果你还没有猜到整数“musicof”是用来记录按钮状态的。

然而,这根本不起作用,我按下按钮只会改变图标。有什么建议么?

4

4 回答 4

0

首先你需要创建超类:在你的 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];
     }

希望这对您有所帮助;)如果是,请评价;)

于 2013-05-08T17:46:40.937 回答
0

你不是 [player prepareToPlay]你的AVAudioPlayer对象,所以播放音频文件需要时间所以首先[player prepareToPlay]你的AVAudioPlayer对象

于 2013-05-08T12:05:46.133 回答
0

musicof 需要是一个实例变量。您需要像这样访问它:

self.musicof

或者

_musicof
于 2013-05-08T11:15:54.977 回答
0

像这样应用它。

在 .h 文件中导入此框架

#import <AVFoundation/AVFoundation.h>

在你的 .m 文件中

@synthesize theAudio;

在 viewDidLoad 中实现这段代码

NSURL *audioURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Tum Hi Ho" ofType:@"mp3"]];

 NSData *audioData  = [NSData dataWithContentsOfURL:audioURL];

 theAudio  = [[AVAudioPlayer alloc] initWithData:audioData error:nil];

 [theAudio setNumberOfLoops:0];

 [theAudio prepareToPlay];

之后实现你的代码

- (IBAction)music:(id)sender{

  if (musicof == 1){
      musicof = 2;
      [music setBackgroundImage:[UIImage imageNamed:@"moff.png"] forState:UIControlStateNormal];
      [music setImage:[UIImage imageNamed:@"moff.png"] forState:UIControlStateNormal];

  if (self.theAudio.playing) {
     [self.theAudio pause];
   }

   [self.theAudio stop];

   self.theAudio.currentTime = 0;
  }
  else{
    musicof = 1;
    [music setBackgroundImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];
    [music setImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];
    [self.theAudio play];
  }
}

我希望它对你有帮助。

于 2013-05-08T11:16:02.390 回答