【问题标题】:Check if AVPlayer is playing already检查 AVPlayer 是否已经在播放
【发布时间】:2013-11-06 21:52:33
【问题描述】:

我在 ios 设备上有一个歌曲的表格视图,如果我选择一行,我会推送到一个新视图,在该视图中 AVPlayer 开始播放所选歌曲。现在,如果我返回并选择另一行,将应用推送到视图将开始播放新歌曲,同时继续播放已经运行的歌曲。

编辑: 我尝试使用这样的速率值但没有成功,因为如果我将它放在我的 viewdidload 方法中,它总是会输出“未播放”,即使正在播放歌曲:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    appDelegate = (SimpleTableAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.audioPlayer = [[AVPlayer alloc] init];
    if (appDelegate.audioPlayer.rate == 0.0f)
    {
        NSLog(@"not playing");
    } else {
    NSLog(@"already playing");
    }
}

【问题讨论】:

    标签: ios avplayer audio-player


    【解决方案1】:

    在这一行

    appDelegate.audioPlayer = [[AVPlayer alloc] init];
    

    您似乎正在分配和初始化一个新的 AVPlayer。因此,您得到“不玩”的结果也就不足为奇了。只需省略该行。

    【讨论】:

      【解决方案2】:

      AVPlayer 有一个rate 属性,它将播放速度指示为浮点数。 0.0f 表示已停止。 playstop 方法只是更改 rate 属性。

      - (void)viewDidLoad
      {
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
      
          appDelegate = (SimpleTableAppDelegate *)[[UIApplication sharedApplication] delegate];
          //Stop potential existing audioPlayer prior to creating new player
          [appDelegate.audioPlayer stop];
      
          //Create new audio player
          appDelegate.audioPlayer = [[AVPlayer alloc] init];
      }
      

      【讨论】:

      • 我忘了提到我实际上尝试了 rate 但这并没有触发。我将发布上面我使用但没有成功的代码......
      • 您的音频播放器将在 viewDidLoad 期间始终停止。您应该在创建新的播放器实例之前停止现有的播放器实例。更新答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多