MediaPlayer的SetOnCompletionListener不起作用

时间:2011-07-05 12:57:43

标签: android

使用服务在后台播放音频文件。

这是我的PreviewServices类。

package com.hungama.myplay.activity;
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.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;

public class PreviewServices extends Service {
 private static final String TAG = "PreviewServices";
 public static MediaPlayer player;
 protected DataManager dataManager = DataManager.getInstance();
 String song_uri, url;
 private IBinder myBinder;
 private boolean isplaying;

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

 public class MyBinder extends Binder {
  PreviewServices getService() {
   return PreviewServices.this;
  }
 }

 @Override
 public void onCreate() {
  Log.d(TAG, "onCreate " + song_uri);

 }

 @Override
 public void onDestroy() {
 player.stop();

 }

 public void onPause() {

  player.pause();
 }

 public double duration() {
  return player.getDuration();
 }

 public void onStart(Intent intent, int startid) {
   Bundle b = intent.getExtras();
  song_uri = b.getString("song_uri");
   Uri path = Uri.parse(song_uri);

  player = MediaPlayer.create(this, path);

  player.setLooping(false); // Set looping
   player.start();
/ *player.setOnCompletionListener(new OnCompletionListener() {

 public void onCompletion(MediaPlayer mp) {
  // TODO Auto-generated method stub
 Log.d("song is completed","song is not playing now");
 dataManager.setPreview_play(true);

 }
});
*/ 

 }
}

My PreviewServices is working fine, 

in My Activity i want to show a message when my song will complete.

m using this code for this purpose.

    PreviewServices preview=new PreviewServices();




MusicScreen.this.preview.player.setOnCompletionListener(new OnCompletionListener() {



public void onCompletion(MediaPlayer mp) {

// TODO Auto-generated method stub

        Log.d("song is complete","song complete");

                    }

                });

我不知道Y在这一行中抛出Null Pointer Exception

MusicScreen.this.preview.player.setOnCompletionListener(new OnCompletionListener()

请告诉我哪里有问题。

提前感谢。

Gaurav Gupta

1 个答案:

答案 0 :(得分:0)

如果您的代码显示正确,我相信您会得到更多回复。每行代码都需要有四个空格或一个选项卡。尝试编辑修复它。

你可以做的一件简单的事就是像这样划分界线(你为x,y和z指定适当的类):

x = MusicScreen.this
y = x.preview
z = y.player
z.setOnCompletionListener(ETC.

然后你可以看到它崩溃的地方。除此之外,我不明白你的节目,所以我无法帮助......