简体   繁体   English

检查AVPlayer是否已经在播放

[英]Check if AVPlayer is playing already

I have a tableview of songs on the ios device and if I select a row, I push to a new view where AVPlayer starts playing the selected song. 我在ios设备上具有歌曲的列表视图,并且如果选择一行,则将推到新视图,AVPlayer将在其中开始播放所选的歌曲。 Now if I go back and select another row, pushing to the view the app will start playing the new song, while continue to play the one that was running already. 现在,如果我返回并选择另一行,则推至视图,该应用将开始播放新歌曲,同时继续播放已经运行的歌曲。

EDIT: I tried using the rate value like this without success, as it will always output "not playing" if i put it in my viewdidload method, even if a song is playing: 编辑:我尝试使用这样的速率值没有成功,因为如果我将它放在viewdidload方法中,即使正在播放一首歌曲,它也会始终输出“ not play”:

- (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");
    }
}

In this line 在这条线

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

you seem to be alloc'ing and init'ing a new AVPlayer. 您似乎正在分配和初始化一个新的AVPlayer。 As such it's not surprising that you get a "not playing" result. 因此,获得“不玩”的结果就不足为奇了。 Simply leave out that line. 只需忽略该行。

AVPlayer has a rate property which indicates the speed of playback as a float. AVPlayer具有一个rate属性,该属性指示浮动播放的速度。 0.0f indicates stopped. 0.0f表示已停止。 The play and stop methods just change the rate property. 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM