0

您好,我已经制作了一个广播应用程序,其中流媒体是从网络完成的

我的源代码是

当用户单击按钮时,将执行以下代码

      if (!NotifyService.iSserviceRunning) {
            new PlayRadio().execute("");
        } 

// 用于启动服务和创建进度条的 AYSNC 类

  class PlayRadio extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        try {
            startService(new Intent(RadioActivity.this, NotifyService.class));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try {
            PD = ProgressDialog.show(RadioActivity.this, "Tuning...", "Please Wait...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//现在服务类将是

   public class NotifyService extends Service {

private static String RADIO_STATION_URL;
public static  MediaPlayer player;
public static boolean iSserviceRunning = false;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    RADIO_STATION_URL = getResources().getString(R.string.streamurl);
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    initializeMediaPlayer();
    startPlaying();
    iSserviceRunning = true;
}

@Override
public void onDestroy() {
    super.onDestroy();
    nm.cancel(R.string.service_started);
    stopPlaying();
    initializeMediaPlayer();
    iSserviceRunning = false;
}


private void startPlaying() {
    player.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            player.start();

        }
    });
    new PrepareTask().execute();
}

private void stopPlaying() {
    if (player.isPlaying()) {
        player.pause();
        player.release();
    }
}

private void initializeMediaPlayer() {
    player = new MediaPlayer();

    try {
        player.setDataSource(RADIO_STATION_URL);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private class PrepareTask extends AsyncTask<Integer, Integer, Integer> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected void onPostExecute(Integer result) {
        if(RadioActivity.PD!=null){
            if(RadioActivity.PD.isShowing()){
                RadioActivity.PD.dismiss();             
            }
        }
    }

    @Override
    protected Integer doInBackground(Integer... arg0) {
        try {
            player.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

}

我做错了吗,因为我的客户说应用程序加载时间过长..他告诉我他有很多这样的应用程序加载速度比我的应用程序快

任何人都可以建议我在我的代码中做错了什么,所以需要很多时间吗?

4

0 回答 0