Hi every one i write a code to play fm radio waves. In my code there are a progressbar and two buttons one of them for play and another for stop. when i click play button it not happen. how can i solve this problem? or is there good suggest for me to play audio m3u8 files?
The code I'm using is:
public class Main extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://url.m3u8");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
I solve this problem by vitamio library. thanks
Try to call setOnPreparedListener() before prepareAsync() if you don't get onPrepared() callback.
According to the documentation, Android 4.0 and above doesn't support mp3 chunks. It supports only ts-files.
Furthermore, if your link hasn't .m3u8 in the end a MediaPlayer can't play it. Which tells that Content-Type: application/vnd.apple.mpegurl is abandoned. And you must have .m3u8 extension in the link.
Related
i am trying to play media file with the following code using floating action button.
it works fine so far but it keeps playing even when i switch to another activity. I want to stop media player when back button pressed. Please help me...
Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final MediaPlayer mp = new MediaPlayer();
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.stop();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("audio/sound.mp3");
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onBackPressed() {
//here i want to stop media player with back button
super.onBackPressed();
}
Override OnDestroy and add below code:
#Override
public void onDestroy() {
if (mp != null) mp.release();
}
I try to integrate a radio streaming in my app but it don't work and i don't know why.
This is the link of my web radio : https://www.radioking.com/widgets/player/player.php?id=480&c=%23f39c12&ii=&p=1&pp=1&i=1&eq=1&po=1&t=1&a=1&f=big
Can you help me ?
This is my code :
public class MainActivity extends AppCompatActivity {
private Button btn;
private boolean playPause;
private MediaPlayer mediaPlayer;
private ProgressDialog progressDialog;
private boolean initialStage = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.audioStreamBtn);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
progressDialog = new ProgressDialog(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!playPause) {
btn.setText("Pause Streaming");
if (initialStage) {
new Player().execute("https://www.radioking.com/widgets/player/player.php?id=480&c=%23f39c12&ii=&p=1&pp=1&i=1&eq=1&po=1&t=1&a=1&f=big");
} else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
btn.setText("Launch Streaming");
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
playPause = false;
}
}
});
}
#Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
class Player extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... strings) {
Boolean prepared = false;
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
initialStage = true;
playPause = false;
btn.setText("Launch Streaming");
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
prepared = true;
} catch (Exception e) {
Log.e("MyAudioStreamingApp", e.getMessage());
prepared = false;
}
return prepared;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (progressDialog.isShowing()) {
progressDialog.cancel();
}
mediaPlayer.start();
initialStage = false;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Buffering...");
progressDialog.show();
}
}
}
Try ExoPlayer which supports features like Dynamic adaptive streaming over HTTP (DASH), SmoothStreaming and Common Encryption, which are not supported by MediaPlayer. It's designed to be easy to customize and extend.
Here is link https://developer.android.com/guide/topics/media/exoplayer.html
You can use exoplayer2 but you need to use the direct link to the stream and not the radio directory php link. Good luck.
I have written an app for a radio station, that is working well. If I click to go back to the desktop and click to activate the application again, it causes the app to stream again, resulting in 2 streams running concurrently.
Here is my code;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setTitle("Standart FM");
startButton = (Button) findViewById(R.id.buttonBasla);
facebookButton = (Button) findViewById(R.id.buttonFacebook);
twitterButton = (Button) findViewById(R.id.buttonTwitter);
Intent myService = new Intent(this, MyMediaPlayerService.class);
pd = new ProgressDialog(MainActivity.this);
mediaPlayer = new MediaPlayer();
if (isOnline()) {
pd.setCancelable(false);
pd.setMessage("Yükleniyor...");
pd.show();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer
.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
pd.dismiss();
}
});
} else {
....
}
startButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myService = new Intent(MainActivity.this,
MyMediaPlayerService.class);
if (mediaPlayer.isPlaying()) {
startButton.setBackgroundResource(R.drawable.andplaybtn);
mediaPlayer.stop();
} else {
startButton.setBackgroundResource(R.drawable.andstopbtn);
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
pd.setCancelable(false);
pd.setMessage("Yükleniyor...");
pd.show();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer
.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
pd.dismiss();
}
});
}
}
});
}
}
How do I solve this problem? How to prevent concurrent streaming in this Activity? Please help me.
You should stop your media player in onPause method, and also say mediaPlayer=null;
#Override
protected void onPause() {
super.onPause();
mediaPlayer.stop();
mediaPlayer=null;
}
Streaming MediaPlayer not working on Samsung galaxy s3. Don't have device myself but getting reports of it not working. Also tried Remote Test Lab and does not work. I have tested on many other devices and all work excepted for s3. Any help would be awesome!
Code:
public class Radio extends Activity {
private MediaPlayer mp;
private ImageButton pauseicon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player_1);
Toast.makeText(this, "Just one moment please", Toast.LENGTH_LONG).show();
pauseicon = (ImageButton) findViewById(R.id.pauseicon);
getActionBar().setDisplayHomeAsUpEnabled(true);
/**
* Play button click event plays a song and changes button to pause
* image pauses a song and changes button to play image
* */
String res = "http://************";
mp = new MediaPlayer();
try {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(res);
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
mp.start();
}
});
mp.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
}
pauseicon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// No need to check if it is pauseicon
if (mp.isPlaying()) {
mp.pause();
((ImageButton) v).setImageResource(R.drawable.playicon);
} else {
mp.start();
((ImageButton) v).setImageResource(R.drawable.pauseicon);
}
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
if (mp != null)
if (mp.isPlaying())
mp.stop();
mp.release();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
if (mp != null) {
if (mp.isPlaying())
mp.stop();
mp.release();
}
// there is no reason to call super.finish(); here
// call super.onBackPressed(); and it will finish that activity for you
super.onBackPressed();
}
}
This is what I do to get a streaming music to play, just tested this on a Samsung Galaxy SIII and it is working fine. The audio stream in this is a public university radio stream. But this should work with other online streams. Just make sure you enable the internet permissions in your manifest.
public class MainActivity extends Activity {
MediaPlayer mpMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
mpMediaPlayer = new MediaPlayer();
mpMediaPlayer.setDataSource("http://streams.tsc.usu.edu:8888");
mpMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mpMediaPlayer.prepare();
mpMediaPlayer.start();
}catch(Exception e){
e.printStackTrace();
}
}
This might be a little late, but for those who are coming to this looking for this answer, one solution might be to implement the MediaPlayer.OnPreparedListener.
public class MyActivity extends Activity implements MediaPlayer.OnPreparedListener
MediaPlayer mpMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
try{
mpMediaPlayer = new MediaPlayer();
mpMediaPlayer.setDataSource("audio url");
mpMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mpMediaPlayer.prepare();
mpMediaPlayer.setOnPreparedListener(this);
}catch(Exception e){
e.printStackTrace();
Log.e(TAG, "Error loading media");
}
}
#Override
public void onPrepared(MediaPlayer mp) {
mpMediaPlayer.start();
}
Recently I started with Android.
I'm trying to do a small example to understand how the sound output with android.
To this I build me a program that plays an mp3 file, and while this playing can change the sound output to:
Internal Speaker
External Speaker
Headset
Earpiece
Is this possible?
I have part of the code done, but do not know how to jump from a sound output to another.
public class TestAudioActivity extends Activity {
private MediaPlayer mediaPlayer;
private ImageButton playButton;
private ImageButton pauseButton;
private ImageButton stopButton;
AudioManager audioManager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeMediaPlayer();
playButton = (ImageButton) findViewById(R.id.playButton);
pauseButton = (ImageButton) findViewById(R.id.pauseButton);
stopButton = (ImageButton) findViewById(R.id.stopButton);
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mediaPlayer != null)
mediaPlayer.start();
else
initializeMediaPlayer();
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
/** if (mediaPlayer != null)
mediaPlayer.pause(); */
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(false);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
});
}
private void initializeMediaPlayer() {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("http://server3.pianosociety.com/protected/bach-bwv772-stahlbrand.mp3");
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// Mostramos mensaje en caso de error.
Toast.makeText(getApplicationContext(), "URL no encontrada", 2000);
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Can anyone help?
Thank you.
I have never played with media activities yet, but yes you can change your sound output while playing for example you can use
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);