Android Question SRT Subtitle Timer1 Show Hide Subtitle

ronovar

Active Member
Licensed User
Longtime User
Hi,

i im currnly working on Subtitle that's display subtitle over movie (i im using VideView for playing video from http link) and need Subtitle over movie...so i have write the code that puts Label on top of ViewView element and reads SRT file from temp folder of android, then parsing times to miliseconds and then compare player position with miliseconds from SRT file, and Tick timer1 to count to next subtitle. My problem si that subtitle is not hiding when there is not subitle.

Here i attached the example in zip so you need to put your link for avi and srt and try how to syncronize subtitle with movie to show and hide text on Label when it needs to. I have playing two days with timer and can't figure ut how to syncronize subtitle with movie. If some one can modify code and upload it again i will be very thank full.
 

Attachments

  • Subtitle.zip
    7.1 KB · Views: 293

ronovar

Active Member
Licensed User
Longtime User
Ok...you just need to download Subtitle.zip....copy on your SD card movie that you have at home (for example: Pirates Of Carribean.avi) and put Pirates Of Carribean.srt and rename in my code above movie and srt name that is like your movie name and run program....
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Can Anyone write a b4a program that reads SRT subtitle file from SD card and then display it on display using Label1.Text = "LINEFROM SDCARD". I tried using timer.interval but is not precise timing.

Thanks.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
If you look into the srt-file, there should be time-indications when to show the subtitle. I guess you need to implement your own code to do that. Maybe this post can help: http://stackoverflow.com/questions/7714121/android-native-video-player-with-subtitles

If you are targeting Jelly Bean or KitKat, I think vidoeview (or perhaps it is the MediaPlayer?) now supports srt-files but I don't think the videoview wrapper exposes those methods. Alternatively, I believe you can use the vitamio-wrapper available here on the forum. If I recall correctly, I tested subtitles with Vitamio and as long as they were local (not streaming), they worked.
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
I tried vitamio-wrapper but the picture is not good like on videoview...so i try to implement my own srt reading file using timer...but unsuccessfull. Question to Erik...can You please modify wrapper for VideoView or MediaPlayer that reads SRT subtitle for input?

Thanks.
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Here is Excellent Code For Parsing SRT File (It does read SRT File from Files Folder - which is included in this example MOVIESUBTITLE.srt). So You just need to unzip this file, open the b4a project file compile and see in logcat subtitle number, time and text.I added conversion of hh:mm:ss,miliseconds to miliseconds.

So now i im intersted in Timer0. Interval that show and hide from log subtitle text when time is begin and out. So If some one can modify code..so that in logcat subtitle text will show and hide what is timing in SRT file.
 

Attachments

  • SRTSubTitle2.zip
    43.2 KB · Views: 289
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Ok, i manage to syncronize Subtitles with video ( i will publish here code when i finish completlly project).

So i have now few questions:

1. How to add to Label1.Text to display special characters?
- For example i have in SRT Characters č,ć,đ,ž and when is displayed using Label1.Text i get ? ? ? for characters...so i read that i need to set text encodings but i don't know how to set encoding for label.

2. How to set Label1.Text on the bottom center of the screen when using -2 for label width and height(i need wiidth and height auto because some srt subtitle have one row , and some have two rows)

I im using these code to init Label

B4X:
'INIT Subtitle Label
    Label1.Initialize("Label1")
    Label1.TextSize = 36
    Label1.TextColor = Colors.White
    Label1.Color = Colors.ARGB(125, 192, 192, 192)
    Label1.Gravity = Gravity.CENTER_HORIZONTAL
    Label1.RequestFocus
    Activity.AddView(Label1, 0, 80%y, -2, -2)
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
How to set Character encoding to iso-8859-2?

I downloaded srt file sucessfully and save it to device, but when reading saved SRT file from device it is missing special character because it is not downloaded using character encoding.

Here is the code that i use and is working but how to add character encodings?

B4X:
'Download SRT Subtitle
Dim Job1 As HttpJob
Job1.Initialize("Job1", Me)

'SEND a GET request
Job1.Download("http://www.mywebsite.com/subtitles/movie.srt")

Sub JobDone(Job As HttpJob)
If Job.Success = True Then
        Select Job.JobName          
                 Case "Job1"
                Dim Out As OutputStream
                Out = File.OpenOutput(File.DirInternalCache, "movie.srt", False)
                File.Copy2(Job.GetInputStream, Out)
                Out.Close
        End Select
End If
End Sub
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Thanks, Erel for replaying...i don't know where i need to put Job.GetString2("iso-8859-2").

I try this but there is ? in special characters linke ć, č, š, đ, ž....

B4X:
Sub JobDone(Job As HttpJob)
      If Job.Success = True Then
              Select Job.JobName 
                   Case"Job1"
                        Dim Out AsOutputStream
                        Job.GetString2("iso-8859-2")
                        Out = File.OpenOutput(File.DirInternalCache, "movie.srt", False)File.Copy2(Job.GetInputStream, Out)
                         Out.Close
               End Select
      End If
End Sub

So reading text is done using array and that is working (when i manually enter Label0.Text = "ččćšđž" it display correctly). So the problem is when the SRT file is downloaded from server to device it needs to be saved to device using iso-8859-2 ccharacter set. So if you can give me example code how to use job.getstring2 that saves file from http link file to device.

Thanks.
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
B4X:
Sub ReadSRT
        'TIMER Code Here
        Timer0.Interval = SRTStop0 - VideoView0.Position
       
        'READ Number, Time, Text From SRT File
        SRTNum  = List0.Get(Pos)        ' Number
        SRTime  = List0.Get(Pos+1)        ' Time
        SRText  = List0.Get(Pos+2)        ' Text1
   
        'INCRESE Second Position In SRT File
        Pos = Pos + 2
       
        'SET Second Line Empty
        SRText2 = ""
   
        'CHECK If Second SRT Line Is Empty
        If (StringFunctions0.Len(List0.Get(Pos+1)) > 0) Then
            'Subtitle Text First And Second Line
            Pos = Pos + 1
            SRText2 = List0.Get(Pos)    ' Text2
            'First Subtitle Line
            SRText1 = SRText
            SRText  = SRText &CRLF& SRText2
        End If
   
        'INCREASE Second Position In SRT File
        Pos = Pos + 2
   
        'GET Time From SRT Time Row
        SRTime0   = StringFunctions0.Split(SRTime, " --> ")
        SRTStart0 = (SRTime0.Get(0))
        SRTStop0  = (SRTime0.Get(1))
   
        'REPLACE , With .
        SRTStart0 = SRTStart0.Replace(",", ".")
        SRTStop0  = SRTStop0.Replace(",", ".")
   
        'CALCULATE Miliseconds From SRT Time
        SRTStart0 = 3600000*SRTStart0.substring2(0,2)+60000*SRTStart0.substring2(3,5)+SRTStart0.substring(6)*1000
        SRTStop0  = 3600000*SRTStop0.substring2(0,2)+60000*SRTStop0.substring2(3,5)+SRTStop0.substring(6)*1000
End Sub

As you can see i im reading file from device(when is downloaded) using List0 element (file is using List0 read into array and then using pos counter read from array - 1st line, 2st line, 3st line.... to the end of file).

So Can You please give an example how to download file and read it to display iso-8859-2 charcters set using your code (TextReader or job.getstring2).

I finished apk but i im missing iso-8859-2 because on special characters i im getting ?

Thanks.
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
B4X:
Sub GetSRT(SubTitle As String)
    'READ SRT File Into Array
    List0 = File.ReadList(File.DirInternalCache, SubTitle)
End Sub

This Sub reads file previously downoaded to dirinternalcache and read it into list(arrays) - ReadList

So how to read using iso-8859-2 using textreader into list array, so that i change code into this line to textreader

B4X:
List0 = File.ReadList(File.DirInternalCache, SubTitle)
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
ok..i fixed special characters i need only this little modifications:

B4X:
Sub GetSRT(SubTitle As String)
    'READ SRT File Into Array
    TextReader0.Initialize2(File.OpenInput(File.DirInternalCache, SubTitle), "iso-8859-2")
    List0 = TextReader0.ReadList
End Sub
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
i im using VideoView to play streamed http file...so if is possible to get beforeplaying video % of loaded buffer with videoview? If not please suggest library that can show % of loaded video before playing.
 
Upvote 0
Top