The right way to use MediaPlayer in an Android app

sorry my english but i'm from brazil and i used google translator.

Well, I'm afraid in this application, where I am trying to make an online streaming radio, works fine in version 2.2, but version 4.0 does not work. The error does not occur, it just does not work. Below is my code.

I appreciate any help.

package com.radiomiriam; import java.io.IOException; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Toast; public class Main extends Activity { public String url = "http://69.64.48.96:9880"; public MediaPlayer player = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void stop(){ Toast.makeText(getApplicationContext(), "Finalizando...", Toast.LENGTH_SHORT).show(); if(player.isPlaying()){ player.stop(); } } public void play(){ if(player.isPlaying()){ Toast.makeText(getApplicationContext(), "Já está em execução!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Preparando...", Toast.LENGTH_SHORT).show(); player.setAudioStreamType(AudioManager.STREAM_MUSIC); player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); try { player.setDataSource(url); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { player.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } player.start(); } } public void onClick(View v) { final int id = v.getId(); switch (id) { case R.id.btnPlay: play(); break; case R.id.btnStop: stop(); break; } } } 

Hello, sorry for my perseverance, but I still have not found a solution to my problem and do not know how to solve it, I again ask you to help solve this problem.

The application works in versions 2.x and 3.x, but not in version 4.x. Below is a link to download the project, which you can take a look at and help me.

http://nsi.inf.br/RadioMiriam.rar (deleted)

+6
source share
4 answers

I downloaded your project and actually returns an error Code: E / MediaPlayer (1976): error (-38, 0)

This is caused by the launch of the media planner before its preparation.

I made a few changes and it started working. You must:

  • Install the onPrepared listener on MediaPlayer (i.e. in onCreate)

     player.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }); 
  • Remove player.start () from play () method

  • Set the data source this way (in the play () method)

     player.setDataSource(this, Uri.parse("http://69.64.48.96:9880/")); 

It works on my HTC Sensation with Android 4.0.3

If you need any help, please let me know.

EDIT: I forgot, I also deleted

 player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); 

It's redundant

+7
source

The prepare() call probably blocks the UI thread (which android 4.0 hates it when you do it)

You will need this work in a background thread such as asynthesis. Here is an anonymous version of the AsyncTask implementation to run play() in the background.

 public void onClick(View v) { final int id = v.getId(); switch (id) { case R.id.btnPlay: new AsyncTask<Void, Void, Void>(){ @Override protected Void doInBackground(Void... voids) { play(); return null; } }.execute(); break; case R.id.btnStop: stop(); break; } } 

You need to read a little about the MediaPlayer states (play, prepare, stop, etc.) to find out why the call blocks the UI during preparation (

for more information on AsyncTasks

+2
source

You have not established any connection between the buttons in your XML file and your code. The onClick method is never called, even if you click the buttons.

Replace the onCreate method as follows:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button playButton = (Button) findViewById(R.id.btnPlay); playButton.setOnClickListener(this); Button stopButton = (Button) findViewById(R.id.btnStop); stopButton.setOnClickListener(this); play(); } 
0
source

In Android, it is always better to do any IO work in the background thread.

And that includes MediaPlayer. but in the case of MediaPlayer, you already have the prepareAsync() method to prepare MediaPlayer in the background for you.

A good way to do this:

 package com.radiomiriam; import java.io.IOException; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Toast; public class Main extends Activity implements OnPreparedListener{ public String url = "http://69.64.48.96:9880"; public MediaPlayer player = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void stop(){ Toast.makeText(getApplicationContext(), "Finalizando...", Toast.LENGTH_SHORT).show(); if(player.isPlaying()){ player.stop(); } } public void play(){ if(player.isPlaying()){ Toast.makeText(getApplicationContext(), "J? est? em execuç?o!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Preparando...", Toast.LENGTH_SHORT).show(); player.setAudioStreamType(AudioManager.STREAM_MUSIC); player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); try { player.setDataSource(url); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { player.setOnPreparedListener(this); player.prepareAsync(); } catch (IllegalStateException e) { e.printStackTrace(); } } } @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub mp.start(); } public void onClick(View v) { final int id = v.getId(); switch (id) { case R.id.btnPlay: play(); break; case R.id.btnStop: stop(); break; } } } 

It is also always recommended that you read the documentation, so here is the link to the MediaPlayer API: MediaPlayer

0
source

All Articles