0

我正在寻找如何在后台运行其内容/类型:音频/AACP的流 Shoutcast 。目前,我正在使用库aacdecoder-android获取运行流,但不明白如何使用服务将流留在后台。有没有人用过这样的东西?

代码:

主要活动:

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!");
            }           
        }); 
    }
}
4

2 回答 2

0

您必须创建后台服务才能让您的音乐播放器在后台运行。

1st你必须启动服务。然后在 Service 的 onCreate() 方法中初始化你的 MultiPlayer 对象。

然后在您的 UI 活动上实现 ServiceConnection(其中有您的多人 UI)。这会将您的 UI 活动与后台服务连接起来。现在您可以控制后台服务和您的多人游戏对象,例如播放、暂停功能。请参阅用于实现 ServiceConnection。

如果您希望用户通过单击通知栏中的通知导航回 ui,您也可以设置您的serviceForground 。

希望这可以帮助

于 2013-04-20T13:33:53.843 回答
0

“使用服务” - 正确:android 服务授予您的应用程序组件额外的运行权限,即使组件不在任务堆栈顶部或即使您的应用程序退出,它们也可以持续存在。大多数情况下,除了那些关键的资源不足的情况,系统不会干扰和停止服务,所以由你来控制它。

专门针对您的情况,官方网站上有一篇关于构建具有后台播放功能的播放器的文章。然而,他们谈论 MediaPlayer,我相信同样的原则适用于任何自定义播放器。

祝你好运。

于 2012-10-07T11:42:08.737 回答