Search code examples
iphoneios7titaniumappceleratortitanium-alloy

Using Ti.Media.VideoPlayer video does not loop on iPhone 4


Having a weird problem with my app.

Trying to play a video in a loop (see the code below). VideoPlayer repeatMode is set to Ti.Media.VIDEO_REPEAT_MODE_ONE.

Works in simulator, works on iPhone 4s, works on iPad Air... does not work on iPhone 4 (all of them running iOS 7.1.1 apart from simulator - 7.1).

Exact symptoms: after one play, video should restart from beginning, but it stops. If controls are available, I can press play to start playing it again, then after reaching the end it stops again.

I could swear that it worked on iPhone 4 before the 7.1 upgrade, but somehow it stopped.

Anyone could replicate this issue? Or have any idea?

Sys info:

  • Titanium SDK: Tested with 3.2.3.GA; 3.2.2.GA; 3.2.1.GA; 3.2.0.GA
  • Platform & version: iOS 7.1.1 (11D201)
  • Device: works on iPhone 4s, iPad Air, simulator; fails on iPhone 4
  • Host Operating System: OSX 10.9.2
  • Titanium Studio: 3.2.3.201404181442
  • Xcode: 5.1.1 (5B1008)
  • Alloy: 1.3.1

Code for basic Alloy project to replicate:

index.js

var videoPlayer = Titanium.Media.createVideoPlayer({
    autoplay : true,
    fullscreen : false,
    mediaControlStyle : Titanium.Media.VIDEO_CONTROL_DEFAULT,
    repeatMode: Ti.Media.VIDEO_REPEAT_MODE_ONE,
    scalingMode : Titanium.Media.VIDEO_SCALING_ASPECT_FIT
});

//Video is 12s long so it is easy to notice that it does not loop.
videoPlayer.url = "video/candles.mp4"; 
$.index.add(videoPlayer);
$.index.open();

index.xml

<Alloy>
    <Window class="container">
    </Window>
</Alloy>

I would appreciate any suggestions.


Solution

  • I have done some research and played around with the Titanium.Media.VideoPlayer and managed to work around the issue. Below you can find the solution in case anyone else encounter this problem.

    First of all, some info on the problem.

    I have found some forum boards where developers were having very similar problem with MPMoviePlayerController on iPad 1st gen. Looped video was less or more randomly freezing on loop. So I think there is a problem with the Apple video player on some devices, and not necessary with Titanium implementation.

    My first approach was to try replay video on complete event that is fired when video stops playing, instead of using repeatMode.

    So far so good. It worked on the first time, yay! But the happiness was rather short. During an hour of test of videoPlayer.play() on event complete, the video sometimes was repeating 10-30 times fine and then suddenly was freezing. What gives? Turns out the video player had some sort of a hiccup.

    Normal sequence of events was as follows:

    • video ends, playbackstate changes to 2,
    • complete event is fired
    • I restart video on complete
    • playbackstate changes to 1, video is playing until the end.

    For a mysterious reason once in a while this sequence got additional something happening:

    • video reaches the end - playbackstate changes to 2,
    • complete event is fired.
    • video gets restarted on complete
    • playbackstate changes to 1, video starts playing
    • suddenly playbackstate changes to 1, video stops, without firing complete event.

    Solution

    So, knowing all this, I have set up listeners to listen to playbackstate instead of complete and when I hear playbackstate: 1 then I restart video and tadaaa! Everything works!.

    Caveat: not 'everything' unfortunately. I am using custom video controls, so I have lost pause, and stop ability (every time video playback state changes to 'stopped' I restart it forcefully). To make it work I have to remember to remove listener every time I press 'pause/stop' buttons and then reattach it afterwards.

    Hope that helps someone.