Suggestion about using raw, assets, and file path for audio
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:3
- Comments:30 (24 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It sounds good.
I have suggestions though:
Instead of doing this:
I suggest it to be written with Factory pattern and change the name of FileAndOrigin to JcFile, and it will become like this:
Usage:
This will be much better and less mistaken than letting user choose the origin every time he enters the path.
Thank you
Ok guys, I made the changes. I think that it stayed good 😃 Please, pull from the master branch 👍 @joielechong @iballan