0

我编写了一个流式传输 url 的服务。有一个 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 活动开始时,我如何使用此服务运行流?

4

0 回答 0