Playing Ogg Sound in Android - java

In my application, I am trying to play a sound file in ogg format, stored in raw folder in res directory of my application. When I press the button that calls below function, it just freezes with the button pressed and does not respond. In the end, I have to terminate the application from Eclipse. Nothing about an error or exception in Logcat.
In debugging mode, it enters create function and never comes back. What am I doing wrong?
private void playbeep()
{
mPlayer = MediaPlayer.create(this, R.raw.beep);
mPlayer.start();
mPlayer.release();
}

You start and release the MediaPlayer at the same time. Try taking this out and see if it works.
mPlayer.release();
Also, check my post here to make sure you have the MediaPlayer set up correctly. If all else fails, try your audio file in a different format.

Related

android - Why MediaPlayer delayes in playing mp3 audio?

I made a small app to practice audio playing in android using MediaPlayer, the app works great but there is a small delay of 1 second after clicking the play button and it's noticeable, I noticed this happens only when starting the audio file, when paused it resumes immediately with no delay,I googled around and seen people suggests using SoundPool instead of MediaPlayer but SoundPool is recommended for short audio clips while my app is playing a full song, what's the cause of this delay? is there a fix or work around this issue?
Here's my code:
private MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaPlayer = MediaPlayer.create(this,R.raw.this_is_america);
Button btnPlay = findViewById(R.id.btnPlay);
Button btnPause = findViewById(R.id.btnPause);
Button btnStop = findViewById(R.id.btnStop);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPlay:
Toast.makeText(getApplicationContext(), "Playing song",
Toast.LENGTH_SHORT).show();
mMediaPlayer.start();
break;
case R.id.btnPause:
Toast.makeText(getApplicationContext(), "Pausing song",
Toast.LENGTH_SHORT).show();
mMediaPlayer.pause();
break;
case R.id.btnStop:
Toast.makeText(getApplicationContext(), "Song stopped",
Toast.LENGTH_SHORT).show();
mMediaPlayer.reset();
mMediaPlayer = MediaPlayer.create(this,R.raw.this_is_america);
break;
}
}
Update: Turned out to be the mp3 audio file had silent pause at the beginning , tried another song and it works fine no noticeable delays , thank you greeble31 for suggesting to check that.
Your song has a silent pause at the beginning ;)
As #Lucefer mentioned, the Android platform has some small unavoidable latency due to the implementation of the audio stack. Or, at least it did a few years back, not sure what the current state of affairs is. At any rate, this delay is generally far too small (~10ms) to notice at the beginning of an audio file; it has more to do with response times for apps that simulate musical instruments and the like.
There is an audio latency issue in the Android operating system. There is a few milliseconds delay while audio recording and audio playing. You can learn more about this by navigating below URLs. And this latency is related to the device types.
https://developer.android.com/ndk/guides/audio/audio-latency
https://source.android.com/devices/audio/latency/measurements
You can use a native toolkit or native program (C, C++ ndk base) to minimize this latency. But you cannot do reduce it to 0Seconds. only minimize the latency.
There is https://superpowered.com/superpowered-android-media-server . You can get support from this but as I know you need to pay for it. I didn't try it. Therefore i don't know how much it reduced the latency.
If you want to remove silent part from your mp3. you can use ffmpeg wrapper for it. Go to https://github.com/WritingMinds/ffmpeg-android-java link there is a working ffmpeg wrapper for Android. you can easily use it.
using FFMPEG with silencedetect to remove audio silence you can find relevant FFmpeg command for it by navigating to this.
in the wrapper you don't need ffmpeg part. You need to replace it from -y. You can get those details when navigate to that wrapper

MediaPlayer plays my MP3 files but really quiet

I have an old Android app, targeted for SDK16, which played my own MP3 sounds loud and clear:
MediaPlayer mp;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.red_click);
mp.setVolume(1.0f, 1.0f);
mp.start();
I haven't done any Android coding for a few years, and now I started to work on my old project again, but now targeted to SDK23. I didn't change my code from the one above. Now my sounds are still playing in the app, but really really quiet.
I tried to search for an answer but couldn't find any: what has changed in MP implementation from SDL16, and how should I play my files now such that I get the full volume for them?

Android Radio App that streams my mp3 from a dropbox server

I have a dropbox media server that has a collection of mp3 files that I want to stream onto an android application.
I know that using the "MediaPlayer" is the best way to go in the API.
How my main concern is how do I automate the process, where music is being played one after another? As if it was like an internet radio app?
Could someone please point me in the correct location for guides or display example code would be great thank you in advance.
Place the music files names in an arraylist (e.g songs) then implement onCompletionListener then set it to your media player. Inside the listener restart the media player to play the next item.
myMediaPlayer.setOnCompletionListener(this);
public void onCompletion(MediaPlayer arg0) {
arg0.release();
if (counter < songs.size()) {
counter++;
arg0 = MediaPlayer.create(getApplicationContext(), songs.get(counter);
arg0.setOnCompletionListener(this);
arg0.start();
}
}
If the server gives apps audio files;put the files in a queue and play them using mediaplayer or another audio player.
Get first song from server and start playing it, meanwhile continue downloading other songs one by one.

MediaPlayer plays the music files only in debug mode ( Android )

I have written an Android application ( in eclipse) that plays a music file as follows:
MediaPlayer mediaPlayer = new MediaPlayer();
try
{
mediaPlayer.setDataSource(fileName);
mediaPlayer.prepare();
mediaPlayer.start();
// mediaPlayer.stop();
}
catch (Exception e)
{
Log.d("Exception---", e.getMessage());
}
When I run it, it does not play the file ( and does not show any exception ), but when I switch to 'debug mode', and trace it line by line, it plays the file. I'm really confused. Would you please help me find out what's wrong?
Thanks.
Put a log before "mediaPlayer.setDataSource(fileName)" and check if you are getting a valid fileName in release mode.
I had similar problem and in my case it didn't like the player.PrepareAsync() command. It wasn't giving any errors in debugger except some miniscule info in the output window which wasn't of any help. Realizing that something was tripping the player I started to change a few things and changed the player.prepareAsync to player.Prepare() and thats all it took in Xamarin.

mediaplayer.start() makes app crash only on Motorola Droid devices

I have a soundboard uploaded on the android market. The app is doing pretty well in the market(50,000+ downloads), but the developer console reports that I have an error, and this is bothering me.
All crash reports come from only one device - Motorola Droid. I've looked at what the error actually is, and it happens when I call the start() method for the MediaPlayer class. I get the following:
java.lang.NullPointerException:
at com.meeg.soundit.Soundboard.playAudio(Soundboard.java:2517)
the code for the method playAudio is as follows and line 2517 is mp.start():
public void playAudio(int resid){
final MediaPlayer mp = MediaPlayer.create(this, resid);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mp.release();
}
});
}
Like I said, my soundboard has over 50,000 downloads, and it has 80 reports, all from the Motorola Droid. Is this something that I should ignore because 80 reports isn't much compared to how many people have used this, is there a problem with Moto Droid's and MediaPlayer, or is it just my code thats faulty?
This was a problem earlier on some builds that causes playback from resources to not work quite right. But you should fix your code to check for null and display appropriate message to the user.

Categories

Resources