Playing an audio file with ExoPlayer a few seconds after calling .prepare()
I have a feature in my app that has to play one short audio file multiple times, and using the code below I'm preparing ExoPlayer
to play the audio:
SimpleExoPlayer player;
private void readyExoPlayerRaw(int rawSound) {
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(rawSound));
final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(this);
try {
rawResourceDataSource.open(dataSpec);
} catch (RawResourceDataSource.RawResourceDataSourceException e) {
e.printStackTrace();
}
MediaSource audioSource =
new ExtractorMediaSource.Factory(
new DefaultDataSourceFactory(this, "appName"))
.createMediaSource(rawResourceDataSource.getUri());
player.prepare(audioSource);
//player.setPlayWhenReady(true);
}
The problem is that the sound plays fine when I uncomment the last line (//player.setPlayWhenReady(true);
), but since I'm playing the sound a few seconds after I run this method, this line of code wont work! I think because it waits for a callback from previous line (.prepare(...)
) and when it's ready, it will then play. So I thought that maybe I should call start()
or something like that on the player, but there's just no such method.
So I'm stuck with calling .prepare()
each time I want to play the audio, but since it plays in very short intervals, calling prepare()
causes an unsuitable delay.
So am I missing something? How can I play a prepared MediaSource
without preparing it again?
android audio exoplayer
add a comment |
I have a feature in my app that has to play one short audio file multiple times, and using the code below I'm preparing ExoPlayer
to play the audio:
SimpleExoPlayer player;
private void readyExoPlayerRaw(int rawSound) {
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(rawSound));
final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(this);
try {
rawResourceDataSource.open(dataSpec);
} catch (RawResourceDataSource.RawResourceDataSourceException e) {
e.printStackTrace();
}
MediaSource audioSource =
new ExtractorMediaSource.Factory(
new DefaultDataSourceFactory(this, "appName"))
.createMediaSource(rawResourceDataSource.getUri());
player.prepare(audioSource);
//player.setPlayWhenReady(true);
}
The problem is that the sound plays fine when I uncomment the last line (//player.setPlayWhenReady(true);
), but since I'm playing the sound a few seconds after I run this method, this line of code wont work! I think because it waits for a callback from previous line (.prepare(...)
) and when it's ready, it will then play. So I thought that maybe I should call start()
or something like that on the player, but there's just no such method.
So I'm stuck with calling .prepare()
each time I want to play the audio, but since it plays in very short intervals, calling prepare()
causes an unsuitable delay.
So am I missing something? How can I play a prepared MediaSource
without preparing it again?
android audio exoplayer
add a comment |
I have a feature in my app that has to play one short audio file multiple times, and using the code below I'm preparing ExoPlayer
to play the audio:
SimpleExoPlayer player;
private void readyExoPlayerRaw(int rawSound) {
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(rawSound));
final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(this);
try {
rawResourceDataSource.open(dataSpec);
} catch (RawResourceDataSource.RawResourceDataSourceException e) {
e.printStackTrace();
}
MediaSource audioSource =
new ExtractorMediaSource.Factory(
new DefaultDataSourceFactory(this, "appName"))
.createMediaSource(rawResourceDataSource.getUri());
player.prepare(audioSource);
//player.setPlayWhenReady(true);
}
The problem is that the sound plays fine when I uncomment the last line (//player.setPlayWhenReady(true);
), but since I'm playing the sound a few seconds after I run this method, this line of code wont work! I think because it waits for a callback from previous line (.prepare(...)
) and when it's ready, it will then play. So I thought that maybe I should call start()
or something like that on the player, but there's just no such method.
So I'm stuck with calling .prepare()
each time I want to play the audio, but since it plays in very short intervals, calling prepare()
causes an unsuitable delay.
So am I missing something? How can I play a prepared MediaSource
without preparing it again?
android audio exoplayer
I have a feature in my app that has to play one short audio file multiple times, and using the code below I'm preparing ExoPlayer
to play the audio:
SimpleExoPlayer player;
private void readyExoPlayerRaw(int rawSound) {
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
DataSpec dataSpec = new DataSpec(RawResourceDataSource.buildRawResourceUri(rawSound));
final RawResourceDataSource rawResourceDataSource = new RawResourceDataSource(this);
try {
rawResourceDataSource.open(dataSpec);
} catch (RawResourceDataSource.RawResourceDataSourceException e) {
e.printStackTrace();
}
MediaSource audioSource =
new ExtractorMediaSource.Factory(
new DefaultDataSourceFactory(this, "appName"))
.createMediaSource(rawResourceDataSource.getUri());
player.prepare(audioSource);
//player.setPlayWhenReady(true);
}
The problem is that the sound plays fine when I uncomment the last line (//player.setPlayWhenReady(true);
), but since I'm playing the sound a few seconds after I run this method, this line of code wont work! I think because it waits for a callback from previous line (.prepare(...)
) and when it's ready, it will then play. So I thought that maybe I should call start()
or something like that on the player, but there's just no such method.
So I'm stuck with calling .prepare()
each time I want to play the audio, but since it plays in very short intervals, calling prepare()
causes an unsuitable delay.
So am I missing something? How can I play a prepared MediaSource
without preparing it again?
android audio exoplayer
android audio exoplayer
asked Nov 20 at 9:53
SIMMORSAL
508
508
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you're on the right track!
setPlayWhenReady
is how you pause and play the audio, but it'll only actually play when it's "ready" aka done preparing.
So you should only have to call your readyExoPlayerRaw
once probably in onResume or onCreate. Then at the end of that method you can set playWhenReady(false)
so that it doesn't start playing until you tell it to.
Then when you want to play the sound, do something like the following:
private void playSound() {
// Go to the beginning of the audio (in case it has played before)
player.seekTo(0);
// Tell it to play the sound
exoPlayer. setPlayWhenReady(true);
}
Let me know if that works.
1
This worked like a charm. Thanks.
– SIMMORSAL
Nov 25 at 7:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390341%2fplaying-an-audio-file-with-exoplayer-a-few-seconds-after-calling-prepare%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you're on the right track!
setPlayWhenReady
is how you pause and play the audio, but it'll only actually play when it's "ready" aka done preparing.
So you should only have to call your readyExoPlayerRaw
once probably in onResume or onCreate. Then at the end of that method you can set playWhenReady(false)
so that it doesn't start playing until you tell it to.
Then when you want to play the sound, do something like the following:
private void playSound() {
// Go to the beginning of the audio (in case it has played before)
player.seekTo(0);
// Tell it to play the sound
exoPlayer. setPlayWhenReady(true);
}
Let me know if that works.
1
This worked like a charm. Thanks.
– SIMMORSAL
Nov 25 at 7:15
add a comment |
I think you're on the right track!
setPlayWhenReady
is how you pause and play the audio, but it'll only actually play when it's "ready" aka done preparing.
So you should only have to call your readyExoPlayerRaw
once probably in onResume or onCreate. Then at the end of that method you can set playWhenReady(false)
so that it doesn't start playing until you tell it to.
Then when you want to play the sound, do something like the following:
private void playSound() {
// Go to the beginning of the audio (in case it has played before)
player.seekTo(0);
// Tell it to play the sound
exoPlayer. setPlayWhenReady(true);
}
Let me know if that works.
1
This worked like a charm. Thanks.
– SIMMORSAL
Nov 25 at 7:15
add a comment |
I think you're on the right track!
setPlayWhenReady
is how you pause and play the audio, but it'll only actually play when it's "ready" aka done preparing.
So you should only have to call your readyExoPlayerRaw
once probably in onResume or onCreate. Then at the end of that method you can set playWhenReady(false)
so that it doesn't start playing until you tell it to.
Then when you want to play the sound, do something like the following:
private void playSound() {
// Go to the beginning of the audio (in case it has played before)
player.seekTo(0);
// Tell it to play the sound
exoPlayer. setPlayWhenReady(true);
}
Let me know if that works.
I think you're on the right track!
setPlayWhenReady
is how you pause and play the audio, but it'll only actually play when it's "ready" aka done preparing.
So you should only have to call your readyExoPlayerRaw
once probably in onResume or onCreate. Then at the end of that method you can set playWhenReady(false)
so that it doesn't start playing until you tell it to.
Then when you want to play the sound, do something like the following:
private void playSound() {
// Go to the beginning of the audio (in case it has played before)
player.seekTo(0);
// Tell it to play the sound
exoPlayer. setPlayWhenReady(true);
}
Let me know if that works.
answered Nov 24 at 19:13
Kyle Venn
1,824822
1,824822
1
This worked like a charm. Thanks.
– SIMMORSAL
Nov 25 at 7:15
add a comment |
1
This worked like a charm. Thanks.
– SIMMORSAL
Nov 25 at 7:15
1
1
This worked like a charm. Thanks.
– SIMMORSAL
Nov 25 at 7:15
This worked like a charm. Thanks.
– SIMMORSAL
Nov 25 at 7:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390341%2fplaying-an-audio-file-with-exoplayer-a-few-seconds-after-calling-prepare%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown