Any way to speed up this code. Android audio streaming

Creating an application and streaming audio from a site. I have a menu, and when I press the button to open the radio activity, it can take from 8 to 20 seconds to load and sometimes force close. Any help would be awesome.

the 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); 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://216.235.91.36/play?s=magic24point7&d=LIVE365&r=0&membername=&session=magic24point7:0&AuthType=NORMAL&app_id=live365%3ABROWSER&SaneID=24.79.96.172-13316781890137014897763&tag=live365"; mp = new MediaPlayer(); try { mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDataSource(res); mp.prepare(); mp.start(); } 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(); } } 
+4
source share
1 answer

Use prepareAsync() and setOnPreparedListener() instead of prepare() . prepare() blocks the UI thread until it returns and is not recommended for the thread. This may be the cause of your failure.

 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) { } 

http://developer.android.com/reference/android/media/MediaPlayer.html#prepare ()

Play the player for synchronous playback. After setting the data source and display surface, you need to either call prepare () or prepareAsync (). For files, it's ok to call prepare (), which blocks until MediaPlayer is ready to play.

Otherwise, I think this is your bottleneck. The fastest way to speed things up is to provide fast data exchange between the server and the client. It seems that your code is not so bad.

+4
source

Source: https://habr.com/ru/post/1494334/


All Articles