Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> python for android : mediaPlay(url) 用不了,怎麼辦

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

編輯:關於Android編程

 

這樣寫就可以了.

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()

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved