"Prepare failed" error with media player after file copy

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I get a "Prepare failed" error when the code is run on the media load statement. Can you check the code and tell us what to fix to stop the errors?

If I use File.DirRootExternal it works ok and no errors.
If I use File.DirDefaultExternal it works ok and no errors.

Using File.DirAssets gives a "no such file or directory" error.
Using File.DirInternal gives a "prepare failed" error.
Using File.DirInternalCache gives a "prepare failed" error.

Thanks.

B4X:
Sub DownloadedMp3File

   ProgressBarDownloading.Indeterminate = True
   ProgressBarDownloading.Visible = True
   
    HttpUtils.Download("Job1", mp3Url)
End Sub


Sub JobDone(Job As String)

  Select Job
        Case "Job1"

            If HttpUtils.IsSuccess(mp3Url) Then 

            Dim mp3 As InputStream
                mp3 = HttpUtils.GetInputStream(mp3Url)
                File.Copy2(mp3,File.OpenOutput(File.DirInternal,"077.mp3",False))

            ProgressBarDownloading.Visible = False

            ToggleButtonPlayPause.Checked = True
            ToggleButtonPlayPause.Enabled = True
            blnCurrentPauseCheckedState = True
            blnCurrentPauseEnabledState = True
   
            mpTheMediaPlayer.Release
            mpTheMediaPlayer.Initialize2("SurahAlMorsalat")
            mpTheMediaPlayer.Load(File.DirInternal, "077.mp3")
            mpTheMediaPlayer.Play
            End If
    End Select
End Sub
 
Last edited:

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I tried the code and still get the prepare failed error.

Did I miss something with my coding?

B4X:
Sub JobDone(Job As String)

  Select Job
        Case "Job1"

'Dim p As Phone
'Dim sb As StringBuilder
'sb.Initialize
'p.Shell("df", Null, sb, Null)
'Msgbox(sb.ToString, "Free space:")

            If HttpUtils.IsSuccess(mp3Url) Then 

            Dim mp3 As InputStream
                mp3 = HttpUtils.GetInputStream(mp3Url)

            Dim out As OutputStream
            out = File.OpenOutput(File.DirInternal,"077.mp3",False)
            File.Copy2(mp3, out)
            out.Close

            ProgressBarDownloading.Visible = False

            ToggleButtonPlayPause.Checked = True
            ToggleButtonPlayPause.Enabled = True
            blnCurrentPauseCheckedState = True
            blnCurrentPauseEnabledState = True
   
            mpTheMediaPlayer.Release
            mpTheMediaPlayer.Initialize2("SurahAlMosalat")
            mpTheMediaPlayer.Load(File.DirInternal, "077.mp3")
            mpTheMediaPlayer.Play
            End If
    End Select


 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I tried the sample from this link and it worked only if "DirRootExternal" is used.

http://www.b4x.com/android/help/randomaccessfile.html#countingoutputstream

As soon as I used "DirInternal" it gets the same error as shown in this log.

Also why does the total bytes of the file show 2390502 for all different file sizes I try to download? Is this a number coming from the site I'm downloading from?

B4X:
2204936 out of 2390502
2204936 out of 2390502
2204936 out of 2390502
2275888 out of 2390502
2356976 out of 2390502
2362768 out of 2390502
2362768 out of 2390502
2390502 out of 2390502
Success!
main_response_streamfinish (B4A line: 370)
mpTheMediaPlayer.Load(File.DirInternal, "001.mp3")
java.io.IOException: Prepare failed.: status=0x1
   at android.media.MediaPlayer.prepare(Native Method)
   at anywheresoftware.b4a.objects.MediaPlayerWrapper.Load(MediaPlayerWrapper.java:79)
   at quran.repeater.main._response_streamfinish(main.java:928)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
   at anywheresoftware.b4a.BA$1.run(BA.java:218)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:3647)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   at dalvik.system.NativeStart.main(Native Method)
java.io.IOException: Prepare failed.: status=0x1
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
What device are you using, and what are the paths of DirInternal and DirRootExternal?
It looks to me that the MediaPlayer needs to re-scan media after the download. (May be just a hunch)
TotalBytes is returned in the Http header response from the server.
What you have shown in the logs, is the 'progress' of the download, not multiple files being downloaded.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
DirRootExternal is /mnt/sdcard
DirInternal is /data/data/myapp/files

This error occured on 2 different devices (C8 Herotab and ASUS Transformer).

I looked up these folders in the file manager and found that /mnt/sdcard did exist and /data/data/myapp/files did not exist.

How do I create this folder? I assumed that using file.copy2 would create the folder before doing the copy since there was no error message until I reached the media player load statement.

 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I did use:

B4X:
Dim blnFolderMade As Boolean
File.MakeDir(File.DirInternal, "MyFiles")
            
blnFolderMade = File.Exists(File.DirInternal,"MyFiles")
            
Msgbox(blnFolderMade, "Folder Made")

but when I use the file manager to see it I can't find it in /data/data/myapp/files

In fact the /data folder is empty without any folder in it.

Am I looking at the right place? Could this be a permissions thing that needs to be set? I hope B4A has a way to set the permissions so this fold can be seen.


I thought so too. Try using MkDir.
Also I am wondering how this will work with the mediaplayer.
Can the mediaplayer object access media from DirInternal of an app?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User

Unless your device is rooted you will not be able to look into it with a file manager.
Only your app can view inside it's internal storage area.
you can use file.listfiles to create a log of your DirInternal so you see that the file is actually there.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I get this error.

B4X:
Your submission could not be processed because a security token was missing or mismatched.

If this occurred unexpectedly, please inform the administrator and describe the action you performed before you received this error.

I will try to upload it to a file download service.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Thanks for looking into it.

Is there a way for the user to download the file into the Android device if they don't have an SD card since File.DirAssets can be updated?

This is an internal permission issue. For now you will only be able to use File.DirAssets (for packaged sound files) or the sd card. I will investigate it for the next update.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…