Radio streaming android - android

Hi I want to implement a radio in my application...I wrote this code, it worked in the emulator YouWave but il dosen't worked in a SmartPhone and another tablet and I don't know why..can you help me please?
This is the code of the radio :
public class radio extends Activity {
MediaPlayer media;
Button buttonplay;
Button buttonStopRecord;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
buttonStopRecord = (Button) findViewById(R.id.Stop);
buttonStopRecord.getBackground().setAlpha(150);
buttonStopRecord.setEnabled(false);
buttonStopRecord.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
if ( media.isPlaying() ) {
media.stop();
}
}
});
buttonplay = (Button) findViewById(R.id.play);
buttonplay.getBackground().setAlpha(150);
buttonplay.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
try
{
media = new MediaPlayer();
media.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
media.setDataSource("http://indiespectrum.com:9000");
media.prepare();
media.start();
if ( media.isPlaying()) {
buttonplay.setEnabled(false);
buttonStopRecord.setEnabled(true);
}
}
catch(Exception e)
{
//Getting Exception
}
}
});
Button accueil=(Button)findViewById(R.id.accueilr);
accueil.getBackground().setAlpha(200);
accueil.getBackground().setAlpha(150);
accueil.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
try{
if ( media.isPlaying() ) {
media.stop();
}
}catch (Exception e)
{
Intent i=new Intent(radio.this,main.class);
startActivity(i);
}
Intent i=new Intent(radio.this,main.class);
startActivity(i);
}
});
}
}

From what I know this code should work on every device with Api higher than 8.
media = new MediaPlayer();
media.setAudioStreamType(AudioManager.STREAM_MUSIC);
media.setDataSource("http://indiespectrum.com:9000");
media.prepareAsync();
media.seOnPreparedListener(this);
And inside the OnPrepared method put media.start(). This worked for me.

Related

Mediaplayer Background Audio not work properly Using URL And Song Not Play Second Time

This is my second activity and when open this activity threw button activity take load and open after some time and some times UI Freeze and some time black screen open for some seconds
public class AartiActivity extends AppCompatActivity {
final MediaPlayer player1 = new MediaPlayer();
private Button btn_playarti, btn_pausearti, btn_stoparti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aarti);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
btn_playarti = (Button) findViewById(R.id.btn_playarti);
btn_pausearti = (Button) findViewById(R.id.btn_pausearti);
btn_stoparti = (Button) findViewById(R.id.btn_stoparti);
try {
player1.setDataSource(STRING_URL);
player1.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btn_playarti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
player.stop();
}
});
player1.start();
}
});
btn_pausearti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (player1.isPlaying()) {
player1.pause();
}
}
});
btn_stoparti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.stop();
}
});
}
#Override
protected void onStop() {
super.onStop();
player1.stop();
}
}

android play one sound at a time

I am stumped on this and I have referenced so many other posts. I'm not asking for anyone to complete my code but simply to point out where I'm going wrong and steer me into the right direction. I want to play an audio file when I click a image button but have it stop when another image button is clicked. The problem I'm having is if I press all the image buttons they will all play audio at the same time.
package com.application.cats.catsshapecolorapp;
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class ColorPage extends AppCompatActivity {
Context context = this;
MediaPlayer mpPurple,mpBlue,mpRed,mpGreen,mpYellow,mpPink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_page);
mpPurple = MediaPlayer.create(context, R.raw.purpleaudiotest);
ImageButton purpleB = (ImageButton) findViewById(R.id.purpleButton);
purpleB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mpPurple.isPlaying()) {
mpPurple.stop();
mpPurple.release();
mpPurple = MediaPlayer.create(context, R.raw.purpleaudiotest);
} mpPurple.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpBlue = MediaPlayer.create(context, R.raw.blueaudiotest);
ImageButton blueB = (ImageButton) findViewById(R.id.blueButton);
blueB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mpBlue.isPlaying()) {
mpBlue.stop();
mpBlue.release();
mpBlue = MediaPlayer.create(context, R.raw.blueaudiotest);
} mpBlue.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpRed = MediaPlayer.create(context, R.raw.redaudiotest);
ImageButton redB = (ImageButton) findViewById(R.id.redButton);
redB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mpRed.isPlaying()) {
mpRed.stop();
mpRed.release();
mpRed = MediaPlayer.create(context, R.raw.redaudiotest);
} mpRed.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpGreen = MediaPlayer.create(context, R.raw.greenaudiotest);
ImageButton greenB = (ImageButton) findViewById(R.id.greenButton);
greenB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mpGreen.isPlaying()) {
mpGreen.stop();
mpGreen.release();
mpGreen = MediaPlayer.create(context, R.raw.greenaudiotest);
} mpGreen.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpYellow = MediaPlayer.create(context, R.raw.yellowaudiotest);
ImageButton yellowB = (ImageButton) findViewById(R.id.yellowButton);
yellowB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mpYellow.isPlaying()) {
mpYellow.stop();
mpYellow.release();
mpYellow = MediaPlayer.create(context, R.raw.yellowaudiotest);
} mpYellow.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
mpPink = MediaPlayer.create(context, R.raw.pinkaudiotest);
ImageButton pinkB = (ImageButton) findViewById(R.id.pinkButton);
pinkB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mpPink.isPlaying()) {
mpPink.stop();
mpPink.release();
mpPink = MediaPlayer.create(context, R.raw.pinkaudiotest);
} mpPink.start();
} catch(Exception e) { e.printStackTrace(); }
}
});
}
update: now the problem is the pinkaudiotest is always the first audio file to play no matter what image button I click. I have to click a second time to hear the correct audio file.
You are creating a new MediaPlayer instance every click. Simply use a single one.
if (media.isPlaying()) {
media.stop();
media.release();
media= MediaPlayer.create(context, R.raw.purpleaudiotest);
} media.start();

Making Sound Button stop after start

I need a button that will start when pressed and stop when pressed again. Otherwise I have overlapping sounds. Can any of you assist me with the code please? Below is what I currently have and can't get the button to stop when clicked again so currently it is just playing and stops when the sound is done causing sounds to overlap. Getting one to stop when another is pressed would also be ideal but I don't have a clue how to easily incorporate this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boardone);
Button one = (Button) findViewById(R.id.button1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(BoardoneActivity.this, R.raw.mouse_laughter);
mp.start();
}
});
Button two = (Button) findViewById(R.id.button2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(BoardoneActivity.this, R.raw.evil_laugh);
mp.start();
}
});
Dont use two objects of Media Player. Bleow is the snippet of methods from my project. If any error let me know.
private MediaPlayerManager mRecordingPlayer = null;
private void initAudioPlayer(){
if(mRecordingPlayer == null){
mRecordingPlayer = new MediaPlayerManager();
}
}
private void playMusic(int position){
initAudioPlayer();
mRecordingPlayer.resetPlayer();
mRecordingPlayer.setupPlayback("your reasource");
mRecordingPlayer.startPlaying();
}
private void stopPlaying(){
initAudioPlayer();
if(mRecordingPlayer.isPlaying()){
mRecordingPlayer.pausePlaying();
}
}
#Override
public void onPause() {
clearAudioPlayer();
super.onPause();
}
private void clearAudioPlayer(){
if(mRecordingPlayer != null ){
mRecordingPlayer.resetPlayer();
}
}
#Override
public void onClick(View view) {
boolean on = ((ToggleButton) view).isChecked();
if(on){
playMusic("play music");
((ToggleButton) view).setChecked(true);
}else{
stopPlaying();
}
}
Try it with below code.
mPlayPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
mPlayPause.setText("Play");
}
} else {
if (mp != null) {
mp.start();
mPlayPause.setText("Pause");
}
}
}
});

how do i stop background music when back button is pressed?

i have 2 activities. and 2 different background music plays on both. when i press a button on the first activity it will go to this activity and the changing of music goes smoothly but i want to stop background music when back button is pressed. i tried this but it force closes. is there any other way to do this?
public class Categories extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
mp = MediaPlayer.create(Categories.this, R.raw.looney);
mp.setLooping(true);
mp.start();
Button cartoonButton = (Button) findViewById(R.id.cartoon);
cartoonButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchCartoon = new Intent(Categories.this, Cartoon.class);
startActivity(launchCartoon);
}
});
Button superButton = (Button) findViewById(R.id.hero);
superButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent launchSuperheroes = new Intent(Categories.this, SuperHeroes.class);
startActivity(launchSuperheroes);
}
});
Button singerButton = (Button) findViewById(R.id.singer);
singerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchSinger = new Intent(Categories.this, Singer.class);
startActivity(launchSinger);
}
});
Button famousButton = (Button) findViewById(R.id.famous);
famousButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchFamous = new Intent(Categories.this, Famous.class);
startActivity(launchFamous);
}
});
}
#Override
protected void onResume() {
super.onResume();
mp.start();
}
#Override
protected void onPause() {
super.onPause();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
You can try this code:
#Override
public void onBackPressed() {
if (player.isPlaying()) {
player.stop();
}
player.release();
super.onBackPressed();
}
Hope this help you :)

How to add soundButton(on&off) on a activity in android apps?

I Have created a branch activity .Now i wanted to add two button on that branch activity.
When i click on 'sound on' button then my beep sound on start and when i clicked on 'sound off' then my beep sound off. and also they hide simultaneously.
Thank's
MY Code on Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_layout);
soundBttnOn =(Button) findViewById(R.id.soundBttnOn);
soundBttnOn.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
startMediaPlayer();
}
}
);
soundBttnoff =(Button) findViewById(R.id.soundBttnOff);
soundBttnoff.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
stopMediaPlayer();
}
}
);
}
private void startMediaPlayer() {
mediaPlayer = MediaPlayer.create(SoundLayout.this,R.raw.keybutton5);
mediaPlayer.start();
}
private void stopMediaPlayer() {
if( mediaPlayer != null ) {
MediaPlayer mp = null;
mp.stop();
mp.release();
}
}
It showing no problem but it is not working too..:P..I am not able to implement sound.
You can do simple google search to find tons of samples for adding button. However for playing sound file check out the MediaPlayer class.
Button startBtn, stopBtn;
//get the reference of Button
....
final MediaPlayer mp = new MediaPlayer();
startBtn.onClickListener() {
public void onClick(View v) {
try {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
stopBtn.onClickListener() {
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
//hide buttons
stopBtn.setVisibiltiy(View.GONE);
startBtn.setVisibility(View.GONE);
}
}
};
PS: For only on and off, you don't need two buttons, you could do with one button.
EDIT:
For single button, just use the playing state of Media player for deciding about the action to take on button click.
singleBtn.onClickListener() {
public void onClick(View v) {
try {
if(mp.isPlaying()) mp.stop();
else {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};

Categories

Resources