我正在尝试实现此视频中的教程,该视频实现了在网络上流式传输文件的播放器:http ://www.youtube.com/watch?v= -Xh4zlHoARM 但似乎永远不会调用 onStartCommand() 函数. 在其他地方阅读可能是因为 onCreate() 函数没有退出或类似的东西,但不确定我能用这些信息做什么。当我运行应用程序时,活动打开,单击播放按钮后,什么也没有发生。
为了调试,我放了一个虚拟的 toast 消息,它在服务 onCreate() 中工作,但从不在 onStartCommand() 或任何随后调用的函数(如 onPrepared 等)中打印任何内容,这让我觉得问题在于 onStartCommand 不是t 被调用。
将我的代码用于活动和服务。活动和服务都提到了
活动:
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;
}
}