Android Question exoplayer play video.mp4 file from usb device

fifiddu70

Well-Known Member
Licensed User
Longtime User
Hi everyone, I'm creating an app for playing video.mp4 content with exoplayer and so far I've succeeded but only by loading the video files into the dirasset and therefore inside the Android device, as per the title I need to play some video files .mp4 not from the internal memory but from a USB stick connected to the Android device, can anyone help me, I post the code that I have created yet to be completed, I would like to point out that I have set the paths in dirrootexternal but as per the title I want to recall them from the USB.

B4X:
#Region Project Attributes
#ApplicationLabel: InfoPoint-V1.0
#VersionCode: 1
#VersionName: 1.0
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: landscape
#CanInstallToExternalStorage: False
#BridgeLogger:True
#End Region

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

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Private player1 As SimpleExoPlayer
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
Private SimpleExoPlayerView1 As SimpleExoPlayerView
Private imgplay1 As ImageView
Private imgplay2 As ImageView
Private imgplay3 As ImageView
Private imgplay4 As ImageView
Private imgplay5 As ImageView
Private imgplay6 As ImageView
Private pnlbase As Panel
Private imgplay7 As ImageView
Private imgplay8 As ImageView
Private imgplay9 As ImageView
Private imgqrocode As ImageView
Private imgplay10 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
player1.Initialize("player")
      
      
End If
Activity.LoadLayout("InfoPoint")
imgplay1.SetLayout(1%x,1%y,15%x,22%y)
imgplay2.SetLayout(18%x,1%y,15%x,22%y)
imgplay3.SetLayout(1%x,28%y,15%x,22%y)
imgplay4.SetLayout(18%x,28%y,15%x,22%y)
imgplay5.SetLayout(1%x,56%y,15%x,22%y)
imgplay6.SetLayout(18%x,56%y,15%x,22%y)
imgplay7.SetLayout(1%x,85%y,15%x,22%y)
imgplay8.SetLayout(18%x,85%y,15%x,22%y)
'imgplay9.SetLayout(1%x,68%y,18%x,19%y)
'imgplay10.SetLayout(20%x,68%y,10%x,19%y)
SimpleExoPlayerView1.SetLayout(35%x,1%y,64%x,60%y)
imgqrocode.SetLayout(78%x,65%y,20%x,30%y)
  
If File.Exists(File.DirRootExternal, "1.jpg") = True Then
imgplay1.Bitmap= LoadBitmap(File.DirAssets,"1.jpg")
      
Else
imgplay1.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
If File.Exists(File.DirRootExternal, "2.jpg") = True Then
imgplay2.Bitmap= LoadBitmap(File.DirRootExternal,"2.jpg")
Else
imgplay2.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
  
End If
  
If File.Exists(File.DirRootExternal, "3.jpg") = True Then
imgplay3.Bitmap= LoadBitmap(File.DirRootExternal,"3.jpg")
Else
imgplay3.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
      
End If
  
If File.Exists(File.DirRootExternal, "4.jpg") = True Then
imgplay4.Bitmap= LoadBitmap(File.DirRootExternal,"4.jpg")
Else
imgplay4.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
If File.Exists(File.DirRootExternal, "5.jpg")= True Then
imgplay5.Bitmap= LoadBitmap(File.DirRootExternal,"5.jpg")
Else
imgplay5.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
If File.Exists(File.DirRootExternal, "6.jpg")= True Then
imgplay6.Bitmap= LoadBitmap(File.DirRootExternal,"6.jpg")
Else
imgplay6.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
If File.Exists(File.DirRootExternal, "7.jpg")= True Then
imgplay7.Bitmap= LoadBitmap(File.DirRootExternal,"7.jpg")
Else
imgplay7.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
If File.Exists(File.DirRootExternal, "8.jpg")= True Then
imgplay8.Bitmap= LoadBitmap(File.DirRootExternal,"8.jpg")
Else
imgplay8.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
If File.Exists(File.DirRootExternal, "9.jpg")= True Then
imgplay9.Bitmap= LoadBitmap(File.DirRootExternal,"9.jpg")
Else
imgplay9.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
If File.Exists(File.DirRootExternal, "10.jpg")= True Then
imgplay10.Bitmap= LoadBitmap(File.DirRootExternal,"10.jpg")
Else
imgplay10.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
End If
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
  
End Sub

Private Sub imgplay1_Click
player1.Prepare(player1.CreateFileSource(File.DirAssets, "1.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay2_Click
player1.Prepare(player1.CreateFileSource(File.DirAssets, "2.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay3_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "3.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay4_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "4.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay5_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "5.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay6_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "6.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay7_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "7.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay8_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "8.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay9_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "9.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay10_Click
player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "10.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i try in rootexetrnal but not function
That´s the baddest errordescription i´ve ever seen

If it does not work then there MUST be an Error. Post the FULL Stacktrace.
Best is to upload a small project showing the problem.

NO ONE can help without seeing what you are doing and how.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Use the ManageExternalStorage class and get permission to use the directory on the SDCard.

In the Main Activity Create, I use this to get the permissions and location of the SDCard directory:

B4X:
' These are declared in Process Globals

'    Dim MES As ManageExternalStorage
'    Dim Storage As ExternalStorage  
'    Dim RootDir As String                  


    If FirstTime Then
        MES.Initialize(Me, "MES")
        Storage.Initialize(Me, "storage")
    End If

    ' Get access to the SDCard
   
    If Not(MES.HasPermission) Then
        MsgboxAsync("This app requires access to all files, please enable the option", "Manage All Files")
        Wait For Msgbox_Result(Res As Int)

        MES.GetPermission
        Wait For MES_StorageAvailable
    End If

    ' Get access to the SDCard directory
   
    Storage.SelectDir(True)
    Wait For Storage_ExternalFolderAvailable

    RootDir = Get_URI_Dir(File.GetText(File.DirInternal,"PersistantUri"))
    Log("Using " & RootDir)

This sub reads the file created by ManageExternalStorage to parse out the directory so it is in the correct format

B4X:
'Get the directory path from the storage URI
Sub Get_URI_Dir(URI As String) As String
   
    ' content://com.android.externalstorage.documents/tree/13FA-1001%3AMovies
    ' to
    ' /storage/13FA-1001/Movies
   
    Dim RtnVal As String=URI
   
    Do While RtnVal.Contains("/")
        RtnVal=RtnVal.SubString(RtnVal.IndexOf("/")+1)
    Loop
   
    RtnVal=RtnVal.Replace("%3A", "/")
   
    Return "/storage/" & RtnVal
   
End Sub


RootDir ends up looking something like "/storage/13FA-1001/Movies"

Then load the file with

B4X:
player.Prepare(player.CreateFileSource(RootDir, "MovieFile.mp4"))

Add the following to the Manifest
B4X:
AddPermission(android.permission.MANAGE_EXTERNAL_STORAGE)
SetApplicationAttribute(android:requestLegacyExternalStorage, true)

This code works with android:targetSdkVersion="33" but is probably going to get blocked if you try to upload it to the Play Store.
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
Use the ManageExternalStorage class and get permission to use the directory on the SDCard.

In the Main Activity Create, I use this to get the permissions and location of the SDCard directory:

B4X:
' These are declared in Process Globals

'    Dim MES As ManageExternalStorage
'    Dim Storage As ExternalStorage 
'    Dim RootDir As String                 


    If FirstTime Then
        MES.Initialize(Me, "MES")
        Storage.Initialize(Me, "storage")
    End If

    ' Get access to the SDCard
  
    If Not(MES.HasPermission) Then
        MsgboxAsync("This app requires access to all files, please enable the option", "Manage All Files")
        Wait For Msgbox_Result(Res As Int)

        MES.GetPermission
        Wait For MES_StorageAvailable
    End If

    ' Get access to the SDCard directory
  
    Storage.SelectDir(True)
    Wait For Storage_ExternalFolderAvailable

    RootDir = Get_URI_Dir(File.GetText(File.DirInternal,"PersistantUri"))
    Log("Using " & RootDir)

This sub reads the file created by ManageExternalStorage to parse out the directory so it is in the correct format

B4X:
'Get the directory path from the storage URI
Sub Get_URI_Dir(URI As String) As String
  
    ' content://com.android.externalstorage.documents/tree/13FA-1001%3AMovies
    ' to
    ' /storage/13FA-1001/Movies
  
    Dim RtnVal As String=URI
  
    Do While RtnVal.Contains("/")
        RtnVal=RtnVal.SubString(RtnVal.IndexOf("/")+1)
    Loop
  
    RtnVal=RtnVal.Replace("%3A", "/")
  
    Return "/storage/" & RtnVal
  
End Sub


RootDir ends up looking something like "/storage/13FA-1001/Movies"

Then load the file with

B4X:
player.Prepare(player.CreateFileSource(RootDir, "MovieFile.mp4"))

Add the following to the Manifest
B4X:
AddPermission(android.permission.MANAGE_EXTERNAL_STORAGE)
SetApplicationAttribute(android:requestLegacyExternalStorage, true)

This code works with android:targetSdkVersion="33" but is probably going to get blocked if you try to upload it to the Play Store.
i have error when launch app, I followed the entire procedure you described and it doesn't work for me, what am I doing wrong?
 

Attachments

  • error.jpg
    error.jpg
    199.3 KB · Views: 73
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
B4X:
#Region  Project Attributes
    #ApplicationLabel: InfoPoint-V1.0
    #VersionCode: 1
    #VersionName: 1.0
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
    #BridgeLogger:True
    #AdditionalJar: com.android.support:support-core-utils
#End Region

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Private xui As XUI
    Private player1 As SimpleExoPlayer
    'Private RP As RuntimePermissions

        Dim MES As ManageExternalStorage
        Dim Storage As ExternalStorage
        Dim RootDir As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private SimpleExoPlayerView1 As SimpleExoPlayerView
    Private imgplay1 As ImageView
    Private imgplay2 As ImageView
    Private imgplay3 As ImageView
    Private imgplay4 As ImageView
    Private imgplay5 As ImageView
    Private imgplay6 As ImageView
    Private pnlbase As Panel
    Private imgplay7 As ImageView
    Private imgplay8 As ImageView
    Private imgplay9 As ImageView
    Private imgqrocode As ImageView
    Private imgplay10 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        player1.Initialize("player")
        MES.Initialize(Me, "MES")
        Storage.Initialize(Me, "storage")
    End If
    Activity.LoadLayout("InfoPoint")
    pnlbase.SetLayout(0%X,0%Y,100%X,100%Y)
    imgplay1.SetLayout(1%x,1%y,15%x,22%y)
    imgplay2.SetLayout(18%x,1%y,15%x,22%y)
    imgplay3.SetLayout(1%x,25%y,15%x,22%y)
    imgplay4.SetLayout(18%x,25%y,15%x,22%y)
    imgplay5.SetLayout(1%x,49%y,15%x,22%y)
    imgplay6.SetLayout(18%x,49%y,15%x,22%y)
    imgplay7.SetLayout(1%x,73%y,15%x,22%y)
    imgplay8.SetLayout(18%x,73%y,15%x,22%y)
    'imgplay9.SetLayout(1%x,68%y,18%x,19%y)
    'imgplay10.SetLayout(20%x,68%y,10%x,19%y)
    SimpleExoPlayerView1.SetLayout(35%x,1%y,64%x,60%y)
    imgqrocode.SetLayout(78%x,65%y,20%x,30%y)
   
    If Not(MES.HasPermission) Then
        MsgboxAsync("This app requires access to all files, please enable the option", "Manage All Files")
        Wait For Msgbox_Result(Res As Int)

        MES.GetPermission
        Wait For MES_StorageAvailable
    End If

    ' Get access to the SDCard directory
 
    Storage.SelectDir(True)
    Wait For Storage_ExternalFolderAvailable

    RootDir = Get_URI_Dir(File.GetText(File.DirInternal,"PersistantUri"))
    Log("Using " & RootDir)


   
   
   
    If File.Exists(File.DirRootExternal, "1.jpg") = True Then
        imgplay1.Bitmap= LoadBitmap(File.DirRootExternal,"1.jpg")
       
        Else
            imgplay1.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")      
    End If
   
    If File.Exists(File.DirRootExternal, "2.jpg") = True Then
        imgplay2.Bitmap= LoadBitmap(File.DirRootExternal,"2.jpg")
    Else
        imgplay2.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
   
    End If
   
If File.Exists(File.DirRootExternal, "3.jpg") = True Then
        imgplay3.Bitmap= LoadBitmap(File.DirRootExternal,"3.jpg")
    Else
        imgplay3.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
       
    End If
   
    If File.Exists(File.DirRootExternal, "4.jpg") = True Then
        imgplay4.Bitmap= LoadBitmap(File.DirRootExternal,"4.jpg")
    Else
        imgplay4.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
    End If
   
    If File.Exists(File.DirRootExternal, "5.jpg")= True Then
        imgplay5.Bitmap= LoadBitmap(File.DirRootExternal,"5.jpg")
    Else
        imgplay5.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
    End If
   
    If File.Exists(File.DirRootExternal, "6.jpg")= True Then
        imgplay6.Bitmap= LoadBitmap(File.DirRootExternal,"6.jpg")
    Else
        imgplay6.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
    End If
   
    If File.Exists(File.DirRootExternal, "7.jpg")= True Then
        imgplay7.Bitmap= LoadBitmap(File.DirRootExternal,"7.jpg")
    Else
        imgplay7.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
    End If
   
    If File.Exists(File.DirRootExternal, "8.jpg")= True Then
        imgplay8.Bitmap= LoadBitmap(File.DirRootExternal,"8.jpg")
    Else
        imgplay8.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
    End If
   
    If File.Exists(File.DirRootExternal, "9.jpg")= True Then
        imgplay9.Bitmap= LoadBitmap(File.DirRootExternal,"9.jpg")
    Else
        imgplay9.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
    End If
   
    If File.Exists(File.DirRootExternal, "10.jpg")= True Then
        imgplay10.Bitmap= LoadBitmap(File.DirRootExternal,"10.jpg")
    Else
        imgplay10.Bitmap= LoadBitmap(File.DirAssets,"banner1.jpg")
    End If
   
End Sub

Sub Get_URI_Dir(URI As String) As String
 
    ' content://com.android.externalstorage.documents/tree/13FA-1001%3AMovies
    ' to
    ' /storage/13FA-1001/Movies
 
    Dim RtnVal As String=URI
 
    Do While RtnVal.Contains("/")
        RtnVal=RtnVal.SubString(RtnVal.IndexOf("/")+1)
    Loop
 
    RtnVal=RtnVal.Replace("%3A", "/")
 
    Return "/storage/" & RtnVal
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub

Private Sub imgplay1_Click
    'RP.CheckAndRequest(RP.PERMISSION_WRITE_EXTERNAL_STORAGE)
    'RP.CheckAndRequest(RP.PERMISSION_READ_EXTERNAL_STORAGE)
    'Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    'If Result Then
   
        'File.Copy(File.DirRootExternal, "1.mp4", File.DirInternal, "1.mp4")
        'MsgboxAsync("Importazione dei file effettuata correttamente, riavviare l'applicazione.","FILE IMPORTATI")
        'player1.Prepare(player1.CreateFileSource(File.DirInternal, "1.mp4"))
        'SimpleExoPlayerView1.Player = player1
    'Else
        'Log("no permission")
    'End If
   
   
End Sub

Private Sub imgplay2_Click
    player1.Prepare(player1.CreateFileSource(RootDir, "2.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay3_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "3.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay4_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "4.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay5_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "5.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay6_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "6.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay7_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "7.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay8_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "8.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay9_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "9.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

Private Sub imgplay10_Click
    player1.Prepare(player1.CreateFileSource(File.DirRootExternal, "10.mp4"))
    SimpleExoPlayerView1.Player = player1
End Sub

I would like it to automatically recognize the pen drive inserted with the video files renamed from 1 to 10 for example: 1.mp4, together with the images always renamed from 1 to 10 such as: 1.jpg and so on
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
i have error when launch app, I followed the entire procedure you described and it doesn't work for me, what am I doing wrong?

In ManageExternalStorage.bas, the package name needs to be set. The version floating around has:

B4X:
' Be sure to reference your app package name in "pakageg:xxx"
    in.Initialize("android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION", "package:b4a.example")

I changed the version I keep to :

B4X:
' Be sure to reference your app package name in "pakageg:xxx"
    in.Initialize("android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION", "package:"  & Application.PackageName)

I also use the ExternalStorage.b4xlib library rather than the class file.
 
Upvote 0
Top