use service to play mp3 not work

sally3599

Member
Licensed User
Longtime User
mp.Load in Service_Create will crash the app

Service module named mp3service.bas:
B4X:
Sub Process_Globals
    Dim mp As MediaPlayerStream
End Sub
Sub Service_Create
    mp.Load("http://website/my.mp3")
End Sub

Sub Service_Start (StartingIntent As Intent)
    'mp.Play
End Sub

Sub Service_Destroy
    'mp.Stop
End Sub

Sub mp_Complete
    'ToastMessageShow("mp Complete..." , True)
End Sub

Sub mp_StreamReady
    'mp.Play
End Sub

Sub mp_StreamError(ErrorCode As String, ExtraData As Int)
    'ToastMessageShow("Error: " & ErrorCode & ", " & ExtraData, True)
    'mp.Stop
End Sub

Main Activity named mp3service.b4a:
B4X:
Sub Process_Globals

End Sub

Sub Globals
      Dim StartButton As Button
   Dim StopButton As Button
            
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    StartButton.Enabled = True
    StopButton.Enabled = False
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause(UserClosed As Boolean)
    StopService(mp3service)
End Sub

Sub StartButton_Click
  StartButton.Enabled = False
  StopButton.Enabled = True
  StartService(mp3service)
End Sub

Sub StopButton_Click
  StartButton.Enabled = True
  StopButton.Enabled = False
  StopService(mp3service)
End Sub

This app will crash when i click the Start Button....
I found the problem is mp.Load("http://website/my.mp3") in Sub Service_Create.

Any help will be appreciate it.
 
Last edited:

sally3599

Member
Licensed User
Longtime User
here is the log

B4X:
08-08 16:34:29.654 I/ActivityManager(  124): Displayed activity com.xtralogic.android.logcollector/.SendLogActivity: 716 ms (total 716 ms)
08-08 16:34:30.364 I/ActivityManager(  124): Start proc mp3.player for service mp3.player/.mp3service: pid=1970 uid=10070 gids={3003}
08-08 16:34:30.964 I/B4A     ( 1970): ** Service (mp3service) Create **
08-08 16:34:30.974 E/B4A     ( 1970): mp3service_service_create (java line: 112)
08-08 16:34:30.974 E/B4A     ( 1970): 
08-08 16:34:30.974 E/B4A     ( 1970): java.lang.NullPointerException
08-08 16:34:30.974 E/B4A     ( 1970): at anywheresoftware.b4a.audio.MediaPlayerStreamWrapper.Load(MediaPlayerStreamWrapper.java:101)
08-08 16:34:30.974 E/B4A     ( 1970): at mp3.player.mp3service._service_create(mp3service.java:112)
08-08 16:34:30.974 E/B4A     ( 1970): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 16:34:30.974 E/B4A     ( 1970): at java.lang.reflect.Method.invoke(Method.java:521)
08-08 16:34:30.974 E/B4A     ( 1970): at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
08-08 16:34:30.974 E/B4A     ( 1970): at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
08-08 16:34:30.974 E/B4A     ( 1970): at mp3.player.mp3service.onCreate(mp3service.java:38)
08-08 16:34:30.974 E/B4A     ( 1970): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
08-08 16:34:30.974 E/B4A     ( 1970): at android.app.ActivityThread.access$3300(ActivityThread.java:125)
08-08 16:34:30.974 E/B4A     ( 1970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
08-08 16:34:30.974 E/B4A     ( 1970): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 16:34:30.974 E/B4A     ( 1970): at android.os.Looper.loop(Looper.java:123)
08-08 16:34:30.974 E/B4A     ( 1970): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-08 16:34:30.974 E/B4A     ( 1970): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 16:34:30.974 E/B4A     ( 1970): at java.lang.reflect.Method.invoke(Method.java:521)
08-08 16:34:30.974 E/B4A     ( 1970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-08 16:34:30.974 E/B4A     ( 1970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-08 16:34:30.974 E/B4A     ( 1970): at dalvik.system.NativeStart.main(Native Method)
08-08 16:34:30.984 W/dalvikvm( 1970): threadid=1: thread exiting with uncaught exception (group=0x400287f0)
08-08 16:34:31.004 W/B4A     ( 1970): java.lang.RuntimeException: Unable to create service mp3.player.mp3service: java.lang.RuntimeException: java.lang.NullPointerException
 
Upvote 0
Top