I'm currently have a media player that is streaming an mp3 file. When that file is done, what is the code so that it goes to the next url/mp3?
And also, is there code to get the name of the file and display it? how would I go about doing that?
thanks
EDIT
see my code below:
package com.example.m3uplayer;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.media.MediaPlayer.OnCompletionListener;
public class m3uPlayer extends Activity implements MediaPlayer.OnCompletionListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
//http://dl.dropbox.com/u/24535120/AfroJack-UMF11-clip2.mp3
//http://dl.dropbox.com/u/24535120/Avicii%20clip%201.mp3
Uri myUri = Uri.parse("http://dl.dropbox.com/u/24535120/AfroJack-UMF11-clip2.mp3");
MediaPlayer sdrPlayer = new MediaPlayer();
try {
sdrPlayer.setDataSource(this, myUri);//"http://mp1.somafm.com:8032");
sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
sdrPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sdrPlayer.start();
}
#Override
public void onCompletion(MediaPlayer sdrPlayer) {
Uri myUri5 = Uri.parse("http://dl.dropbox.com/u/24535120/Avicii%20clip%201.mp3");
try {
sdrPlayer.setDataSource(this, myUri5);
sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
sdrPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sdrPlayer.start();
}
}
You can register an OnCompletionListener with the media player. When it receives its callback notification, you need to call reset(), setDataSource, and prepare() on the media player for the next URL.
I don't believe there is anything in the api to tell you what data source a media player is using. You need to keep track of that yourself.
Related
I have added two buttons to my app, one to play and one to start. As soon as i start the app the songs starts playing automatically. when i press on stops it stops but when i press on play it doesnt starts again
To stop I have used mediaplayer.stop() which is working fine
To start I have used
mediaplayer.start()
which is not working
According to flow diagram its given we need to use
prepare()
and then
OnPreparedListener.onPrepared()
and then
start()
I don't know how to use these functions. Please help me
Don't call stop() for stopping the mediaplayer use reset() instead. I dont have good reason for that. But, it'll work for sure.
// for stopping it call below statement
if(mediaPlayer.isPlaying())
mediaPlayer.reset();
//for playing it again
mediaPlayer.prepare();
mediaPlayer.start();
For starting media player again use following code.
if (mediaplayer != null) {
mediaplayer.start();
}
and one more thing, instead of using
mediaplayer.stop()
use
mediaplayer.pause();
so it will pause the current song instead of stop.
Just check the condition whether it is null or not.
Try following code on Play Button
mp.reset();
mp.setDataSource(song path);
mp.prepare();
mp.start();
mp.setDataSource(song path); not necessary if you playing same sound again
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
btnsound=(Button)findViewById(R.id.play_sound);
btnstopsound=(Button)findViewById(R.id.stop_sound);
mp=new MediaPlayer();
btnsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.setDataSource(Sound);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
mp.setLooping(true);
}
});
btnstopsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setLooping(false);
mp.stop();
}
});
}
i'm trying to develop an android voice recorder.i am able to record and play back the file but the problem is i can't get the decibel of the voice recording.the reason i need it is because i want to show a sound meter that when the app is recording it show's the sound decibel meanwhile.
any idea how can i do it?
Here is my code :
btRecord.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
recorder.start();
/**** i need a method like recorder.Volume(); ***/
}catch(Exception ex)
{}
I am trying to play an url but its not playing and the code i used is below..the logcat is showing Mediaplayer error(1,-1002), start state is 0 and error(-38, 0) why...? where i am going wrong......can u help me out how to play........
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class BacaFatihahActivity extends Activity {
final String songs_urIs= "http://stream.radiosai.net:8002/";
// private TextView txt_song_title;
private MediaPlayer mediaplayer;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton btn_play = (ImageButton) findViewById(R.id.button_play);
ImageButton btn_pause = (ImageButton) findViewById(R.id.button_pause);
ImageButton btn_next = (ImageButton) findViewById(R.id.button_next);
ImageButton btn_previous = (ImageButton) findViewById(R.id.button_Previous);
//txt_song_title = (TextView) findViewById(R.id.txt_song_title);
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
btn_play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mediaplayer.setDataSource(songs_urIs);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaplayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaplayer.start();
}
});
}
}
right code but wrong api level~
it's not every api level supports this way to play a media,http live streaming ,may be you need api level 10 or higher
Does any one have code they know will run android audio stream from a URL, that will work in android OS 2.2. I have looked on the developer site and there code doesn't seem to work for me. Some of the other code or tutorials are for older versions of android.
This was the code from the code i ve been using(won't work):
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Radio extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(URL);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// This is code for a button that starts the stream when clicked
Button bRadio = (Button) findViewById(R.id.button_stream);
bRadio.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View V) {
mp.start();
}
});
}
}
As you are setting datasource to URL, use mp.prepareAsync() instead of mp.prepare().
if you are ok with JNI, there is some sample code here:
the creating JNI URI player is liking this:
// configure audio source
SLDataLocator_URI loc_uri = {SL_DATALOCATOR_URI, (SLchar *) uri_utf8};
SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
SLDataSource audioSrc = {&loc_uri, &format_mime};
// configure audio sink
SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
SLDataSink audioSnk = {&loc_outmix, NULL};
// create audio player
const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME};
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
(*engineEngine)->CreateAudioPlayer(engineEngine, &uriPlayerObject, &audioSrc,&audioSnk, 3, ids, req);
(*uriPlayerObject)->Realize(uriPlayerObject, SL_BOOLEAN_FALSE);
(*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_PLAY, &uriPlayerPlay);
... get other interfaces...
then you could play:
(*uriPlayerPlay)->SetPlayState(uriPlayerPlay,SL_PLAYSTATE_PLAYING);
Hi I used the following code to call an activity when audio play get finished. But I want to play the audio in current activity, when play get finished it has to jump for the next activity. But here it jumps to the next activity and play the audio. Any suggestions.
package com.fsp.mus;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
public class MyRecording extends Activity{
String mFileName;
MediaPlayer mPlayer;
Boolean stopplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myrecording);
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mPlayer.start();
if(mPlayer.getCurrentPosition()== mPlayer.getDuration())
{
Intent stopplay=new Intent(MyRecording.this,Recorded_Message.class);
startActivity(stopplay);
}
}
}
mPlayer.setOnCompletionListener(new
OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
Intent stopplay= new Intent(MyRecording.this,Recorded_Message.class);
startActivity(stopplay);
}
});
I'll try to explain what is wrong in your code.
onCreate calls only one time when an Activity creates. So, in your code you set a source for your mPlayer:
mPlayer.setDataSource(mFileName);
then prepare it:
mPlayer.prepare();
and then starts playing:
mPlayer.start();
As you just started mPlayer, of cource if statment will be false and the code in if will never be performed:
if(mPlayer.getCurrentPosition()== mPlayer.getDuration())
{
Intent stopplay=new Intent(MyRecording.this,Recorded_Message.class);
startActivity(stopplay);
}
I remind you that onCreate is called only once.
So, use event as described above.