1

I'm using the music player component from react-jinke-music-player to use it in my dash application, I'm wondering how can I play local mp3 files or midi files(not even working using urls) instead of urls.

Here is what I've tried so far:

MusicComponent.react.js

import React, {Component} from 'react';
import ReactJkMusicPlayer from 'react-jinke-music-player';
import PropTypes from 'prop-types';

const audioList1 = [
  {
    name: 'name',
    singer: 'singer name',
    cover:
      'http://res.cloudinary.com/alick/image/upload/v1502689731/Despacito_uvolhp.jpg',
    musicSrc:
      './gnawa.mp3',
  },
]
const options = {
  // audio lists model
  audioLists: audioList1,
  ... etc
}

export default class MusicComponent extends Component {
    render() {
        return (
            <div>
                <ReactJkMusicPlayer {...options}/>,
            </div>
        );
    }
}

MusicComponent.defaultProps = {};

MusicComponent.propTypes = {
    /**
     * The ID used to identify this component in Dash callbacks.
     */
    id: PropTypes.string,
};

Here's a slice of the project structure:

enter image description here

As you can see the mp3 file is in the same location as the MusicComponent.react.js component, why absolute path ./gnawa.mp3 not working here ?

And any help or advice will be appreciated. Thank you in advance.

Machavity
  • 30,841
  • 27
  • 92
  • 100
mhannani
  • 492
  • 1
  • 5
  • 18

1 Answers1

1

As you can see the mp3 file is in the same location as the MusicComponent.react.js component, why absolute path ./gnawa.mp3 not working here ?

Because the absolute URL that ./gnawa.mp3 resolves to does not map on to that file … and you haven't done anything to give a URL to that file.

You need to either:

  • Put the file somewhere it has an actual HTTP(S) URL and use that URL
  • Have the user pick the file with a <input type="file"> and then read it using the FileReader API
  • import it into a variable after having configured whatever bundler your project is using to support that file format (e.g. as per this question).
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks for the quick response @Quentin, Actually I've tried the third suggestion you provided but in vaine, I will to update my question with that. How about playing Midi files(at least using urls), Do you have any idea ? – mhannani Jun 11 '21 at 15:52
  • 1
    It's better to use `URL.createObjectURL(file)` instead of using the FileReader – Endless Jun 12 '21 at 11:43