Android Question my mp3player stops when the mobile device go to sleep. why? please help

Kadandale Ganapathy Bhat

Member
Licensed User
my mp3player stops when the mobile device go to sleep. why? please help
my code is
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: श्रीसुमध्वविजयः
#VersionCode: 1
#VersionName:
#SupportedOrientations: portrait

#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: false

#End Region

Sub Process_Globals
Dim MP As MediaPlayer
Dim timer1 As Timer
Dim timer2 As Timer
End Sub

Sub Globals
Dim playstatus As String
Dim FilePntr As Int
Dim barPosition As SeekBar
Dim barVolume As SeekBar
Dim lblPosition As Label
Dim Looping As ToggleButton
Private Label1 As Label
Private lbnext As Label
Private lbpa As Label
Private lbpr As Label
Private lbst As Label
Private lbstop As Label
Dim filelist As List
Dim maintext As List
Dim startpos As Int
Dim endpos As Int
Dim path1 As String
Dim sloka As String
Dim sloka1 As String
Private clv1 As CustomListView
Dim totalduration As Int
Dim totalpos As Int
Dim totalpos1 As Int
Dim currenttext As String
Dim pretext As String
Dim trfilelist As List
Dim path2 As String
Private paneltool As Panel
Private btminus As Button
Private btplus As Button
Private Label2 As Label
Private lbbm As Label
Private lbpagccount As Label
Private Panel1 As Panel
Private pno As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("1")
timer1.Initialize("timer1", 1000)
timer2.Initialize("timer2", 1000)
timer2.Enabled = False
MP.Initialize2("MP")
trfilelist.Initialize
path1 = File.DirAssets
trfilelist = File.ListFiles(path1)
File.MakeDir(File.DirInternal,"audio/mv")
Dim filename As String
path2 = File.DirInternal
For u = 0 To trfilelist.Size-1
filename = trfilelist.Get(u)
If filename.SubString2(filename.Length-3,filename.Length) = "mp3" Then
File.Copy(File.DirAssets,trfilelist.Get(u),File.DirInternal & "/audio/mv",trfilelist.Get(u))
End If
Next
path1 = File.DirInternal & "/audio/mv"
filelist.Initialize
filelist = File.ListFiles(path1)
filelist.Sort(True)
For i=0 To filelist.Size-1
MP.Load(path1,filelist.Get(i))
totalduration = totalduration + MP.Duration
Next

For i = 0 To filelist.Size-1

MP.Load(path1, filelist.Get(0))
clv1.ScrollToItem(i)


Next

maintext.Initialize
maintext=File.Readlist(File.DirAssets,"mv.txt")

loadmv

End Sub

Sub loadmv
clv1.DefaultTextSize = 22
For i=0 To maintext.Size-1
sloka1 = maintext.Get(i)
If sloka1.Contains("॥") Then
sloka = sloka & CRLF & sloka1
clv1.AddTextItem(sloka,sloka)
sloka = ""
Else
sloka = sloka & CRLF & sloka1
End If

Next

End Sub

Sub Activity_Resume
If MP.IsPlaying Then MP.Play

timer1.Enabled = True

timer1_Tick 'don't wait one second for the UI to update.

End Sub

Sub Activity_Pause (UserClosed As Boolean)
MP.Play

End Sub

Sub timer1_Tick
If MP.IsPlaying Then
totalpos1 = totalpos + MP.Position

barPosition.Value = totalpos1 / totalduration * 100

lblPosition.Text = "Position: " & ConvertToTimeFormat(totalpos1) & _
" (" & ConvertToTimeFormat(totalduration) & ")"

End If
End Sub


Sub ConvertToTimeFormat(ms As Int) As String
Dim seconds, minutes As Int
seconds = Round(ms / 1000)
minutes = Floor(seconds / 60)
seconds = seconds Mod 60
Return NumberFormat(minutes, 1, 0) & ":" & NumberFormat(seconds, 2, 0) 'ex: 3:05
End Sub

Sub barVolume_ValueChanged (Value As Int, UserChanged As Boolean)
MP.SetVolume(barVolume.Value / 100, barVolume.Value / 100)
End Sub

Sub barPosition_ValueChanged (Value As Int, UserChanged As Boolean)
If UserChanged = False Then Return 'the value was changed programmatically

MP.Position = Value / 100 * totalduration
If MP.IsPlaying = False Then 'this can happen when the playback reached the end and the user changes the position
MP.Play
End If
timer1_Tick 'immediately update the progress label

End Sub

Sub Looping_CheckedChange(Checked As Boolean)
MP.Looping = Checked
End Sub

Sub lbstop_Click
MP.Stop
pretext = clv1.GetValue(FilePntr)
clv1.DefaultTextColor=Colors.Blue
clv1.DefaultTextSize= 22
clv1.RemoveAt(FilePntr)
clv1.InsertAtTextItem(FilePntr,pretext,pretext)
clv1.ScrollToItem(0)
End Sub

Sub lbst_Click
If playstatus = "pa" Then
MP.Play
Else
FilePntr = 0
For i = 0 To filelist.Size-1
MP.Load(path1, filelist.Get(0))
MP.Play
clv1.ScrollToItem(0)
Next
currenttext = clv1.GetValue(FilePntr)
clv1.DefaultTextColor=Colors.Red
clv1.DefaultTextSize= 25
clv1.RemoveAt(FilePntr)
clv1.InsertAtTextItem(FilePntr,currenttext,currenttext)
End If
playstatus = ""
End Sub

Sub lbpr_Click
If FilePntr>0 Then
FilePntr = FilePntr-1
'For i = 0 To filelist.Size-1
MP.Load(path1, filelist.Get(FilePntr))
MP.Play
clv1.ScrollToItem(FilePntr)
'Next

pretext = clv1.GetValue(FilePntr+1)
clv1.DefaultTextColor=Colors.Blue
clv1.DefaultTextSize= 22
clv1.RemoveAt(FilePntr+1)
clv1.InsertAtTextItem(FilePntr+1,pretext,pretext)

currenttext = clv1.GetValue(FilePntr)
clv1.DefaultTextColor=Colors.Red
clv1.DefaultTextSize= 25
clv1.RemoveAt(FilePntr)
clv1.InsertAtTextItem(FilePntr,currenttext,currenttext)
Else
MP.Stop
End If
End Sub

Sub lbpa_Click
playstatus = "pa"
MP.Pause
End Sub

Sub lbnext_Click
If FilePntr < filelist.Size-1 Then
FilePntr = FilePntr+1
'For i = 0 To filelist.Size-1
MP.Load(path1, filelist.Get(FilePntr))
MP.Play
clv1.ScrollToItem(FilePntr)
'Next

pretext = clv1.GetValue(FilePntr-1)
clv1.DefaultTextColor=Colors.Blue
clv1.DefaultTextSize= 22
clv1.RemoveAt(FilePntr-1)
clv1.InsertAtTextItem(FilePntr-1,pretext,pretext)

currenttext = clv1.GetValue(FilePntr)
clv1.DefaultTextColor=Colors.Red
clv1.DefaultTextSize= 25
clv1.RemoveAt(FilePntr)
clv1.InsertAtTextItem(FilePntr,currenttext,currenttext)

Else
MP.Stop
End If

End Sub

Sub clv1_ItemClick (Index As Int, Value As Object)
MP.Stop
If FilePntr<filelist.Size Then
pretext = clv1.GetValue(FilePntr)
clv1.DefaultTextColor=Colors.Blue
clv1.DefaultTextSize= 22
clv1.RemoveAt(FilePntr)
clv1.InsertAtTextItem(FilePntr,pretext,pretext)
End If
FilePntr = Index
MP.Load(path1,filelist.Get(FilePntr))
currenttext = clv1.GetValue(FilePntr)
clv1.DefaultTextColor=Colors.Red
clv1.DefaultTextSize= 25
clv1.RemoveAt(FilePntr)
clv1.InsertAtTextItem(FilePntr,currenttext,currenttext)
MP.Play
End Sub
Sub MP_Complete
totalpos = totalpos + MP.Position


If FilePntr < filelist.Size Then

MP.Load(path1, filelist.Get(FilePntr))
MP.Play

clv1.ScrollToItem(FilePntr)
pretext = clv1.GetValue(FilePntr-1)
clv1.DefaultTextColor=Colors.Blue
clv1.DefaultTextSize= 22
clv1.RemoveAt(FilePntr-1)
clv1.InsertAtTextItem(FilePntr-1,pretext,pretext)

currenttext = clv1.GetValue(FilePntr)
clv1.DefaultTextColor=Colors.Red
clv1.DefaultTextSize= 25
clv1.RemoveAt(FilePntr)
clv1.InsertAtTextItem(FilePntr,currenttext,currenttext)
Else

barPosition.Value = 0
clv1.ScrollToItem(0)
totalpos = 0
totalpos1 = 0
lblPosition.Text = "Position: " & ConvertToTimeFormat(totalpos1) & _
" (" & ConvertToTimeFormat(totalduration) & ")"


currenttext = clv1.GetValue(filelist.Size-1)
clv1.DefaultTextColor=Colors.blue
clv1.DefaultTextSize= 22
clv1.RemoveAt(filelist.Size-1)
clv1.InsertAtTextItem((filelist.Size-1),currenttext,currenttext)
clv1.ScrollToItem(0)
End If
'Next
End Sub
 

Brandsum

Well-Known Member
Licensed User
Its better to create another service which will contain the media player object. Set automatic foreground mode to never and create your own notification and set that as service foreground notification. You can add your mp3 title image etc to that notification to make it more informative.
 
Upvote 0

Kadandale Ganapathy Bhat

Member
Licensed User
Its better to create another service which will contain the media player object. Set automatic foreground mode to never and create your own notification and set that as service foreground notification. You can add your mp3 title image etc to that notification to make it more informative.
It works. Thank you very much. God bless you
 
Upvote 0
Top