Added Android code
[wl-app.git] / Android / r2-streamer / r2-parser / src / main / java / org / readium / r2_streamer / model / publication / SMIL / MediaOverlayNode.java
1 package org.readium.r2_streamer.model.publication.SMIL;
2
3 import org.w3c.dom.Element;
4
5 import java.io.Serializable;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 /**
10  * Created by gautam chibde on 23/5/17.
11  */
12
13 public class MediaOverlayNode implements Serializable {
14     private static final long serialVersionUID = 7329984331545950872L;
15
16     public String text;
17     public String audio;
18     public List<String> role;
19     public List<MediaOverlayNode> children;
20
21     public MediaOverlayNode() {
22         this.children = new ArrayList<>();
23         this.role = new ArrayList<>();
24     }
25
26     @Override
27     public String toString() {
28         return "MediaOverlayNode{" +
29                 "text='" + text + '\'' +
30                 ", audio='" + audio + '\'' +
31                 ", role=" + role +
32                 ", children=" + children +
33                 '}';
34     }
35
36     /**
37      * Generate Clip from current instance object
38      *
39      * @return The generated Clip.
40      */
41     public Clip clip() throws IndexOutOfBoundsException {
42         Clip newClip = new Clip();
43
44         // Retrieve the audioString (containing timers + audiofile url), then
45         // retrieve both.
46         newClip.relativeUrl = this.audio.split("#")[0];
47         String times = this.audio.split("#")[1];
48         return parseTimer(times, newClip);
49     }
50
51     /**
52      * Parse the time String to fill clip.
53      *
54      * @param times The time string ("t=S.MS,S.MS") as created in {@link SMILParser#parseAudio(Element)}
55      * @param clip  The Clip instance where to fill the parsed data.
56      * @return returns clips with start, end and duration
57      */
58     private Clip parseTimer(String times, Clip clip) throws IndexOutOfBoundsException {
59         // Remove "t=" prefix from times string.
60         times = times.substring(2, times.length());
61         // Parse start and end times.
62         Double start = Double.parseDouble(times.split(",")[0]);
63         Double end = Double.parseDouble(times.split(",")[1]);
64         clip.start = start;
65         clip.end = end;
66         clip.duration = end - start;
67         return clip;
68     }
69 }