python for android : mediaPlay(url) 用不瞭,怎麼辦

 

這樣寫就可以瞭.

play.py

 

# -*- coding: utf-8 -*-
import android
import os
droid = android.Android()
path = u'/sdcard/sl4a/test.mp3'
url = u'file://' + path
if os.path.exists(path):
    droid.startActivity('android.intent.action.VIEW',url,'audio/mp3')
else:
    print path,'not exists'

參考書中一個實用的例子 mplay.py

 

import android
import os, sys
droid = android.Android()
# Specify our root directory and make sure it exists.
base_dir = '/sdcard/sl4a'
if not os.path.isdir(base_dir):
    print base_dir,'is not dir'
    sys.exit(4)

def show_dir(path=base_dir):
    Shows the contents of a directory in a list view.
    # The files & directories under path.
    nodes = os.listdir(path)
    # Make a way to go up a level.
    if path != base_dir: nodes.insert(0, '..')
    droid.dialogCreateAlert(os.path.basename(path).title())
    droid.dialogSetItems(nodes)
    droid.dialogShow()

    # Get the selected file or directory.
    result = droid.dialogGetResponse().result
    droid.dialogDismiss()
    if 'item' not in result:
        return
    target = nodes[result['item']]
    target_path = os.path.join(path, target)
    if target == '..': target_path = os.path.dirname(path)
    # If a directory, show its contents.
    if os.path.isdir(target_path):
        show_dir(target_path)
    elif os.path.splitext(target)[1].lower() == '.mp3':
        url = 'file://' + target_path
        print url
        droid.startActivity('android.intent.action.VIEW',url, 'audio/mp3')
    # inform the user.
    else:
        droid.makeToast('Only .mp3 files are currently supported!')
        show_dir(path)

if __name__ == '__main__':
    show_dir()

 

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *