Android Question Background Service - Vitamio

supriono

Member
Licensed User
Longtime User
Hei guys,
I made program to listen music from internet radio.
My problem is when activity pause, service didn't play music because vitamio videoview pause

Here my main Code
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
    Public play As Boolean
    Public ph As Phone
End Sub

Sub Globals
    Private pnlUtama As Panel
    Private btnPlay As Button
    Private btnFB As Button
    Private btnTwitter As Button
    Private btnWG As Button
    Private pnlNav As Panel
 
    Dim VitamioMediaController1 As Vitamio_MediaController
    Dim VitamioVideoView1 As Vitamio_VideoView
 
    Dim sm As SlideMenu
    Private btnMenu As Button
 
    Private mAlphaAnimation As Animation
    Private mRotateAnimation As Animation
    Private ivLogo As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout ("radio")
 
    sm.Initialize(Activity, Me, "SlideMenu", 0, 222dip)
    sm.AddItem("Wiki Gunungkidul", "Informasi", LoadBitmap(File.DirAssets, "icon.png"), 1)
    sm.AddItem("Hubungi Kami", "Hubungi Manajemen", LoadBitmap(File.DirAssets, "icon.png"), 2)
    sm.AddItem("Investasi", "Mari Berbisnis", LoadBitmap(File.DirAssets, "icon.png"), 3)
    sm.AddItem("SmileCodes", "Programmer", LoadBitmap(File.DirAssets, "logo.png"), 4)

    mAlphaAnimation.InitializeAlpha("", 0.1, 1)
    mAlphaAnimation.Duration = 5000
    mAlphaAnimation.Start(ivLogo)
    mAlphaAnimation.RepeatCount = -1
    mAlphaAnimation.RepeatMode = mAlphaAnimation.REPEAT_REVERSE
 
    mRotateAnimation.InitializeRotateCenter("", 0, 90, btnMenu)
    mRotateAnimation.Duration = 200
 
    StartService (WikiRadio)
End Sub

Sub playRadio 
    If VitamioVideoView1.CheckVitamioLibs Then 
        VitamioVideoView1.Initialize("VitamioVideoView1")
        Activity.AddView(VitamioVideoView1, 100%x, 100%y, 100%x, 100%y)
     
        VitamioMediaController1.Initialize("VitamioMediaController1")
        VitamioMediaController1.Hide
     
        VitamioVideoView1.SetMediaController(VitamioMediaController1)
        VitamioVideoView1.SetVideoQuality(VitamioVideoView1.VIDEO_QUALITY_HIGH)
     
        VitamioVideoView1.SetVideoPath("http://103.16.199.126:6767/wiki")
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        StopService(WikiRadio)
        Activity.Finish
        ExitApplication
        Return True
    Else If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
        Dim currVol As Int = ph.GetVolume(ph.VOLUME_MUSIC)
        ph.SetVolume(ph.VOLUME_MUSIC, currVol + 1, True)
        Return True
    Else If KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
        Dim currVol As Int = ph.GetVolume(ph.VOLUME_MUSIC)
        ph.SetVolume(ph.VOLUME_MUSIC, currVol - 1, True)
        Return True
    End If
End Sub
Sub btnPlay_Click
    If play = True Then
        VitamioVideoView1.StopPlayback
        VitamioVideoView1.Invalidate
        VitamioVideoView1.RemoveView
        play = False
        btnPlay.SetBackgroundImage(LoadBitmap(File.DirAssets, "play.png"))
    Else If play = False Then
        playRadio
        play = True
        btnPlay.SetBackgroundImage(LoadBitmap(File.DirAssets, "stop.png"))
    End If
End Sub
Sub btnWG_Click
    wikigkweb.url = "http://wikigunungkidul.com"
    wikigkweb.title = "Wiki Gunung Kidul"
    StartActivity(wikigkweb)
    Activity.Finish
End Sub
Sub btnTwitter_Click
    wikigkweb.url = "https://mobile.twitter.com/wikigunungkidul"
    wikigkweb.title = "Twitter Wiki Gunungkidul"
    StartActivity(wikigkweb)
    Activity.Finish
End Sub
Sub btnFB_Click
    wikigkweb.url = "https://www.facebook.com/pages/WikiGunungkidul/317163711785052"
    wikigkweb.title = "FB Wiki Gunungkidul"
    StartActivity(wikigkweb)
    Activity.Finish
End Sub

'    events raised by Vitamio_MediaController
Sub VitamioMediaController1_Hidden
    Log("VitamioMediaController1_Hidden")
End Sub

Sub VitamioMediaController1_Shown
    Log("VitamioMediaController1_Shown")
End Sub

'    events raised by Vitamio_VideoView
Sub VitamioVideoView1_BitmapSubtitleUpdated(Bitmap1() As Byte, Width As Int, Height As Int)
 
End Sub

Sub VitamioVideoView1_Buffering(Percent As Int)
    '    Log("VitamioVideoView1_Buffering: "&Percent)    '    commented out to avoid excess logging
End Sub

Sub VitamioVideoView1_Complete
    Log("VitamioVideoView1_Complete")
End Sub

Sub VitamioVideoView1_Error(MEDIA_ERROR As Int) As Boolean
    '    this event Sub must return a Boolean value to indicate whether it has handled the error or not
    '    returning True will prevent the library from taking any action such as displaying an error message
    Log("VitamioVideoView1_Error")
 
    Return True
End Sub

Sub VitamioVideoView1_Info(What As Int, Extra As Int) As Boolean
    '    what does this event mean?
    '    what do the Int parameters represent?
 
    '    Log("VitamioVideoView1_Info")    '    commented out to avoid excess logging
    Return False     '    indicates that this Sub has NOT handled the event and that the library should handle it
End Sub

Sub VitamioVideoView1_Prepared
    Log("VitamioVideoView1_Prepared")
End Sub

Sub VitamioVideoView1_SeekComplete
 
End Sub

Sub VitamioVideoView1_TextSubtitleUpdated(SubText As String)
 
End Sub

'Event sub which is called when an item in the slidemenu is clicked
Sub SlideMenu_Click(Item As Object) 
    Select Case Item
        Case 1    'Wiki Gunungkidul
            Msgbox("Wiki Gunungkidul, Seperti Radio - Mirip Majalah" & CRLF & _
                "Radio streaming yang didirikan sebagai hobi dan kebanggaan warga Gunungkidul", "Informasi")
        Case 2    'Hubungi Kami
            Msgbox("Anda dapat menghubungi kami di : " & CRLF & _
                "Web : http://wikigunungkidul.com" & CRLF & _
                "Twitter : @wikigunungkidul" & CRLF & _
                "Facebook: WikiGunungkidul", "Hubungi Wiki Gunungkidul")
        Case 3    'Investasi
            Msgbox("Anda ingin menanamkan modal atau berbisnis dengan kami?" & CRLF & _
                "Silakan datang langsung ke xxxxx" & CRLF & _
                "No. Telp    : +6281234567890", "Mari Berbisnis")
        Case 4    'SmileCodes
            Msgbox("Software House" & CRLF & _
                "WhatsApp : +6283835701554" & CRLF & _
                "Pin BBM : 7AEA4420", "SmileCodes")
    End Select
End Sub
Sub btnMenu_Click
    sm.Show
    mRotateAnimation.Start(btnMenu)
End Sub

And here my service code
B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
 
End Sub
Sub Service_Create
    Utama.play = False
    CallSub(Utama, "btnPlay_Click") 
 
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.Insistent = False
    n.Sound = False
    n.Light = False
    n.OnGoingEvent = True
    n.SetInfo2("WikiRadio", "Seperti Radio - Mirip Majalah", "wg", Utama)
 
    Service.StartForeground(1, n)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Many thank to your answer
 
Last edited:
Top