无线电应用程序进入服务

时间:2015-11-10 06:16:37

标签: java android

您好我正在按照我发现制作应用程序播放收音机的教程,应用程序一直有效,直到您退出,因为声音停止。我已经研究过并发现它需要是一项服务,但我不知道如何将其更改为服务,这是我使用的代码。

    import android.app.Activity;
    import android.os.Bundle;

    import java.io.IOException;

    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnBufferingUpdateListener;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;

    public class myMain extends Activity implements OnClickListener {

    private ProgressBar playSeekBar;

private Button buttonPlay;

private Button buttonStopPlay;

private MediaPlayer player;

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

    initializeUIElements();

    initializeMediaPlayer();
}

private void initializeUIElements() {

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

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

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

}

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

private void startPlaying() {
    buttonStopPlay.setEnabled(true);
    buttonPlay.setEnabled(false);

    playSeekBar.setVisibility(View.VISIBLE);

    player.prepareAsync();

    player.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            player.start();
        }
    });

}

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

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);
}

private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.setDataSource("stream url");
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}

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

我尝试更改它,但我不确定如何将主要活动中的功能放入新服务然后运行它。

public class MyService extends Service implements     MediaPlayer.OnPreparedListener {
private static final String ACTION_PLAY = "com.example.action.PLAY";
MediaPlayer mMediaPlayer = null;

public int onStartCommand(Intent intent, int flags, int startId) {

    if (intent.getAction().equals(ACTION_PLAY)) {
        mMediaPlayer =  private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.setDataSource("stream url");
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
} // initialize it here
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.prepareAsync(); // prepare async to not block main        thread
    }
}

/** Called when MediaPlayer is ready */
public void onPrepared(MediaPlayer player) {
    player.start();
}

}

我尝试使用上面的代码,但我不知道如何启动操作以及它是否可行。     感谢您阅读:)

1 个答案:

答案 0 :(得分:1)

从您的主要活动中调用服务。

startService(new Intent(MusicService.ACTION_PLAY));

您的服务类。

public class MusicService extends Service {

    public static MediaPlayer mp;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        mp = new MediaPlayer();     
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {  
    String action = intent.getAction();
    if (action.equals(ACTION_PLAY))
        processPlayRequest();
    return START_STICKY;
}