2015-04-21 2 views
0

Je suis en train de construire un prototype pour un projet. Je veux être en mesure de diffuser de la musique depuis mon site Web vers l'application.Diffusion audio du site Web vers l'application

Voici ce que j'ai jusqu'à présent. Essayer avec un m3u parce que je n'ai honnêtement aucune idée quoi d'autre à utiliser pour cela.

Button buttonPlay; 
Button buttonStop; 
MediaPlayer mPlayer; 
String url = "http://www.*******.com/*****/files.m3u"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    buttonPlay = (Button) findViewById(R.id.play); 
    buttonPlay.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) {     
      mPlayer = new MediaPlayer(); 
      mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      try { 
       mPlayer.setDataSource(url); 
      } catch (IllegalArgumentException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show(); 
      } catch (SecurityException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       mPlayer.prepare(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show(); 
      } 
      mPlayer.start(); 
     } 
    }); 
    buttonStop = (Button) findViewById(R.id.stop); 
    buttonStop.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(mPlayer!=null && mPlayer.isPlaying()){ 
       mPlayer.stop(); 
      } 
     } 
    }); 

Le problème est qu'il lève IOexception après préparation. Maintenant, j'ai lu quelque part que vous avez à télécharger des fichiers m3u et ensuite les lire ligne par ligne. Si c'est le cas, y a-t-il un meilleur moyen? J'essaie de faire une sorte d'application de radio à des fins de test qui va jouer des chansons au hasard, pas dans l'ordre, donc je ne veux pas coder les fichiers MP3,

Répondre

0

vous ne pouvez pas préparer un flux, car il doit télécharger de manière asynchrone. vous devrez faire ceci

Button buttonPlay; 
Button buttonStop; 
MediaPlayer mPlayer; 
String url = "http://www.*******.com/*****/files.m3u"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

buttonPlay = (Button) findViewById(R.id.play); 
buttonPlay.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) {     
     mPlayer = new MediaPlayer(); 
     mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     try { 
      mPlayer.setDataSource(url); 
     } catch (IllegalArgumentException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show(); 
     } catch (SecurityException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show(); 
     } catch (IllegalStateException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
       mPlayer.prepareAsync(); 
       mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       mPlayer.start(); 
      } 
     };); 
     } catch (IllegalStateException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show(); 
     } 
    } 
}); 
buttonStop = (Button) findViewById(R.id.stop); 
buttonStop.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(mPlayer!=null && mPlayer.isPlaying()){ 
      mPlayer.stop(); 
     } 
    } 
}); 
+0

Je ne sais pas trop comment implémenter ceci. Je n'ai jamais joué avec le mediaPlayer avant. – jham

+0

@jham j'ai mis à jour mon exemple –

+0

Je n'arrive toujours pas à le faire fonctionner. Il ne joue pas le son de test que j'ai placé dans le fichier m3u. – jham