1822899841@qq.com
1822899841@qq.com
  • 发布:2016-11-24 15:12
  • 更新:2019-06-14 11:03
  • 阅读:1236

native.js扫描android手机本地音乐

分类:Native.js
<script type="text/javascript">  
            mui.init();  

            document.addEventListener("plusready", function() {  

                if(plus.os.name == "Android") {  
                    var Context = plus.android.importClass("android.content.Context");  
                    var Cursor = plus.android.importClass("android.database.Cursor");  
                    var MediaStore = plus.android.importClass("android.provider.MediaStore");  

                    var list = document.getElementById("list");  
                    var li;  

                    var resolver = Context.getContentResolver();  
                    var c =resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);  
                    c.moveToFirst();  

                    if(c != null) {  
                        while(c.moveToNext()) {  
                            //扫描本地文件,得到歌曲的相关信息  
                            var music_name = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));  
                            var music_singer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));  
                            var music_time = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DURATION));  
                            var music_path = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));  

                            li = document.createElement('li');  
                            li.className = "mui-table-view-cell mui-media";  
                            li.setAttribute('name', music_name);  
                            li.setAttribute('data-type', 'File');  
                            li.innerHTML = '<a class="mui-navigate-right">' +  
                                '<img class="mui-media-object mui-pull-left" src="img/folder.png">' +  
                                '<div class="mui-media-body">' + music_name +  
                                '<p class="mui-ellipsis">' + music_singer +  
                                '</p></div></a>';  
                            list.appendChild(li);  
                        }  
                    }  

                }  
            }, false);  

        </script>

但是一直出现Uncaught TypeError: Object function (create) { this.UUID = window.plus.tools.UUID('JSB'); this.TYPE = 'JSBObject'; var args = window.plus.ios.__Tool.process(arguments); if ( create && plus.tools.I......r' at pages/my/LocalMusic.html:110 的错误。就是这行var resolver = Context.getContentResolver();
不知道怎么解决。

2016-11-24 15:12 负责人:无 分享
已邀请:
1822899841@qq.com

1822899841@qq.com (作者)

解决了。

<script type="text/javascript">  
            mui.init();  

            document.addEventListener("plusready", function() {  

                if(plus.os.name == "Android") {  
                    var Context = plus.android.importClass("android.content.Context");  
                    var ContentResolver = plus.android.importClass("android.content.ContentResolver");  
                    var Cursor = plus.android.importClass("android.database.Cursor");  
                    var Uri = plus.android.importClass("android.net.Uri");  
                    var MediaStore = plus.android.importClass("android.provider.MediaStore");  
                    var main = plus.android.runtimeMainActivity();  

                    var list = document.getElementById("list");  
                    var li;  

                    var context = main;  
                    var Uri = new Uri();  
                    Uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
                    var resolver = new ContentResolver();  
                    resolver = context.getContentResolver();  
                    var c = new Cursor();  
                    c =resolver.query(Uri, null, null, null, null);  
                    c.moveToFirst();  

                    if(c != null) {  
                        while(c.moveToNext()) {  
                            //扫描本地文件,得到歌曲的相关信息  
                            var music_name = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));  
                            var music_singer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));  
                            var music_time = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DURATION));  
                            var music_path = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));  

                            li = document.createElement('li');  
                            li.className = "mui-table-view-cell mui-media";  
                            li.setAttribute('name', music_name);  
                            li.setAttribute('data-type', 'File');  
                            li.innerHTML = '<a class="mui-navigate-right">' +  
                                '<img class="mui-media-object mui-pull-left" src="img/folder.png">' +  
                                '<div class="mui-media-body">' + music_name +  
                                '<p class="mui-ellipsis">' + music_singer +  
                                '</p></div></a>';  
                            list.appendChild(li);  
                        }  
                    }  
                    c.close();  
                }  
            }, false);  

        </script>
水灵退散

水灵退散

大神,怎么获取文件路径啊

527455294@qq.com

527455294@qq.com

苹果呢?

JackietLiu

JackietLiu

c.moveToNext()返回false。

该问题目前已经被锁定, 无法添加新回复