android无线电流媒体背景和进度对话框

时间:2013-03-16 11:09:19

标签: android audio-streaming progressdialog

我正在android中开发一个Radio应用程序,它将从url和后台流中显示进度对话框加载流但显示ANR。我正在使用媒体播放器的服务来启动和停止。需要帮助。我使用过asynctask但无法获得解决方案。 MainActivity.class

button_Play.setOnClickListener(new View.OnClickListener() 
{               
@Override
public void onClick(View v) 
{
    new DownloadTask().execute();
}
});


class DownloadTask extends AsyncTask<Void, Void, Void> 
{
@Override
protected void onPreExecute()
{
    progressDialog = ProgressDialog.show(MainActivity.this , "Android Streaming
Player" , "Connecting to Stream..." , true , false);

Log.d("onPreExecute()----->" , "MainActivity.class");

super.onPreExecute();

}

@Override
protected Void doInBackground(Void... params)
{

Log.d("doInBackground()----->" , "MainActivity.class");

startService(new Intent(MainActivity.this , RadioService.class));

return null;

}

@Override
protected void onPostExecute(Void result) 
{

Log.d("onPostExecute()----->" , "MainActivity.class");

progressDialog.dismiss();

super.onPostExecute(result);

}


}    


Here is service class:

RadioService.class

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
Log.d("onStartCommand------->", "RadioService.class");

try 
{
Log.d("MediaPlayer--->TRY BLOCK", "MediaPlayer");

URL url2 = new URL("http://141.139.35.23:8000");

URLConnection urlConnection = url2.openConnection();

player.setDataSource(urlConnection.getURL().toString());

player.setAudioStreamType(AudioManager.STREAM_MUSIC);

player.prepare();

Log.d("MediaPlayer--->TRY BLOCK AFTER ASYNC", "MediaPlayer");

player.setOnPreparedListener(new OnPreparedListener()
{

@Override
public void onPrepared(MediaPlayer mp) 
{

Log.d("MediaPlayer--->ON PREPARED BEGINS", "MediaPlayer");

//mp.setOnErrorListener(MainActivity.this);

mp.start();

//mp.setOnErrorListener(this);

mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

                    Toast.makeText(getApplicationContext() , "Streaming started " , Toast.LENGTH_SHORT).show();

                    Log.d("MediaPlayer--->ON PREPARED ENDS", "MediaPlayer");

                }
            });
        } catch (Exception e) 
        {
            Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
        } 



        return START_STICKY;
        //return super.onStartCommand(intent, flags, startId);
}

1 个答案:

答案 0 :(得分:0)

class Loading extends AsyncTask<Object, Object, Object>{
    protected void onPreExecute(){
        loadDialog.show();
    }
    @Override
    protected Object doInBackground(Object... arg0) {
        if(rFlag==false){
            mediaPlayer = new MediaPlayer();
            try{
                url = "rtsp://ra.dio";
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (Exception e) {
                loadDialog.dismiss();
                errmsg = "error";
                // TODO: handle exception
            }           
            while(rFlag==false){

                mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

                    public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
                        // TODO Auto-generated method stub
                        if(arg1 >= 98){
                            loadDialog.dismiss();
                            rFlag=true;
                        }
                    }
                });

            }
        }
        else if(rFlag==true){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
            rFlag=false;
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        // loaded
    }

}