更新AVQueuePlayer的项目

时间:2018-02-10 13:09:03

标签: ios swift avqueueplayer

我正在编写包装器以使用AVQueuePlayer创建音频播放器。我希望能够更新项目(更新播放列表,添加,从播放列表中删除......)我找到的唯一方法是:

var audioPlayer = AVQueuePlayer()

public var playerItems : [AVPlayerItem] = [] {
    didSet {
        self.audioPlayer.removeAllItems()
        playerItems.forEach({ self.audioPlayer.insert($0, after: nil)})
    }
}

1 个答案:

答案 0 :(得分:0)

您可以创建AVQueuePlayer的新实例:

func remove(at index: Int) {
    guard self.audioPlayer.items().indices ~= index else { return }
    let item = self.audioPlayer.items()[index]
    self.audioPlayer.remove(item)
}

但是如果你想删除/更新/插入项目,你需要为它编写单独的方法。

例如:

namespace BackendBundle\Controller;

use BackendBundle\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;

class PassController extends Controller {
public function passAction(Request $request) {
    $changePasswordModel = new ChangePassword();
    $form = $this->createForm(ChangePasswordType::class, $changePasswordModel);

    $form->handleRequest($request);

    if ($form->isSubmitted()) {

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $user = $this->getUser(); //metemos como id la del usuario sacado de su sesion
            $encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
            $password = $encoder->encodePassword($changePasswordModel->getPassword(), $user->getSalt());
            $user->setPassword($password);
            $em->persist($user);
            $flush = $em->flush();
            if ($flush === null) {
                $this->session->getFlashBag()->add('success', 'El usuario se ha editado correctamente');
                return $this->redirectToRoute("others_show"); //redirigimos la pagina si se incluido correctamete
            } else {
                $this->session->getFlashBag()->add('warning', 'Error al editar el password');
            }
        } else {
           // dump($form->getErrors());
            $this->session->getFlashBag()->add('warning', 'El password no se ha editado por un error en el formulario !');
        }
    }

    return $this->render('BackendBundle:Others:editPass.html.twig', array(
                'form' => $form->createView(),
    ));
}
}