Android Question playing an MP4 file that resides on the device

Colin Evans

Active Member
Licensed User
Longtime User
Hi, I'm sure this is a simple question but I can't find an answer

I have a number of mp4 files that reside in the folder I believe to be

/storage/emulated/0/Movies or /storage/emulated/legacy/Movies

I just want an interface that I can allow my grandson to press a button and the related video will play, can this be done without having to load the files into the application
 
Last edited:

Colin Evans

Active Member
Licensed User
Longtime User
Hi Erel, thanks for the reply, I added the entry to the manifest editor but I'm still at a loss I've tried the following code but not sure as it doesn't play the mp4 file
B4X:
Sub button1_click
    Dim DirName As String
    DirName="/storage/emulated/0/Movies/"
    MP4Text="MrTumble.mp4"
    player1.Initialize("player")
    player1.CreateFileSource(DirName,MP4Text)
    SimpleExoPlayerView1.Player = player1
    player1.Play
End Sub
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Erel, I want to control the interface , he's only 18 months and loves to press the screen, I was hoping I could restrict his options to only playing the six MP4 files I have for him, with big nice buttons, so back to my original question can it be done and if so , how, many thanks

I took out the player1.initialize which just causes the program to stop and return back to the android screen

Also when you mention MediaPlayer, I have had a look for any examples but it seems to be linked to audio files not video files (MP4)
 
Last edited:
Upvote 0

colboy

Member
Licensed User
Longtime User
You need to move the Initialize to the Activity_Create, is what I think Erel meant. This means it won't get re-initialized every time the button is pressed.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Firstly I should add that the tablet I am using for my Grandson is running Android 4.4 don't know if that would make a difference, but the MP4 file plays with the default video player so I know thats okay.

I've changed the code to use the default file location, after finding where the app resides I moved the mp4 file to the files folder in the hope it would solve the problem, but it doesn't, the file still doesn't play

I've watched the tutorial and i'm nearly sure you state I don't need and permissions at runtime if I'm using the default file location, anyway here is my simple code, I would greatly appreciate if you could tell me where I'm going wrong, or it isn't possible, many thanks

B4X:
Sub Process_Globals
    Private player1 As SimpleExoPlayer
    Public MP4Text As String
End Sub

Sub Globals
    Private button1 As Button
    Private SimpleExoPlayerView1 As SimpleExoPlayerView
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    'Log(File.DirDefaultExternal)
    'Label1.Text=File.DirDefaultExternal
    player1.Initialize("player")
End Sub

Sub button1_click
    Dim DirName As String
    MP4Text="MrTumble.mp4"
    player1.CreateFileSource(File.DirDefaultExternal,MP4Text)
    SimpleExoPlayerView1.Player = player1
    player1.Play
End Sub

Just to add, I've tried it on an up to date android tablet using android 7 and it still doesn't work, also here is my Manifest

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

hope that helps
 
Last edited:
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Erel, I've created a zip file and uploaded it to Dropbox and shared it with support@basic4ppc.com named B4A, is that all I need to do, haven't used dropbox for years, the program is just a simple one to try and see if playing the mp4 files was possible so its not cosmetically right yet, if it is possible then I'll amend and add the rest of the MP4 files, hope you can read it okay and can find a solution, many thanks
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Think I've shared it, already zipped, Dropbox says you opened it, hopefully this will work, long delays don't help given its a day between messages, I assume your time zone is v ery different to the UK
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
B4X:
#Region  Project Attributes 
    #ApplicationLabel: Albie
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: true
#End Region

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

Sub Process_Globals
    Private player1 As SimpleExoPlayer
    Public MP4Text As String
End Sub

Sub Globals
    Private button1 As Button
    Private SimpleExoPlayerView1 As SimpleExoPlayerView
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    Log(File.DirDefaultExternal)
    Label1.Text=File.DirDefaultExternal
    player1.Initialize("player")
End Sub

Sub button1_click
    Dim DirName As String
    MP4Text="MrTumble.mp4"
    player1.CreateFileSource(File.DirDefaultExternal,MP4Text)
    SimpleExoPlayerView1.Player = player1
    player1.Play
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

just plain and simple until I know i can do it, then I'll expand on the interface, wasn't being funny about the delay, just wondered where you were based, appreciate the help
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
android:targetSdkVersion="26"
Log("Exists: " & File.Exists(File.DirDefaultExternal,MP4Text)) output is Exists: true
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Sorted, after messing about with various methods
seems the code I was using for the defualt directory need tweaking
B4X:
Sub button1_click
    Dim DirName As String
    MP4Text="MrTumble.mp4"
    player1.CreateFileSource(File.DirDefaultExternal,MP4Text)
    SimpleExoPlayerView1.Player = player1
    player1.Play
End Sub

changed it to
B4X:
Sub button1_click
    Dim DirName As String
    MP4Text="MrTumble.mp4"
    Label2.Text=File.DirDefaultExternal & "/" & MP4Text
    Label1.Text="Exists: " & File.Exists(File.DirDefaultExternal,MP4Text)
    player1.Prepare(player1.CreateFileSource(File.Dirdefaultexternal, MP4Text))
    SimpleExoPlayerView1.Player = player1
    player1.Play
    'player1.CreateFileSource(File.DirDefaultExternal,MP4Text)
    'SimpleExoPlayerView1.Player = player1
    'player1.Play
End Sub

player1.prepare was required, I knew I'd used it before so found my old code and tried it, now happy, thanks for all your help, cheers, COlin
 
Upvote 0
Top