2011-07-05 56 views
0

使用服務在後臺播放音頻文件。MediaPlayer的SetOnCompletionListener不工作

這是我的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"); 

        } 

       }); 

我不知道Ÿ它拋出空指針異常此行

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

請建議我在哪裏的問題。

在此先感謝。

拉夫古普塔

回答

0

如果您的代碼顯示正確,我相信您會得到更多回復。每行代碼在開始時都需要四個空格或一個製表符。嘗試編輯修復它。

,你可以做一個簡單的事情是打破了類似的行(你的X,Y和Z指定適當的類):

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

然後,你可以看到它打破了。除此之外,我不明白你的節目,所以我不能幫助...