Sound plays back in Android Emulator but not on my G1 - java

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()

Related

Audio does not play in Android Q

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();

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)

streaming live audio in android with MP

i'm trying to stream live audio form the internet using setDataSource, however when I add the url it says it's an unhandled exception.. any ideas whats wrong here?
String daytonPolice = "http://relay.radioreference.com:80/691484064";
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(daytonPolice); // It will not take the string of my url
mp.prepareAsync();
mp.start();
The issue is because you are calling start() right after calling prepareAsync(). You need to set an onPreparedListener:
String daytonPolice = "http://relay.radioreference.com:80/691484064";
MediaPlayer mp = new MediaPlayer();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(daytonPolice); // It will not take the string of my url
mp.prepareAsync();

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.

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