2012-09-26 48 views
0

我正在尋找如何在後臺運行流Shoutcast的內容/類型:audio/AACP。 目前,我正在使用庫aacdecoder-android獲取運行流,但不知道如何使用服務將流保留在後臺。 有沒有人使用過類似的東西?如何在Android上播放流式aacp運行背景

代碼:

MainActivity:

public class MainActivity extends Activity { 
    public AACPlayer mp; 
    private Toast myToast; 
    private Handler mHandler; 
    private ImageParser mImageParser; 

    private WebView publicidadecapa; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     menu.add("Sair"); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getTitle().toString() == "Sair") { 
      finish(); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.maintab);  

     mp = new AACPlayer(new RadioCallBack(this));     
     ProgressBar loading = (ProgressBar) findViewById(R.id.loadingAudio); 
     loading.setVisibility(View.INVISIBLE); 
     ImageView playButton = (ImageView) findViewById(R.id.playButton); 
     playButton.setTag("1"); 
     playButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       ImageView playButton = (ImageView) arg0; 
       ConnectivityManager cm = 
         (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

       if (cm.getActiveNetworkInfo() == null || !cm.getActiveNetworkInfo().isConnectedOrConnecting()) { 
        if (myToast == null) { 
         myToast = Toast.makeText(getBaseContext(), "Verifique a conexão com a internet", 5000); 
        } 
        myToast.setDuration(5000); 
        myToast.show(); 
        return; 
       } 

       if (playButton.getTag() == "1" && cm.getActiveNetworkInfo().isConnectedOrConnecting()) { 

        playButton.setImageResource(R.drawable.btn_menustop); 
        playButton.setTag("0"); 
        playButton.setVisibility(View.INVISIBLE); 
        ProgressBar loading = (ProgressBar) findViewById(R.id.loadingAudio); 
        loading.setVisibility(View.VISIBLE); 
        ((TextView)findViewById(R.id.textView1)).setText("Conectando..."); 


        mp.playAsync("http://voxsc1.somafm.com:9002/"); 

       } else if (playButton.getTag() == "0") {     

        playButton.setVisibility(View.INVISIBLE); 
        ProgressBar loading = (ProgressBar) findViewById(R.id.loadingAudio); 
        loading.setVisibility(View.VISIBLE); 
        playButton.setImageResource(R.drawable.btn_menuplay); 
        playButton.setTag("1"); 

        mp.stop(); 
       }            

      }   
     });   

     final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);   

     SeekBar volumeControl = (SeekBar) findViewById(R.id.volumeControl); 
     volumeControl.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); 
     volumeControl.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)); 
     volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 

      public void onStopTrackingTouch(SeekBar arg0) { 


      } 

      public void onStartTrackingTouch(SeekBar arg0) { 


      } 

      public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { 
       audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1,0); 
       if(arg1 == 0) { 
        ((ImageView) findViewById(R.id.imageView2)).setImageResource(R.drawable.ic_volume2); 
       } else { 
        ((ImageView) findViewById(R.id.imageView2)).setImageResource(R.drawable.ic_volume1); 
       } 
      } 
     });   
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

    } 
    @Override 
    protected void onDestroy() { 
     if (this.isFinishing()) { 
      //mp.stop(); 
     } 
     super.onDestroy(); 
    } 

    public void onStart() 
    { 
     super.onStart(); 

     // your code 
    } 


    public Object fetch(String address) throws MalformedURLException,IOException { 
     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    } 


    private Boolean isOnline() { 
     ConnectivityManager cm = 
       (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo ni = cm.getActiveNetworkInfo(); 
     if(ni != null && ni.isConnected()) 
      return true; 

     return false; 
    } 

    /* Abrir em uma nova aba o link da publicidade */ 
    public class MyWebClient extends WebViewClient{ 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      //Uri uri = Uri.parse(url); 
      Context context = view.getContext(); 
      Intent intent = new Intent(context,OpenSiteWebView.class); 

      Bundle parametros = new Bundle(); 
      parametros.putString("url", url); 

      intent.putExtras(parametros); 

      startActivity(intent); 
      return true; 
     } 
    } 
} 

RadioCallBack:

public class RadioCallBack implements PlayerCallback { 

    private MainActivity activity; 

    public RadioCallBack(MainActivity activity) { 
     super(); 
     this.activity = activity; 
    } 

    public void playerException(Throwable arg0) { 
     activity.runOnUiThread(new Runnable() {      
      public void run() { 
       Toast.makeText(activity, "O stream pode estar offline! Tente novamente mais tarde", 50000).show(); 

       ProgressBar loading = (ProgressBar) activity.findViewById(R.id.loadingAudio); 
       loading.setVisibility(View.INVISIBLE); 
       ImageView playButton = (ImageView) activity.findViewById(R.id.playButton); 
       playButton.setVisibility(View.VISIBLE); 
       playButton.setImageResource(R.drawable.btn_menuplay); 
       playButton.setTag("1"); 
       ((TextView)activity.findViewById(R.id.textView1)).setText("Pressione PLAY para tocar!"); 
      } 
     }); 
    } 

    public void playerPCMFeedBuffer(boolean arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 

    } 

    public void playerStarted() { 
     activity.runOnUiThread(new Runnable() {    
      public void run() { 
       ProgressBar loading = (ProgressBar) activity.findViewById(R.id.loadingAudio); 
       loading.setVisibility(View.INVISIBLE); 
       ImageView playButton = (ImageView) activity.findViewById(R.id.playButton); 
       playButton.setVisibility(View.VISIBLE); 
       ((TextView)activity.findViewById(R.id.textView1)).setText("Mais música brasileira"); 
      }   
     });  
    } 

    public void playerStopped(int arg0) { 
     activity.runOnUiThread(new Runnable() {    
      public void run() { 
       activity.mp = null; 
       activity.mp = new AACPlayer(new RadioCallBack(activity)); 
       ProgressBar loading = (ProgressBar) activity.findViewById(R.id.loadingAudio); 
       loading.setVisibility(View.INVISIBLE); 
       ImageView playButton = (ImageView) activity.findViewById(R.id.playButton); 
       playButton.setVisibility(View.VISIBLE); 
       ((TextView)activity.findViewById(R.id.textView1)).setText("Pressione PLAY para tocar!"); 
      }   
     }); 
    } 
} 
+0

你能否把XML文件放到你的代碼中。我需要儘快播放AACP流媒體。請幫我解決這個問題。 –

+0

我想播放這個流:「http://stream2.servercristianonetwork.com:8072/"...請幫助我的那個... –

+0

@DennySharma對不起,爲已故的評論,但如果你仍然期待對於答案:在您的網站上,您可以下載m3u文件。這些很容易發揮,關於如何做到這一點,在stackoverflow上有很多問題。 – user2988879

回答

0

「使用服務」 - 右:Android服務與附加的運行授予您的應用程序組件即使組件不在組件上時也能讓它們持續存在的特權操作任務堆棧或甚至當您的應用程序退出時。大多數情況下,除了那些缺乏資源的嚴重情況外,系統不會干擾和停止服務,因此如何控制服務取決於您。

專門針對您的情況,在官方網站上有an article關於具有背景回放功能的建築玩家。然而他們談論MediaPlayer,我相信,對於任何定製玩家來說,這些原則都是相同的。

祝你好運。

0

爲了讓您的音樂播放器在後臺運行,您必須創建背景服務。

1您必須開始服務。然後在Service的onCreate()方法中初始化您的MultiPlayer對象。

然後在您的UI活動(其中包含您的多人UI)上實施ServiceConnection。這會將您的UI活動與後臺服務相關聯。現在您可以控制背景服務和多人對象的例如播放,暫停功能。請參閱this用於實施ServiceConnection。

如果您希望用戶通過單擊通知欄中的通知導航回到用戶界面,也可以創建serviceForground

希望這會有幫助

相關問題