onStartCommand()函数似乎没有被调用。

时间:2013-10-29 02:18:04

标签: android service media-player android-service android-mediaplayer

我正在尝试从此视频实现教程,该视频实现了一个在Web上流式传输文件的播放器:http://www.youtube.com/watch?v=-Xh4zlHoARM但似乎onStartCommand()函数永远不会被调用。在其他地方读取它可能是因为onCreate()函数没有退出或类似的东西,但不知道我可以用这些信息做什么。当我运行应用程序时,活动打开,单击播放按钮时,没有任何操作。

为了调试,我放了一个虚拟toast消息,它在服务onCreate()中工作,但从不在onStartCommand()或随后调用的任何函数(如onPrepared等)中打印任何东西,这使我的问题是onStartCommand没有被调用。

将我的代码放入活动和服务中。提及活动和服务

的活动:

package com.example.irfansproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class StreamMusic extends Activity implements OnClickListener {

private Button buttonPlayStop;
Intent serviceIntent;
Boolean boolMusicPlaying = false;
String strAudioLink="cairnomount.mp3";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.streammusic);
    try{
        serviceIntent = new Intent(this,myPlayService.class);
        initViews();
        buttonPlayStop.setOnClickListener(this);
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    buttonPlayStop = (Button) findViewById(R.id.bStreamMusic);
}

private void initViews() {
    // TODO Auto-generated method stub
    buttonPlayStop = (Button) findViewById(R.id.bStreamMusic);
    buttonPlayStop.setBackgroundResource(R.drawable.bplay);
}

/*private void setListeners() {
    // TODO Auto-generated method stub

}*/

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    buttonPlayStopClick();
}

private void buttonPlayStopClick() {
    // TODO Auto-generated method stub
    if (!boolMusicPlaying) {
        buttonPlayStop.setBackgroundResource(R.drawable.bpause);
        playAudio();
        boolMusicPlaying = true;
    } else {
        buttonPlayStop.setBackgroundResource(R.drawable.bplay);
        stopAudio();
        boolMusicPlaying = false;
    }
}

private void stopAudio() {
    // TODO Auto-generated method stub
    try{
        stopService(serviceIntent);
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(), Toast.LENGTH_SHORT).show();

    }   

}

private void playAudio() {
    // TODO Auto-generated method stub
    serviceIntent.putExtra("sentAudioLink", strAudioLink);
    try{
        startService(serviceIntent);


    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(), Toast.LENGTH_SHORT).show();

    }
}

}

服务:

package com.example.irfansproject;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.IBinder;
import android.widget.Toast;

public class myPlayService extends Service implements OnCompletionListener,
    OnPreparedListener, OnErrorListener, OnSeekCompleteListener, OnInfoListener,    OnBufferingUpdateListener {

private MediaPlayer mediaPlayer = new MediaPlayer();
private String sntAudioLink;

@Override
public void onCreate() {

    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnSeekCompleteListener(this);
    mediaPlayer.setOnInfoListener(this);
    mediaPlayer.reset();
    Toast.makeText(this,"poop ",Toast.LENGTH_SHORT).show(); 

}

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


    sntAudioLink = intent.getExtras().getString("sentAudioLink");
    mediaPlayer.reset();
    if (!mediaPlayer.isPlaying()) {
        try {

            mediaPlayer.setDataSource("http://www.tonycuffe.com/mp3/"+sntAudioLink);

            // prepare media player
            mediaPlayer.prepareAsync();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Start Stick return means service will explicitly continue until user
    // ends it
    return START_STICKY;

}

public void onDestroy() {
    super.onDestroy();
    if (mediaPlayer != null) {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }

}

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

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
    // TODO Auto-generated method stub

}

@Override
public void onSeekComplete(MediaPlayer mp) {
    // TODO Auto-generated method stub

}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub
    switch (what) {
    case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
        Toast.makeText(this,
                "Media Error: Not valid for Progressive Playback " + extra,
                Toast.LENGTH_SHORT).show();
    case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
        Toast.makeText(this, "Media Error: Server Died!! " + extra,
                Toast.LENGTH_SHORT).show();
    case MediaPlayer.MEDIA_ERROR_UNKNOWN:
        Toast.makeText(this, "Media Error: Unknown " + extra,
                Toast.LENGTH_SHORT).show();

    }

    return false;
}

@Override
public void onPrepared(MediaPlayer mp) {
    // TODO Auto-generated method stub
    playMedia();

}

private void playMedia() {
    // TODO Auto-generated method stub
    if (!mediaPlayer.isPlaying()) {
        mediaPlayer.start();
    }
}

private void stopMedia() {
    // TODO Auto-generated method stub
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
}

@Override
public void onCompletion(MediaPlayer mp) {
    // TODO Auto-generated method stub
    stopMedia();
    stopSelf();

}

@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub
    return false;
}

}

1 个答案:

答案 0 :(得分:0)

你拼错了“开始”。

  
    

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

  

您应该能够添加@Override注释而不会出错。