Audio does not play in Android Q - java

I am trying to play audio from SD Card in Android Q, unfortunately the audio player is not working on Android Q but it works perfectly in Android version pre Q like Android Pie.
Here is my code
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(getUrl));
mediaPlayer.prepare();
mediaPlayer.setVolume(0.5f, 0.5f);
mediaPlayer.setLooping(false);
seekBar.setMax(mediaPlayer.getDuration());
seekBarHint2.setText(convertDuration(mediaPlayer.getDuration()));
mediaPlayer.start();

Related

MediaPlayer delay 0.4 seconds for Bluetooth headset

I'm developing an application with playing audio. In several cases I change the AudioStreamType to AudioManager.STREAM_RING(is mode when music is playing both: speakers and headset). When I use Bluetooth headset, I have a small annoying delay.
So i was reading that Bluetooth has a buffer and and this is logical
, but how I can solve this? Also i tried to change buffer size or delay for mediaPlayer.
So, i create a mediaPlayer like as:
#Override
public void onInit() {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setLooping(true);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
songManager = new SongsManager(context);
}
#Override
public void onPrepare() {
// prepare media player
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
absolutePathToPlayingSong = /*some place*/;
mediaPlayer.setDataSource(/*some place*/);
mediaPlayer.prepare();
mediaPlayer.setVolume(volumeLvl, volumeLvl);
}
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
}
then i just start player:
mediaPlayer.start();
So, i already tried:
to change a prepare() to prepareAsync() - probably it was tiny help, to may be i had a sound hallucinations. Unfortunately it broke a seekTo() in one place
added onPrepared() and start playing from listener - I not sure that it help
Give me please any advance^ how to fix delay for Bluetooth headset? I checked this device in YouTube application and it work great. So problem in my application. sorry my bad English!)
I have 2 target devices android 7.0(SDK 24) and android 4.2 (SDK 16)

Play Video Using External Media Player in Android

Well, I've used VideoView to play my Video. It only support .mp4 format(Correct me if I'm wrong), I need to play .flv video. I've MX Player/VLC player installed on my Android phone. How do I load the list of available Media Players when I click Play Video Button in my Activity.
Below is the code I've written using Video View, if it helps
public void onClick(View v) {
File root = Environment.getExternalStorageDirectory();
String externalFilesDir = getExternalFilesDir(null).toString();
String videoResource = externalFilesDir +"/" + "VID_20160115_215637181.mp4";
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath(videoResource);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.seekTo(0);
videoView.start();
}
});
}
If You want to play video in some other player, you can use ACTION_VIEW intent
Example in this question: Android intent for playing video?

How to start a second media player ..?

I am developing an App where I want to play two mp3 files one is background music and another audio plays after 20 seconds.
mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec);
mediaPlayer1 = MediaPlayer.create(this,R.raw.sound3 );
private void buttonClick(){
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
try
{
mediaPlayer.start();
mediaPlayer1.start();
startPlayProgressUpdater();
}
catch (IllegalStateException e) {
mediaPlayer.pause();
}
}
Use MediaPlayer to play background music and SoundPool for everything else.
You can play two files in Mediaplayer by using this piece of code in a thread(don't use UI thread for this) :
mediaplayer = new MediaPlayer();
mediaplayer.reset();
//For media file 1
mediaplayer.setDataSource(dataSourceOne);
mediaplayer.prepare();
mediaplayer.start();
Thread.sleep(500);//Set the time as per your need.
//For media file 2
mediaplayer.reset();
mediaplayer.setDataSource(dataSourceTwo);
mediaplayer.setLooping(true);
mediaplayer.prepare();
mediaplayer.start();
As per the better implementation perspective, write an util class with all the common methods (play(),pause(),stop()) and call every method from your class based on the requirement using a thread.

Sound plays back in Android Emulator but not on my G1

For my Android game prototype, I'm playing back .wav resources using:
MediaPlayer mp = MediaPlayer.create(context, soundID);
mp.start();
This works fine in the Eclipse Android emulator, but when I run the same program on my G1, no sound happens. What could be the cause of this?
Try doing it this way:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
instead of MediaPlayer.create()

Doing an audio loop on Android

I have a track I want to play 'megadeth', I'm calling it by:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.megadeth);
And playing it by using 'mp.start'.
And I just want to know, how can I get this audio mp3 to loop?
Before adding
mp.start();
Add
MediaPlayer mp = MediaPlayer.create(MainMenu.this, R.raw.megadeth);
mp.setLooping(true);
the mp.setLooping(true); automatically allows the raw file to be used over and over again.

Categories

Resources