streaming live audio in android with MP - java

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

Related

SeekTo initially in Android's Mediaplayer makes mediaplayer not work

This is the method where I create my Mediaplayer and set it's source, but directly after I call seekTo it does't play anymore (If I give it some time-depends on the audio, it worksm--> Conclusion after debug).
public void makeAudio(boolean isInit){
try{
if(!isInit){
mediaPlayer = new MediaPlayer();
}
soundSource = episode.getAudio();
mediaPlayer.setDataSource(soundSource);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
soundLength = mediaPlayer.getDuration();
seekBar.setMax(soundLength/1000);
mediaPlayer.seekTo(progress);//the problem I guess
}
});
}catch (Exception e){
e.printStackTrace();
}
}
It shows me this error message:
E/MediaPlayerNative: Attempt to perform seekTo in wrong state: mPlayer=0x706c23ebc0, mCurrentState=0
I would like to have any suggestion about this, because I want to make my mediaplayer go to the progress value(I get it from my intent).
I suggest you to use ExoPlayer, it is an improved version of MediaPlayer and addListener works better for online audio.
You have to add listener until it is prepared and only then you can get the duration:
exoPlayer.addListener(new Player.Listener() {
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_READY) {
long realDurationMillis = exoPlayer.getDuration();
seekBar.setMax((int)realDurationMillis/1000);
dialog.dismiss();
}
if (playbackState == ExoPlayer.STATE_ENDED){
performEndExoPlayer();
}
}
});
Here is an example you can have a look https://developer.android.com/codelabs/exoplayer-intro#5

How to use MediaPlayer which plays music stored in a folder found in the asset

I am creating an android app from android studio.
My intention is to have a function to play different songs depending on the string argument.
MediaPlayer mysound;
public void play(String song){
mysound = Mediaplayer.create(this, "../../../../asset/soundlib/" + song);
mysound.play();
}
I tried the R.assets.song. It just does not work.
Is there a way to have the song named C.mp3? It says that they should not be capitalized and all of the arguments taken are basically chords like C A F...
Thank you
For android java use
void test_mp(String file_name)
{
MediaPlayer mediaPlayer = null;
mediaPlayer = new MediaPlayer();
try {
AssetFileDescriptor afd = act.getAssets().openFd(file_name);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
} catch (final Exception e) {
e.printStackTrace();
}
mediaPlayer.start();
}
So for your case, you can call it like test_mp("SoundLib/A.mp3");
Try below code for play assets song :
fun playSound(context: Context, assetsFileName: String?) {
try {
val mediaPlayer = MediaPlayer()
val descriptor: AssetFileDescriptor = context.getAssets().openFd(assetsFileName!!)
mediaPlayer.setDataSource(
descriptor.getFileDescriptor(),
descriptor.getStartOffset(),
descriptor.getLength()
)
descriptor.close()
mediaPlayer.prepare()
mediaPlayer.isLooping = false
mediaPlayer.start()
} catch (e: Exception) {
e.printStackTrace()
}
}
Thanks

Playing sound as Media/Alarm/Ringtone?

My app plays an alarm. In the Android Sound settings, this sound is controlled by the "Ring volume" slider and not the "Alarm volume" slider. How can I change the sound to be controlled by the "Alarm volume"?
public void doAlarm(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
if (!r.isPlaying())
r.play();
}
I have tried using setAudioAttributes, but the result was that the "Media Volume" slider controlled the volume:
public void doAlarm(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).build());
mp.setLooping(true);
mp.start();
}
What am I missing?
I have found a solution: not using the .create() method. Instead use .setDataSource() and .prepare(). Code below:
public void ringAlarm() {
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
if (alarmUri == null) {
Log.e("ringAlarm" , "alarmUri null. Unable to get default sound URI");
return;
}
MediaPlayer mp = new MediaPlayer();
// This is what sets the media type as alarm
// Thus, the sound will be influenced by alarm volume
mp.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM).build());
try {
mp.setDataSource(getApplicationContext(), alarmUri);
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
// To continuously loop the alarm sound
mp.setLooping(true);
mp.start();
}

Android MediaPlayer unknown error

I am creating a MediaPlayer instance in MainActivity's onCreate() method like this
MediaPlayer mPlayer = MediaPlayer.create(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav")));
It is created successfully but I get this error:
07-06 18:33:44.266 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-06 18:33:44.267 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
Also tried this but the same error on logcat:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav")));
} catch (IOException e) {
e.printStackTrace();
}
mp.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
I have tried different audio files with different formats but result is the same error. I have also tried searching the answer on stackoverflow but could not resolve the issue. Can you help me on this?
It seems there is some issue with your file. Update that with this :
musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
And then follow these steps:
1. getLoaderManager().initLoader(0,null,this);
2. #Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id){
case 0 : return new CursorLoader(getApplicationContext(),musicUri,null,null,null,null);
return new Loader<>(this);
}
3. #Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case 0 :
if(data != null && data.moveToFirst()) {
songTitleColumnIndex = data.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
songTitle = data.getString(songTitleColumnIndex);
songsList.add(songTitle);
} while (data.moveToNext());
}
break;
So the SongTitles are being put in the SongList which is an ArraList of String Type.
Hope This Helps.
Try using a FileInputStream to start your MediaPlayer with a FileDescriptor instead:
String yourFilePath = "/wherever/your/file/is.wav";
MediaPlayer mPlayer = new MediaPlayer();
try{
FileInputStream inputStream = new FileInputStream(yourFilePath);
mPlayer.setDataSource(inputStream.getFD());
inputStream.close();
mPlayer.prepare();
mPlayer.start();
}catch (IOException e){
Log.e("IOException", e.getMessage());
}

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

Categories

Resources