cinder92 / react-native-get-music-files

React Native package to get music files from local and sd for iOS and Android
MIT License
128 stars 64 forks source link

Get files from specific folder #67

Open ajoykarmakar opened 4 years ago

ajoykarmakar commented 4 years ago

I have a music application where the user can download the music file also. So when I fetched the music files I got all the files that the user store in his storage device but I want only those mp3 that user downloaded and stored in the "download" folder. How can I achieve this feature?

Drazail commented 4 years ago

@ajoykarmakar i guess you could use some fs module ( like RNFetchBlob ) to retrieve URIs you want, then use this packages to extract the metadata.

however, in my opinion, the more sensible approach would be to store the metadata and path/URL upon fetching them from the back-end( in some local database like sqLite or realm ).

ajoykarmakar commented 4 years ago

Yupp, I have used this plugin to download the songs and stored in the storage but how can I get only from a specific folder where the user stored the songs. my current code is -

  MusicFiles.getAll({               
                    duration: true, //default : true       
                    fields: ['title', 'artwork', 'duration', 'artist', 'genre', 'lyrics', 'albumTitle']
                }).then(tracks => {
                    this.state.loading = false
                    this.setState({
                        offlineresults: tracks,
                        loading: false
                    })
                }).catch(error => {
                    // console.log(error)
                })

I got all songs from this code.

Drazail commented 4 years ago

you could use RNAndroidAudioStore.getSongByPath ( see here) to retrieve the metadata from a path, however this wont work on iOS.


to be honest though, for your use case, I truly think it would be a better solution to just use a local database instead of this package.

I assume you fetch the metadata and the urls from a back-end service at some point, store them in a database. you can also add a path field, set it to null, and insert the the download path into that field in the callback on the download function of RNFetchBlob.


It is possible implement a function to get songs from a specific path, something like this should work on the android native side:

private Cursor cursor;
private int columnIndex;

String[] projection = {MediaStore.Audio.Media._ID};
cursor = managedQuery( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection, 
        MediaStore.Audio.Media.DATA + " like ? ",
        new String[] {"%PATH_TO_FOLDER_CONTAINING_SONGS%"},  
        null);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);

however, I don't think this is a reasonable solution for your use case either.

ajoykarmakar commented 4 years ago

Yeah, I can do it with local storage but if the user somehow deletes the mp3 file from the file manager then the app still shows the file but didn't able to play the music. I think that's wrong and a bad UX and thank you for your coding support but that's a java code, How to implement this code, I have no idea can you help me out, please?

Drazail commented 4 years ago

honestly this is how we handled this issue in our app:

we use this package only to discover songs, artists, albums, playlists, ... which are not viewed, played, downloaded or created by our own app.

* I will try to implement a getSongsByPath as soon as I can.

ajoykarmakar commented 4 years ago

It will be really helpful if you are able to implement these features and my app run only on the android version so if you help me only on the android version still it will completely fine.

Drazail commented 4 years ago

@ajoykarmakar this function is now implemented in v3dev branch of my fork.

https://github.com/Drazail/react-native-get-music-files/tree/v3dev#getsongbypaths

iasreact1 commented 1 year ago

@Drazail I implemented react-native-get-music-files with my react-native-0.70.6 and as my react version higher not going for manual installation and when i build the project it gives me the error for android.support.annotaion which is due to androidx, and i changed it to androidx and the project will build successfully and now when i use the code MusicFiles.getAll({ blured: false, // works only when 'cover' is set to true artist: true, duration: true, //default : true cover: false, //default : true, genre: true, title: true, cover: true, minimumSongDuration: 10000, // get songs bigger than 10000 miliseconds duration, batchNumber : 1, delay: 1000 }) .then(resp => { // console.log("resd", resp); }) .catch(er => alert(JSON.stringify(error))); and during each time i call that the app get crashed without any error message , i cannot understand what to do to get my device music files , please help @Drazail , @ajoykarmakar

Drazail commented 1 year ago

@iasreact1

Its been a while since I worked on this. however, if you are interested, you can take a look at the source code of my repo (which is practically the android side of this package) and let me know if you need any help with that.

13627491210 commented 2 months ago

@ajoykarmakar该功能现已在我的 fork 的 v3dev 分支中实现。

hello ,i used it ,but failed 。

13627491210 commented 2 months ago

@ajoykarmakar该功能现已在fork的v3dev分支中实现。

你好,我用过了,但是失败了。 this is my code 。
import GetMusicFiles, { Constants, CoverImage } from "react-native-get-music-files-v3dev-test"; GetMusicFiles.getSongByPath({ cover: true, coverFolder: "pathToCoverFolder", path: "/storage/emulated/0/Download/" }) .then(f => { console.log(f); }) .catch(er => console.log(e)); };
i get a error 。 [TypeError: undefined is not a function]

13627491210 commented 2 months ago

@ajoykarmakar该功能现已在fork的v3dev分支中实现。

你好,我用过了,但是失败了。 this is my code 。 import GetMusicFiles, { Constants, CoverImage } from "react-native-get-music-files-v3dev-test"; GetMusicFiles.getSongByPath({ cover: true, coverFolder: "pathToCoverFolder", path: "/storage/emulated/0/Download/" }) .then(f => { console.log(f); }) .catch(er => console.log(e)); }; i get a error 。 [TypeError: undefined is not a function]

@Drazail please help me , GetMusicFiles.getSongs({ ... }) is OK, but GetMusicFiles.getSongByPaths and GetMusicFiles.getSongByPath is error 。