我在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");
    }
}
有帮助吗?

解决方案

在这条线中

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

您似乎是分配的,并启动了一个新的Avplayer。因此,您获得“不玩”的结果也就不足为奇了。只需忽略那条线即可。

其他提示

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];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top