help sound file in background when receive new message via http

fifiddu70

Well-Known Member
Licensed User
Longtime User
Hello everyone, I would add in my program, the ability to receive messages even when the app is in the background, is expected to play an audio file for each message received, to receive messages using my personal web site with FTP sending a text file. txt and receiving message using http with a timer set every 4 seconds that updates the label lblMessage in case of new messages, how do I get sound from the audio file whenever a new message when the program is in the background?
someone can create an example?
thanks
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim hc As HttpClient
   Dim req As HttpRequest
   Dim timer1 As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim txtmessagerx As EditText
   Dim lblmessage As Label
   Dim mp As MediaPlayer
   Dim btnsend As Button
   Dim txttext As EditText
   Dim ftp1 As FTP
   Dim txtwriter As TextWriter
   Dim p As Phone
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("assistente")
   timer1.Initialize("timer1",4000)
   timer1.Enabled=True
   txtmessagerx.Visible=False
   req.InitializeGet("http://www.example.com/message/bottle.txt") '<--- Replace this with your info
   hc.Execute(req, 1)
End Sub
Sub timer1_Tick
   hc.Execute(req, 1)
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub hc_ResponseSuccess(Response As HttpResponse, TaskId As Int)
   txtmessagerx.text = Response.GetString("UTF8")
   lblmessage.text=txtmessagerx.text
End Sub
Sub txtmessagerx_TextChanged (Old As String, New As String)
   If txtmessagerx.text = Old Then
   Else
   sound
   End If
End Sub
Sub sound
   mp.Initialize
   mp.Load(File.DirAssets,"ding.wav")
   mp.Play
End Sub
Sub btnsend_Click
   txtwriter.Initialize(File.OpenOutput(File.DirInternal, "bottle.txt" , False))
   txtwriter.Write(txttext.text)
   ftp1.Initialize("ftp1","ftp.example.com",21,"example","example")
   txtwriter.Close
   ftp1.SendCommand("MKD","/message") 
   ftp1.UploadFile(File.DirInternal,"bottle.txt",True,"/message/bottle.txt")
     ftp1.Close
   txttext.text=""
   ToastMessageShow("Messagge send",True)
   p.HideKeyboard(Activity)
End Sub
 
Last edited:

Air

Member
Licensed User
Longtime User
Try using Soundpool (needs Audio.library).

Save a Soundfile inside your File.DirAssets. In this Samplecode
the Soundfile has the name "snd.ogg" inside File.DirAssets

B4X:
Sub Process_Globals
   'Variable for Soundplay
   Dim sp As SoundPool
   Dim snd_file As Int
end Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      ' Load temporary Soundfile
      sp.Initialize(1)
      snd_file = sp.Load(File.DirAssets,"snd.ogg")
   End If
end Sub

Sub txtmessagerx_TextChanged (Old As String, New As String)
    If txtmessagerx.text = Old Then
    Else
   If sp.IsInitialized = False Then sp.Initialize(1)
   sp.play(snd_file, 1, 1, 1, 0, 1)
    End If
End Sub
 
Last edited:
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
erel I have this problem, in service mode, the audio file plays every 4 seconds, I want it to sound any new text message changed in activity are managed with a EditText and a label but I can not in service.

B4X:
Sub Service_Create
   SI.Initialize2("SI",999)
   n.Initialize
   
   hc.Initialize("hc")
   req.InitializeGet("http://www.siciliabit.com/message/bottle.txt") 
   
   timer1.Initialize("timer1",4000)
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   timer1.Enabled=True
   hc.Execute(req, 1)
   n.Icon = "icon"
   'n.Sound=True
   n.Vibrate = true
   n.SetInfo("Message wait", "Waiting for server...", Main)
    n.Sound = False
   n.OnGoingEvent = True
   Service.StartForeground(1, n)
   StartServiceAt("", DateTime.Now + 0.1 * DateTime.TicksPerMinute, False)
   
End Sub
Sub timer1_Tick
   hc.Execute(req, 1)
End Sub
Sub Service_Destroy

End Sub
Sub hc_ResponseSuccess(response As HttpResponse, TaskId As Int)
   
   mp.Initialize
   mp.Load(File.DirAssets,"ding.wav")
   mp.Play
   
End Sub
Sub stop 
   service.StopForeground(1)
   n.Cancel(1)
End Sub


in activity the sub is:
B4X:
Sub hc_ResponseSuccess(Response As HttpResponse, TaskId As Int)
   txtmessagerx.Text = Response.GetString("UTF8")
   lblmessage.Text=txtmessagerx.Text
      
End Sub
Sub txtmessagerx_TextChanged (Old As String, New As String)
   If txtmessagerx.Text = Old Then
   
   Else
   mp.Initialize
   mp.Load(File.DirAssets,"ding.wav")
   mp.Play
   End If
End Sub

in service the sub is:
[CODE]
Sub hc_ResponseSuccess(response As HttpResponse, TaskId As Int)
   
   mp.Initialize
   mp.Load(File.DirAssets,"ding.wav")
   mp.Play
   
End Sub

not work when new message, work every 4 second as timer.
 
Upvote 0
Top