I am developing a simple radio streaming application to play radio using a URL. This application is working in all the versions except for V>=4.0
Do anyone have any Idea over this.
initializeUIElements();
StartPlaying();
private void initializeUIElements() {
buttonPlay = (ImageView) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
playSeekBar=(ProgressBar)findViewById(R.id.progressBar1);
}
private void startPlaying() {
initializeMediaPlayer();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
Log.i("on prepared", "on prepared");
mp.start();
}
});
player.setOnErrorListener(new OnErrorListener(){
public boolean onError(MediaPlayer arg0, int arg1,
int arg2) {
Toast.makeText(getApplicationContext(),"An error happened while preparing radio",Toast.LENGTH_LONG).show();
player.reset();
initializeMediaPlayer();
return false;
}
});
private void initializeMediaPlayer() {
player = new MediaPlayer();
player.reset();
try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://***************");
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.i("percent", ""+percent);
if( (percent!=0)||(percent==100) )
{
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay.setVisibility(View.VISIBLE);
}
}
});
In Android version 4.0, It is giving an error as:
11-23 13:06:37.329: E/MediaPlayer(4011): Error (1,-2147483648)
and Player.bufferingUpdateListener() is not called. Here It is showing just that playseekbar revolving and revolving.. Please help on this.
It can be a problem with your particular device. Try running the same on 4.0 emulator.
Related
I have one mp4 video and i try to play my video in TextureView.
I have one problem. In Nexus or HTC one i have no problem but in some Huawei devices i have this message
Unable To create Media player
this is a my source
private void playVideoAnimation()
{
textureView.setVisibility(View.VISIBLE);
mMediaPlayer = new MediaPlayer();
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
Log.e("FILE_NAME", FILE_NAME);
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if (mediaPlayer.isPlaying()) {
}
mediaPlayer.start();
}
});
mMediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.e("FILE_NAME", "Finished");
}
});
}
as I said I can play my video in Nexus and HTC devices but yesterday i run my app in Huawei and i have this error message.
How i can check if device can play video?
If anyone knows solution please help me
I tried to use the lib prebuilt, but couldn't, I got error on - FFmpegMediaPlayer mp = new FFmpegMediaPlayer();
OR can somebody to recommend good Android MediaPlayer(minSdkVersion >=9) library for playing stream audio?
GIT URL for FFmpegMediaPlayer
have sample code :
FFmpegMediaPlayer mp = new FFmpegMediaPlayer();
mp.setOnPreparedListener(new FFmpegMediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(FFmpegMediaPlayer mp) {
mp.start();
}
});
mp.setOnErrorListener(new FFmpegMediaPlayer.OnErrorListener() {
#Override
public boolean onError(FFmpegMediaPlayer mp, int what, int extra) {
mp.release();
return false;
}
});
try {
mp.setDataSource("<some path or URL>");
mp.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Im having some problems playing a simple song (stored in the external SD) using MediaPlayer API. I really dont know what Im doing wrong, cause I havent seen the code error in any other questions and using the developer I havent found it either.
The error Im getting is this:
08-25 00:20:49.514: D/MediaPlayer(26764): mPlayerID = 94
08-25 00:20:49.519: E/MediaPlayer(26764): error (1, -2147483648)
08-25 00:20:49.597: E/MediaPlayer(26764): Error (1,-2147483648)
AudioActivity.java
public class AudioActivity extends Activity{
Audio audioPlayer;
public AudioActivity(){
audioPlayer = new Audio();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set of view where videos and the listView (is there is any) will appear
setContentView(R.layout.audio_player);
Button play = (Button)findViewById(R.id.playButton);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String path = Environment.getExternalStorageDirectory() + "/" + "song";
audioPlayer.loadFile(path);
audioPlayer.prepare();
audioPlayer.play();
}
});
Button pauseResume = (Button)findViewById(R.id.pauseButton);
pauseResume.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
audioPlayer.pause();
}
});
Button stop = (Button)findViewById(R.id.stopButton);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
audioPlayer.stop();
}
});
}
}
Audio.class
public class Audio {
MediaPlayer mediaPlayer;
String pathSong;
public Audio(){
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
Log.d("Error", "Arg1: " + arg1 + ".Arg2: " + arg2);
mediaPlayer.reset();
return false;
}
});
}
public void loadFile(String path){
try {
mediaPlayer.setDataSource(path);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void prepare(){
try {
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}
public void stop(){
mediaPlayer.stop();
}
public void pause(){
mediaPlayer.pause();
}
public int getDuration(){
return mediaPlayer.getDuration();
}
public MediaPlayer getMediaPlayer(){
return mediaPlayer;
}
public TrackInfo[] getTrackInfo(){
return mediaPlayer.getTrackInfo();
}
}
Any idea? Why the code (1, -2147483648) doesnt appear anywhere? Thanks!
I am developing one application where i want to play live stream radio. I have an url using which i will stream the radio and play. I have a play button by clicking which i want to play the radio. For that, i have written some code which is not at all working. Here is my code:
mp = new MediaPlayer();
try {
mp.setOnPreparedListener(this);
Log.d("Testing", "start111");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
String url="xxxxxx";
mp.setDataSource(url);
mp.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
Log.d("Testing", "Exception ::: 1111 "+e.getMessage());
} catch (IllegalStateException e) {
Log.d("Testing", "Exception ::: 2222 "+e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.d("Testing", "IOException ::: 3333 "+e.getMessage());
e.printStackTrace();
}
Can anyone please help me??
You can find good information regarding radio streaming.
Github radio streaming example
and also there is a question in SOF which can also be helpful
Stackoverflow radio streaming example
Hope it will help. thanks
Try this.
public class RadioStream extends Activity {
private final static String stream = "http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio2_mf_p";
Button play;
MediaPlayer mediaPlayer;
boolean started = false;
boolean prepared = false;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_stream);
play = (Button) findViewById(R.id.play);
play.setEnabled(false);
play.setText("Loading..");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (started) {
mediaPlayer.pause();
started = false;
play.setText("Play");
} else {
mediaPlayer.start();
started = true;
play.setText("Pause");
}
}
});
new PlayTask().execute(stream);
}
#Override
protected void onPause() {
super.onPause();
/* if(started)
mediaPlayer.pause();*/
}
#Override
protected void onResume() {
super.onResume();
/*if(started)
mediaPlayer.start();*/
}
#Override
protected void onDestroy() {
super.onDestroy();
// mediaPlayer.release();
}
private class PlayTask extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
}
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
play.setEnabled(true);
play.setText("Play");
}
}
}
Please do try the code below and call the given method at the on create of your activity or at the onclick listener of your button. Remember to handle the stop and start button the imgV is an imageView for my button.
private MediaPlayer player;
private void startMediaPlayer() {
String url = "http:yoururl.com"; // your URL here
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException 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();
}
if(isPlaying){
try {
mediaPlayer.prepareAsync();
progress.setVisibility(View.VISIBLE);
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
}
mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
}
});
}
boolean isPlaying = true;
private void startPlaying() {
isPlaying = true;
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
imgV.setImageResource(R.drawable.stop);
}
private void stopPlaying() {
if (mediaPlayer.isPlaying()) {
isPlaying = false;
mediaPlayer.stop();
mediaPlayer.release();
initializeMediaPlayer();
}
imgV.setImageResource(R.drawable.play);
}
I have a Mp3Link need to play it on my Device in a Separate Thread
I have tried this,But when i execute my code ,I'm not able to play the Music,Could any one look at My Code,What Went Wrong?
Here My Code:
Thread trd = new Thread(new Runnable(){
public void run(){
//code to do the HTTP request
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(mp3Link);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
Log.d("Mediaplyer>>>>>>>>", "Mediaplyer>>>>>>>>");
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
}
});
trd.start();
How about put
mediaPlayer.prepareAsync();
at the end of run() function ?
Thread trd = new Thread(new Runnable(){
public void run(){
//code to do the HTTP request
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(mp3Link);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// mediaPlayer.prepareAsync(); // <== Marked
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
Log.d("Mediaplyer>>>>>>>>", "Mediaplyer>>>>>>>>");
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
mediaPlayer.prepareAsync(); // <== Add
}
});
trd.start();
Hope this helps.