{"action":"created","issue":{"url":"https://api.github.com/repos/zmxv/react-native-sound/issues/519","repository_url":"https://api.github.com/repos/zmxv/react-native-sound","labels_url":"https://api.github.com/repos/zmxv/react-native-sound/issues/519/labels{/name}","comments_url":"https://api.github.com/repos/zmxv/react-native-sound/issues/519/comments","events_url":"https://api.github.com/repos/zmxv/react-native-sound/issues/519/events","html_url":"https://github.com/zmxv/react-native-sound/issues/519","id":390994815,"node_id":"MDU6SXNzdWUzOTA5OTQ4MTU=","number":519,"title":"我用 使用环信接收语音消息 用react native fs 下载 后 无法播放本地文件","user":{"login":"loss99","id":18660247,"node_id":"MDQ6VXNlcjE4NjYwMjQ3","avatar_url":"https://avatars0.githubusercontent.com/u/18660247?v=4","gravatar_id":"","url":"https://api.github.com/users/loss99","html_url":"https://github.com/loss99","followers_url":"https://api.github.com/users/loss99/followers","following_url":"https://api.github.com/users/loss99/following{/other_user}","gists_url":"https://api.github.com/users/loss99/gists{/gist_id}","starred_url":"https://api.github.com/users/loss99/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/loss99/subscriptions","organizations_url":"https://api.github.com/users/loss99/orgs","repos_url":"https://api.github.com/users/loss99/repos","events_url":"https://api.github.com/users/loss99/events{/privacy}","received_events_url":"https://api.github.com/users/loss99/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-12-14T07:22:01Z","updated_at":"2018-12-17T07:49:39Z","closed_at":null,"author_association":"NONE","body":"### **收到的语音消息的URL**   url: \"https://a1.easemob.com/1124180905177345/yjxm/chatfiles/210c4610-ff4c-11e8-a35e-4399f0ecd2fd\"\r\n### **下载 过程**\r\nconst downloadDest = `${RNFS.CachesDirectoryPath}/${((Math.random() * 1000) | 0)}.mp3`;\r\n        const formUrl = url;\r\n        const options = {\r\n            fromUrl: formUrl,\r\n            toFile: downloadDest,\r\n            background: true,\r\n            begin: (res) => {\r\n                console.log('begin', res);\r\n                console.log('contentLength:', res.contentLength / 1024 / 1024, 'M');\r\n            },\r\n            progress: (res) => {\r\n                let pro = res.bytesWritten / res.contentLength;\r\n                this.setState({\r\n                    progressNum: pro,\r\n                });\r\n            },\r\n        };\r\n        try {\r\n            console.log('12121',options);\r\n            const ret = RNFS.downloadFile(options);\r\n            ret.promise.then(res => {\r\n                console.log('语音下载成功success', res);\r\n                console.log('文件大下载地址是 downloadDest);\r\n                callback(downloadDest)\r\n            }).catch(err => {\r\n                console.log('err', err);\r\n                callback('error')\r\n            });\r\n        }\r\n        catch (e) {\r\n            console.log(error);\r\n        }\r\n### **播放 过程**\r\nthis.downloadFile(this.props.item.url,response=>{\r\n            if (response=='error'){\r\n                console.warn('下载失败了,请重试');\r\n            } else {\r\n                console.log('我已经下载熬好了 文件 可以播放了',response)\r\n                let localSong = response;\r\n                setTimeout(() => {\r\n                    //file://var/containers/Bundle/Application/76A725C1-78CF-4B02-8CEC-CE2013EA7077/RNProjectPlayground.app/184.mp3'\r\n                    console.log('我要播放的是',localSong)\r\n                    this.sound = new Sound(localSong, '', (error) => {\r\n                        if (error) {\r\n                            console.warn(error)\r\n                        } else {\r\n                            console.log('播放准备')\r\n                            this.sound.play((success) => {                       \r\n                                if (!success) {\r\n                                    Alert.alert('播放失败');\r\n                                }else {\r\n                                    alert('1')\r\n                                }\r\n                            });\r\n                            if (!this.state.duration) {\r\n                                this.setState({\r\n                                    duration: Math.floor(this.sound.getDuration())\r\n                                });\r\n                            }\r\n                        }\r\n                    });\r\n                }, 100)\r\n            }\r\n"},"comment":{"url":"https://api.github.com/repos/zmxv/react-native-sound/issues/comments/447752014","html_url":"https://github.com/zmxv/react-native-sound/issues/519#issuecomment-447752014","issue_url":"https://api.github.com/repos/zmxv/react-native-sound/issues/519","id":447752014,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0Nzc1MjAxNA==","user":{"login":"laihenyi","id":491662,"node_id":"MDQ6VXNlcjQ5MTY2Mg==","avatar_url":"https://avatars3.githubusercontent.com/u/491662?v=4","gravatar_id":"","url":"https://api.github.com/users/laihenyi","html_url":"https://github.com/laihenyi","followers_url":"https://api.github.com/users/laihenyi/followers","following_url":"https://api.github.com/users/laihenyi/following{/other_user}","gists_url":"https://api.github.com/users/laihenyi/gists{/gist_id}","starred_url":"https://api.github.com/users/laihenyi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laihenyi/subscriptions","organizations_url":"https://api.github.com/users/laihenyi/orgs","repos_url":"https://api.github.com/users/laihenyi/repos","events_url":"https://api.github.com/users/laihenyi/events{/privacy}","received_events_url":"https://api.github.com/users/laihenyi/received_events","type":"User","site_admin":false},"created_at":"2018-12-17T07:49:39Z","updated_at":"2018-12-17T07:49:39Z","author_association":"NONE","body":"你試試：\r\npath: '/var/containers/Bundle/Application/76A725C1-78CF-4B02-8CEC-CE2013EA7077/RNProjectPlayground.app/184.mp3'\r\n\r\n我是使用require()取得檔案，在resolveAssetSource()會的到 'file:///usr/.../{filename}.mp3'\r\n我查看RNSoundModule.java的createMediaPlayer function, 發現有使用File檢查檔案是否存在\r\n但File file = new File(fileName);會將我的路徑變為  'file:/usr/.../{filename}.mp3', 導致路徑錯誤無法取得檔案\r\n進一步查看MediaPlayer的setDataSource, 裡面已經有檢查檔案以及轉換路徑，轉換出的路徑是‘/usr/.../{filename}.mp3’，這是正確的\r\n\r\n所以我修改了RNSoundModule.java的createMediaPlayer, 移除了'File file = new File(fileName);'與'file.exists();' ，就可以順利的播放\r\n\r\n\r\n------\r\ntry it:\r\npath: '/var/containers/Bundle/Application/76A725C1-78CF-4B02-8CEC-CE2013EA7077/RNProjectPlayground.app/184.mp3'\r\n\r\nI is use require(), and resolveAssetSource is return 'file:///usr/.../{filename}.mp3'\r\nRead the createMediaPlayer of RNSoundModule.java for Android \r\n\r\nRNSoundModule.java: \r\nFile file = new File(fileName);  // ->  path convert to file:/usr/.../{filename}.mp3'\r\nfile.exists(); // -> will return false, will can't play\r\n\r\nIn setDataSource of MediaPlayer, has check file is exist, and convert path, \r\n(file:///usr/.../{filename}.mp3 - > /usr/.../{filename}.mp3)\r\nI remove  'File file = new File(fileName);'  and 'file.exists();' of RNSoundModule.java , will pass.\r\n\r\nYou can try it.\r\n"}}
{"actor":{"display_login":"laihenyi"}}