-1

I am trying to develop an android media player using kivy and I am currently stuck on getting all the audio files data on the android device and populating the data in a recycle view. I have done some research and found out that I can achieve this with the help of the java MediaStore class but I don't know how to go about it.

I have tried looking at the pyjnius documentation but its not quite detailed for a beginner and so any help in terms of an illustration on how I could achieve this will be highly appreciated.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Wilson
  • 1

2 Answers2

0

Have not used MediaStore, but here is how I used pyjnius to install an apk file:

            from jnius import cast
            from jnius import autoclass
            # the download is the app, install it using an Android Intent

            PythonActivity = autoclass('org.kivy.android.PythonActivity') #request the Kivy activity instance
            Intent = autoclass('android.content.Intent') # get the Android Intent class
            Uri = autoclass('android.net.Uri')
            File = autoclass('java.io.File')

            intent = Intent() # create a new Android Intent
            intent.setDataAndType(Uri.fromFile(File(str(self.localUpdateFile))), "application/vnd.android.package-archive")
            intent.setAction(Intent.ACTION_VIEW) #set the action (use ACTION_VIEW for install)
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

            currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
            currentActivity.startActivity(intent) # show the intent in the activity

I would expect starting MediaStore would be similar, but not sure how you would get info from it.

John Anderson
  • 35,991
  • 4
  • 13
  • 36
0

Works for me, on Android <= 9

from jnius import autoclass

def get_songs(self):
    PythonActivity = autoclass('org.kivy.android.PythonActivity')
    Cursor = autoclass('android.database.Cursor')
    Uri = autoclass('android.net.Uri')

    contentResolver = PythonActivity.mActivity
    uri = Uri.parse("content://media/external/audio/media/")
    # uri = Uri.parse("content://media/internal/audio/media/")

    cursor = contentResolver.getContentResolver().query(uri, None, None, None, None)

    if cursor is not None and cursor.moveToFirst():
        while cursor.moveToNext():
            title = cursor.getString(cursor.getColumnIndex('MediaStore.Audio.Media.TITLE'))  # retrieve songs title
            # path = cursor.getString(cursor.getColumnIndex('MediaStore.Audio.Media._data'))  # retrieve songs path

            print(title)

    cursor.close()

MrDario
  • 1
  • 1
  • 2