Reputation: 320
I am accessing the audio files from
/data/user/0/app_packagename/files/filename
I don't have WRITE_EXTERNAL
READ_EXTERNAL
permissions.
Now I can access files which are saved in the /data/user/0/app_packagename/files/
without WRITE_EXTERNAL
READ_EXTERNAL
permissions.
And can able to play all the audio files in my application.
In my application I am playing all files in queue. Few of my users are not able to play second audio file once 1st audio file completed.
Below is my code
void playNext(File varientFile) {
if (mSessionMediaPlayer != null) {
if (mSessionMediaPlayer.isPlaying()) {
mSessionMediaPlayer.stop();
}
mSessionMediaPlayer.release();
mSessionMediaPlayer = null;
}
mSessionMediaPlayer = new MediaPlayer();
if (mSessionMediaPlayer != null) {
Uri uri = Uri.parse(variantFile.getAbsolutePath());
mSessionMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mSessionMediaPlayer.setDataSource(this, uri);
mSessionMediaPlayer.setLooping(false);
mSessionMediaPlayer.prepare();
mSessionMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
playNext(mFiles.get(mNextIndex);
}
});
mSessionMediaPlayer.start();
runScheduler();
}
}
Instead of
mSessionMediaPlayer = new MediaPlayer();
I had tried with
mSessionMediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
Even MediaPlayer.create()
is returning null
. mSessionMediaPlayer.prepare()
is throwing an exception
Cause : Prepare failed.: status=0x1
Whats wrong with my code? Why it is getting null
or prepare failed
? I had tested the files as well they are working, audio file format is in .aac
.
The same user who is not able to play second audio file. He is able to play first file with the same code. But if same method is calling from onCompleted() it is not working. Why so?
Can anyone please help me to fix this issue?
Upvotes: 0
Views: 1006
Reputation: 2878
Instead of creating a new object every time, try with the same Media Player object something like this.
MediaPlayer mSessionMediaPlayer;
mSessionMediaPlayer = MediaPlayer.create(getApplicationContext(),uri);
mSessionMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mSessionMediaPlayer) {
moveToNext(mSessionMediaPlayer,nextUri);
}
});
mSessionMediaPlayer.prepare();
mSessionMediaPlayer.start();
private void moveToNext(MediaPlayer mSessionMediaPlayer,Uri uri){
mSessionMediaPlayer.create(getApplicationContext(), uri);
mSessionMediaPlayer.prepare();
mSessionMediaPlayer.start();
}
Upvotes: 0