服务没有开始

时间:2013-07-31 14:56:53

标签: android service stream background-process

我写了一个流式传输网址的服务。有一个Twitter按钮只能打开浏览器的链接。当我点击这个按钮但是我在按钮事件中启动服务时,它没有启动。我的服务代码在这里;

public class MyMediaPlayerService extends Service implements
    OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
    OnVideoSizeChangedListener, SurfaceHolder.Callback {

private String path = "http://someUrl.m3u8";
private MediaPlayer mMediaPlayer;
private static final String MEDIA = "media";;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;
private int mVideoWidth;
private int mVideoHeight;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    mMediaPlayer = new MediaPlayer(this);
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
}

public void onCompletion(MediaPlayer arg0) {

}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

    if (width == 0 || height == 0) {

        return;
    }
    mIsVideoSizeKnown = true;
    mVideoWidth = width;
    mVideoHeight = height;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void onPrepared(MediaPlayer mediaplayer) {
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
}

public void surfaceCreated(SurfaceHolder holder) {
}

public void onPause() {
    releaseMediaPlayer();
    doCleanUp();
}

public void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
    doCleanUp();
}

private void releaseMediaPlayer() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

private void doCleanUp() {
    mVideoWidth = 0;
    mVideoHeight = 0;
    mIsVideoReadyToBePlayed = false;
    mIsVideoSizeKnown = false;
}

private void startVideoPlayback() {
    mMediaPlayer.start();
}

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

    doCleanUp();
    try {
        mMediaPlayer.setDataSource(path);
        mMediaPlayer.prepare();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.getMetadata();
        mMediaPlayer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return START_NOT_STICKY;
}
}

我也在这样的按钮事件中启动了服务;

public void onClick(View v) {
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, 
                    Uri.parse("https://twitter.com/standart_fm"));
            Intent svc = new Intent(MainActivity.this,
                    MyMediaPlayerService.class);
            //startService(new Intent(MainActivity.this, MyService.class));
            startService(svc);
            startActivity(browserIntent);
        }

日志猫是;

07-31 14:44:15.392: D/Vitamio[Player](562): THREAD AUDIO_OUTPUT END
07-31 14:44:15.419: D/Vitamio[Player](562): THREAD AUDIO_DECODE END
07-31 14:44:15.440: I/Vitamio[Player](562): CLOSE STREAM AUDIO END
07-31 14:44:15.440: I/Vitamio[Player](562): CLOSE AVFORMAT BEGIN
07-31 14:44:15.450: I/Vitamio[Player](562): CLOSE AVFORMAT END
07-31 14:44:15.450: I/Vitamio[Player](562): FREE VIDEOPIC BEGIN
07-31 14:44:15.450: I/Vitamio[Player](562): FREE VIDEOPIC 2 END
07-31 14:44:15.450: I/Vitamio[Player](562): FREE SUBPIC BEGIN
07-31 14:44:15.450: I/Vitamio[Player](562): FREE SUBPIC 1 END
07-31 14:44:15.450: I/Vitamio[Player](562): FREE AUDIOFRAME BEGIN
07-31 14:44:15.450: I/Vitamio[Player](562): FREE AUDIOFRAME 8 END
07-31 14:44:15.450: I/Vitamio[Player](562): DELETE GLOBAL REF BEGIN
07-31 14:44:15.450: I/Vitamio[Player](562): DELETE GLOBAL REF END
07-31 14:44:15.450: I/Vitamio[Player](562): FILE CLOSE END
07-31 14:44:15.450: D/Vitamio[Player](562): THREAD NOTIFY END
07-31 14:44:15.460: I/Vitamio[Player](562): VPLAYER RELEASE END

因此。如何在Twitter活动开始时使用此服务运行流程?

0 个答案:

没有答案