question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion about using raw, assets, and file path for audio

See original GitHub issue

I’ve tinkering with JcPlayer and success in implementing feature for raw, assets, and file path for the audio file. I’m using a pojo for categorizing each files based on its source. The pojo is like this:

public class FileAndOrigin {
   private String filePath;
   private Origin origin;
   ...
   // getter, setter, and constructor omitted.
}

where the Origin is an enum:

public enum Origin {
   URL, RAW, ASSETS, FILE_PATH
}

So, if we want to add the file we only need to do this:

ArrayList<FileAndOrigin> urls = new ArrayList<>();
urls.add(new FileAndOrigin("http://www.villopim.com.br/android/Music_01.mp3", Origin.URL));
urls.add(new FileAndOrigin("http://www.villopim.com.br/android/Music_02.mp3", Origin.URL));
urls.add(new FileAndOrigin(this.getFilesDir() + "/" + "13.mid", Origin.FILE_PATH));
urls.add(new FileAndOrigin("49.v4.mid", Origin.ASSET));
urls.add(new FileAndOrigin("56.mid", Origin.ASSET));
urls.add(new FileAndOrigin(String.valueOf(R.raw.a_203), Origin.RAW));
urls.add(new FileAndOrigin(String.valueOf(R.raw.a_34), Origin.RAW));
player.initWithPlaylist(urls, "Awesome music");

And in JcPlayerService, we only need to do a slight changes in play method (this is a rough change):

public void play(JcAudio jcAudio)  {
    this.currentJcAudio = jcAudio;

    try {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();

            if(jcAudio.getOrigin() == Origin.URL) {
                mediaPlayer.setDataSource(jcAudio.getUrl());
            } else if(jcAudio.getOrigin() == Origin.RAW) {
                AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(Integer.parseInt(jcAudio.getUrl()));
                if (afd == null) return; // TODO: Should throw error.
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                afd.close();
            } else if(jcAudio.getOrigin() == Origin.ASSET) {
                AssetFileDescriptor afd = getApplicationContext().getAssets().openFd(jcAudio.getUrl());
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                afd.close();
            } else if(jcAudio.getOrigin() == Origin.FILE_PATH) {
                Log.d(TAG, "file://"+jcAudio.getUrl());
                mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(jcAudio.getUrl()));
            }
            mediaPlayer.prepareAsync();

            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnErrorListener(this);

        } else if (isPlaying) {
            stop();
            play(jcAudio);

        } else {
            mediaPlayer.start();
            isPlaying = true;

            if(jcPlayerServiceListener != null)
                jcPlayerServiceListener.onContinueAudio();
        }
    }catch (IOException e){
        e.printStackTrace();
    }

    updateTimeAudio();
    jcPlayerServiceListener.onPlaying();

    if(notificationListener != null)
        notificationListener.onPlaying();
}

This is working currently.

But, Is it a good design for JcPlayer?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:30 (24 by maintainers)

github_iconTop GitHub Comments

5reactions
iballancommented, Nov 30, 2016

It sounds good.

I have suggestions though:

Instead of doing this:

urls.add(new FileAndOrigin("http://www.villopim.com.br/android/Music_01.mp3", Origin.URL));
urls.add(new FileAndOrigin("http://www.villopim.com.br/android/Music_02.mp3", Origin.URL));
urls.add(new FileAndOrigin(this.getFilesDir() + "/" + "13.mid", Origin.FILE_PATH));
urls.add(new FileAndOrigin("49.v4.mid", Origin.ASSET));
urls.add(new FileAndOrigin("56.mid", Origin.ASSET));
urls.add(new FileAndOrigin(String.valueOf(R.raw.a_203), Origin.RAW));
urls.add(new FileAndOrigin(String.valueOf(R.raw.a_34), Origin.RAW));

I suggest it to be written with Factory pattern and change the name of FileAndOrigin to JcFile, and it will become like this:

public class JcFile /*FileAndOrigin*/ {
   private String filePath;
   private Origin origin;
   ...
   // getter, setter, and constructor omitted.

  
  public static JcFile createWithRaw(@RawRes int rawId){
    // here is the JcFile creation stuff
    return new JcFile(String.valueOf(R.raw.a_203), Origin.RAW); // So user wont choose origin you will set it here
  }
  public static JcFile createWithAsset(Context con, String assetName){
    // here is the JcFile creation stuff
  }
  // ... and so on
}

Usage:

urls.add(JcFile.createWithRaw(R.raw.a_34));
urls.add(JcFile.createWithUrl("http://www.villopim.com.br/android/Music_01.mp3"));
urls.add(JcFile.createWithAsset(context,"49.v5.mid");
urls.add(JcFile.createWithFile(this.getFilesDir() + "/" + "13.mid"));

This will be much better and less mistaken than letting user choose the origin every time he enters the path.

Thank you

3reactions
jeancsanchezcommented, Dec 4, 2016

Ok guys, I made the changes. I think that it stayed good 😃 Please, pull from the master branch 👍 @joielechong @iballan

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to play audio file from raw/assets folder on the native ...
How to play audio file from raw/assets folder on the native/default media player? ; void copyFileToDir ; (String audioFile){ File ; test =...
Read more >
[Audio] Path of Common Voice cannot be used for ... - GitHub
It is a bit scary IMO to see raw bytes for users. Overall, I think it's better to leave the data in it's...
Read more >
MediaPlayer overview - Android Developers
You can play audio or video from media files stored in your application's resources (raw resources), from standalone files in the filesystem ...
Read more >
Reducing the file size of your build - Unity - Manual
... Assets - they are actually the extra data that is added to “raw” Asset files to store references and settings. ... Suggestions...
Read more >
Manage project assets in Logic Pro - Apple Support
You can also specify a recording path for audio files outside the project, in File > Project Settings > Recording > Audio Recording...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found