【问题标题】:Play/Pause with the same button [AVAudioPlayer]使用相同的按钮播放/暂停 [AVAudioPlayer]
【发布时间】:2011-07-09 15:16:25
【问题描述】:

如何通过按一次UIbutton 来播放带有IBAction 的声音,然后使用AVAudioPlayer 再次按按钮来暂停它?我还想改变那个UIButton在声音播放和不播放时的状态。

这是我的代码:

- (IBAction)Beat
{
    if ([Media2 isPlaying])
    {
        [Media2 pause];
        [Button17 setSelected:NO];
    }

    else
    {
        Path = [[NSBundle mainBundle] pathForResource:@"Beat" ofType:@"mp3"];
        AVAudioPlayer *Media2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
        [Media2 setDelegate:self];
        [Media2 play];
        [Button17 setSelected:YES];
    }
}

【问题讨论】:

    标签: iphone objective-c xcode avaudioplayer


    【解决方案1】:

    这是使用BOOL变量的简单方法。

    viewDidLoad 中设置playing = NO

    -(void)PlayStop{    
        if (playing==NO) {
            // Init audio with playback capability
            [play setBackgroundImage:[UIImage imageNamed:@"hmpause.png"] forState:UIControlStateNormal];
    
            AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:______ error:&err];
            [audioPlayer prepareToPlay];
    
            audioPlayer.delegate=self;
            [audioPlayer play];
    
            playing=YES;
        }
        else if(playing==YES){
            [play setBackgroundImage:[UIImage imageNamed:@"Audioplay.png"] forState:UIControlStateNormal];
    
            [audioPlayer pause];
    
            playing=NO;
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用以下方法让您的 audioPlayer 实例准备好播放。

      /*
       Prepares the audio file to play.
       */
      -(void) initWithAudioPath:(NSString *) audioPath {
          // Converts the sound's file path to an NSURL object
          NSURL *audioPathURL = [[NSURL alloc] initFileURLWithPath:audioPath];
      
          self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioPathURL error:nil];
          audioPlayer.delegate = self;
      
          [audioPlayer prepareToPlay];
      
          [audioPathURL release];
      }
      
      -(void) pausePlaybackForPlayer:(AVAudioPlayer *) player {
          [player pause];
      }
      
      -(void) startPlaybackForPlayer:(AVAudioPlayer *) player {
          if (![player play]) {
              NSLog(@"Could not play %@\n", player.url);
          }
      }
      
      - (IBAction)Beat {
          if (audioPlayer.playing == NO) {
              // Audio player is not playing.
              // Set the button title here to "stop"...
              [self startPlaybackForPlayer:audioPlayer];
          }else {
              // Audio player is playing.
              // Set the button title here to "play"...
              [self pausePlaybackForPlayer:audioPlayer];
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-25
        • 1970-01-01
        • 2012-01-18
        • 2014-12-05
        • 1970-01-01
        相关资源
        最近更新 更多