0

您好,我正在为广播电台构建应用程序。现在我可以在应用程序中播放音乐,但是当我进入主屏幕时,音频停止了。有人对如何使用我的代码在后台继续播放音频有一些提示建议吗?

我的代码:

package com.wemait.rtvhardenberg;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.wemait.rtvhardenberg.Nieuws.GetJSONData;
import com.wemait.rtvhardenberg.helper.AlertDialogManager;
import com.wemait.rtvhardenberg.helper.BackgroundSoundService;
import com.wemait.rtvhardenberg.helper.ConnectionDetector;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class Muziek extends Activity implements OnClickListener {

    private final static String RADIO_STATION_URL = "http://stream.intronic.nl/rtvhardenberg";

    private ProgressBar playSeekBar;

    private Button buttonPlay;

    private Button buttonStopPlay;

    private MediaPlayer player;

    /**
     * Reference to the ImageView which will display the animation.
     */
    ImageView animation;

    // Connection detector
    ConnectionDetector cd;

    // Alert dialog manager
    AlertDialogManager alert = new AlertDialogManager();

    // Progress Dialog
    private ProgressDialog pDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.muziek);

        cd = new ConnectionDetector(getApplicationContext());

        // Check for internet connection
        if (!cd.isConnectingToInternet()) {
            // Internet Connection is not present
            alert.showAlertDialog(Muziek.this, "Internet Connectie Error", "Zorg voor een werkende internet connectie", false);
            // stop executing code by return
            return;
        }

        animation = (ImageView)findViewById(R.id.imageAnimation);

        animation.setBackgroundResource(R.drawable.animation);

        //Intent svc=new Intent(this, BackgroundSoundService.class);
        //startService(svc);

        initializeUIElements();

        initializeMediaPlayer();
    }

    private void initializeUIElements() {

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.INVISIBLE);


        animation.setVisibility(View.INVISIBLE);

        buttonPlay = (Button) findViewById(R.id.button_play);
        buttonPlay.setOnClickListener(this);

        buttonStopPlay = (Button) findViewById(R.id.button_stop);
        buttonStopPlay.setEnabled(false);
        buttonStopPlay.setOnClickListener(this);
    }

    public void onClick(View v) {
        if (v == buttonPlay) {
            startPlaying();
        } else if (v == buttonStopPlay) {
            stopPlaying();
        }
    }

    private void startPlaying() {
        //I tried this but didn't work thats why i've commented it away
        //new BackgroundSound().execute();
        //Intent svc=new Intent(this, BackgroundSoundService.class);
       // startService(svc);
        pDialog = new ProgressDialog(Muziek.this);
            pDialog.setMessage("Live radio laden ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show(); 

        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);


        playSeekBar.setVisibility(View.INVISIBLE); // plaatje

        AnimationDrawable frameAnimation = 
                (AnimationDrawable) animation.getBackground();
        animation.setVisibility(View.INVISIBLE);
        frameAnimation.start();

        player.prepareAsync();
        frameAnimation.start();
        player.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                player.start();
               // buttonRecord.setEnabled(true);
                //new BackgroundSound().execute();

            }
        });
    }

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

        buttonPlay.setEnabled(true);
        buttonStopPlay.setEnabled(false);
        playSeekBar.setVisibility(View.INVISIBLE);
        animation.setVisibility(View.INVISIBLE);
        //buttonRecord.setEnabled(false);
        //buttonStopRecord.setEnabled(false);
        //stopRecording();
    }


    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();
        }
        Intent svc=new Intent(this, BackgroundSoundService.class);
        startService(svc);

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                pDialog.dismiss();
                animation.setVisibility(View.VISIBLE);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }

}
4

1 回答 1

0

看起来您已经在使用Service应用程序的 for 部分,这是长时间运行的后台活动的正确方法。

为了让事情在后台运行,你应该初始化MediaPlayer内部Service而不是你的Activity. 一旦它运行,您可以Service通过向它发送额外Intent的 s 或绑定到它来与它通信(请参阅绑定服务)。

于 2012-11-06T00:40:29.780 回答