Android Question media player strange behavior on Xiaomi phone

giggetto71

Active Member
Licensed User
Longtime User
Hi,
I have an app that let the user to choose a ringtone, save the URI using KVS and then play it back. Actually the app does much more but I have narrowed the issue to this playback strange behavior which I confirmed by creating a super simple app that basically allows the user to select a tone, save as KVS and play it. Upon open, it reads back from KVS and on this Xiaomi it fails to play (images 4 and 5) while if the user plays the tone after selecting it (1,2 and 3) it works. the URI are just the same (the selected and the saved) so I really don't get why it crashes using the saved one. Again, on all other phones I tried, it works just fine.
Any idea?

I am attaching the code and a some images showing the issue.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    'for browsing files
    Private cc As ContentChooser
    Private OldIntent As Intent
    Type AlertType(Name As String,Uri As String, UriPath As String)       
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.
    
    Private Button1 As Button
    
    Private lbUri As Label
    
     Dim rm As RingtoneManager
     Dim GlobalUri As String
    Private lblPath1 As Label
    Private lblSavedUri As Label
    Private lblSavedPAth As Label
    Private Button2 As Button
    
    Public mp As MediaPlayer
    Private lblUri As Label
    Private Button3 As Button
    Public Alert As AlertType
    Public Alerts As Map
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("L1")
    Activity.Title = "Uri test"
    Alerts.Initialize
    
    If FirstTime Then
        Starter.kvs.Initialize(File.DirInternal, "Uri Test")               
    End If
    
    
    If Starter.KVS.ContainsKey("Alerts") Then ' I know I could have simply saved 2 strings but in the real APP I store a Map so I wanted to do the same here..
        Dim TempElem As AlertType
        Alerts = Starter.KVS.Get("Alerts")
        TempElem = Alerts.Get("ciccio")
        GlobalUri = TempElem.Uri
        lblSavedUri.Text = GlobalUri
        lblSavedPAth.Text = TempElem.UriPath
    End If
    
End Sub

Sub Activity_Resume
    If IsRelevantIntent(Activity.GetStartingIntent) Then
        Dim in As JavaObject = Activity.GetStartingIntent
        Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
        Try

            
            If Starter.KVS.ContainsKey("Alerts") Then
                Dim TempElem As AlertType
                Alerts = Starter.KVS.Get("Alerts")
                TempElem = Alerts.Get("ciccio")
                GlobalUri = TempElem.Uri
                lblSavedUri.Text = GlobalUri
                lblSavedPAth.Text = TempElem.UriPath
            End If
                            
        Catch
            Log(LastException)
        End Try
    End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    
   BrowseRingToner
End Sub

Sub BrowseRingToner
    Dim ff As String
    'ToastMessageShow(rm.GetDefault(rm.TYPE_ALARM),True)
    'Dim mp As MediaPlayer
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then Return

    rm.ShowRingtonePicker("rm", Bit.Or(rm.TYPE_RINGTONE, Bit.Or(rm.TYPE_ALARM,rm.TYPE_NOTIFICATION)), True, ff)
    
End Sub


Sub rm_PickerResult (Success As Boolean, Uri As String)
    
    GlobalUri = Uri
        
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then Return
    If Alerts.Size = 0 Then
       Dim NewElem As AlertType
       NewElem.Initialize
       NewElem.Name = "ciccio"
       NewElem.Uri = GlobalUri
       NewElem.UriPath = GetPathFromContentResult(GlobalUri)
       Alerts.Put(NewElem.Name,NewElem)   
    Else
        Dim TempElem As AlertType       
        TempElem = Alerts.Get("ciccio")
        TempElem.Uri = GlobalUri
        TempElem.UriPath = GetPathFromContentResult(GlobalUri)
    End If
    
    
    lblUri.text = GlobalUri
    lblPath1.Text = GetPathFromContentResult(GlobalUri)
    
    ToastMessageShow("Uri:" & GlobalUri & " Path:" & GetPathFromContentResult(GlobalUri),True)
    Log(GetPathFromContentResult(GlobalUri))
    'save
    
    Starter.KVS.Put("Alerts",Alerts)
    
    
    'Starter.KVS.Put("ChosenUri",GlobalUri)
    'Starter.KVS.Put("ChosenUriPath",GetPathFromContentResult(GlobalUri))
    lblSavedUri.Text = GlobalUri
    lblSavedPAth.Text = GetPathFromContentResult(GlobalUri)
End Sub




Private Sub IsRelevantIntent(in As Intent) As Boolean
    If in.IsInitialized And in <> OldIntent And in.Action = in.ACTION_SEND Then
        OldIntent = in
        Return True
    End If
    Return False
End Sub



Sub GetPathFromContentResult(UriString As String) As String
    Try
        If UriString = "" Then Return ""
        If UriString.StartsWith("/") Then Return UriString 'If the user used a file manager to choose the image
        Dim Cursor1 As Cursor
        Dim Uri1 As Uri
        Dim Proj() As String = Array As String("_data")
        Dim cr As ContentResolver
        cr.Initialize("")
        If UriString.StartsWith("content://com.android.providers.media.documents") Then
            Dim i As Int = UriString.IndexOf("%3A")
            Dim id As String = UriString.SubString(i + 3)
            Uri1.Parse("content://media/external/images/media")
            Cursor1 = cr.Query(Uri1, Proj, "_id = ?", Array As String(id), "")
        Else
            Uri1.Parse(UriString)
            Cursor1 = cr.Query(Uri1, Proj, "", Null, "")
        End If
        Cursor1.Position = 0
        Dim res As String
        res = Cursor1.GetString("_data")
        Cursor1.Close
        Dim SlashIndex As Int, StringLen As Int, UriPath As String, ss As StringFunctions
        ss.Initialize
        UriPath = res
        SlashIndex = UriPath.LastIndexOf("/")
        StringLen = ss.Len(UriPath) - SlashIndex-1
        If SlashIndex <> 0 Then
            res =  ss.Right(UriPath,StringLen)       
        End If   
        Return res
    Catch
        res= ""
        Return res
    End Try
End Sub


Private Sub Button2_Click
   'try to play
   If GlobalUri <> "" Then
       Dim rm As RingtoneManager
       Try       
          mp.Initialize2("mp")
          ToastMessageShow ("GetContentDir:" & rm.GetContentDir ,True)
          mp.Load(rm.GetContentDir, GlobalUri)
          mp.Play
       Catch
              ToastMessageShow(LastException,True)
       End Try                   
   End If
End Sub




Private Sub Button3_Click
    mp.Stop
End Sub
 

Attachments

  • XiaomiUri1.PNG
    XiaomiUri1.PNG
    103.8 KB · Views: 153
  • XiaomiUri2.PNG
    XiaomiUri2.PNG
    129.6 KB · Views: 166
  • XiaomiUri3.PNG
    XiaomiUri3.PNG
    112.5 KB · Views: 151
  • XiaomiUri4.PNG
    XiaomiUri4.PNG
    82.9 KB · Views: 158
  • XiaomiUri5.PNG
    XiaomiUri5.PNG
    168 KB · Views: 166

giggetto71

Active Member
Licensed User
Longtime User
First step is to post the error message from the logs and post it as text.
I wish I could. I don't have that phone. that is from a user who is running this debug little app to help me identifying the issue. Maybe I can try to generate a text file from the app the ask him to send it over email?

Don't assume that the URI will be accessible forever
ok. that is a good point, but the 2 tests are done like 5 seconds one from the other (meaning test#1 play the sound after getting the Uri from the ringtonepicker and test #2 play the sound from the KVS saved uri got 5 seconds before from the ringtonepicker)
Also if you look at the picture if I try to select again from the picker and put it on a label it looks identical to the KVS saved one (at least visually).
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
Ok. Thanks. At the end I used a workaround by copying locally to a safe dir. Thanks for your help as usual.
 
Upvote 0
Top