覆盖onPrepared不被自动调用

时间:2014-07-13 10:02:22

标签: java android android-mediaplayer

以下是我的音乐播放器的服务类,从未调用实现的onPrepared()方法,因此我必须使用setOnPreparedListener来完成所有工作。 我想从未实现的部分执行它。

public class MusicService extends Service implements MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener {

    //media player 
    private MediaPlayer player;
    //song list
    private ArrayList<Song> songs;
    //current position
    private int songPosn;
    private String songTitle="";
    private static final int NOTIFY_ID=1;
    private final IBinder musicBind = new MusicBinder();

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

    public boolean onUnbind(Intent intent){
        player.stop();
        player.release();
        return false;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        //initialize position
        songPosn = 0;
        //create player
        player = new MediaPlayer();
    }

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

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

    //this is never being called and instead of this the one in setOnPreparedListener is called
    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        //start music 
        mp.start();

        Intent notIntent = new Intent(this, MainActivity.class);
        notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendInt = PendingIntent.getActivity(this, 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentIntent(pendInt)
        .setSmallIcon(R.drawable.play)
        .setTicker(songTitle)
        .setOngoing(true)
        .setContentText("Playing")
        .setContentText(songTitle);
        Notification not = builder.build();
        startForeground(NOTIFY_ID, not);
    }

    public void initMusicPlayer(){
        //set player properties
        player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }

    public void setSong(int songIndex){
        songPosn=songIndex;
    }

    public void setList(ArrayList<Song> theSongs){
        songs = theSongs;
    }

    public void playSong(){
        //play a cool song
        player.reset();
        //get song

        Song playSong = songs.get(songPosn);
        songTitle = playSong.getTitle();
        //get ID
        long currSong = playSong.getId();
        //set path to sdcard or squaremash server
        Uri trackUri = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, currSong);
        try {
            player.setDataSource(getApplicationContext(), trackUri);
        } catch (Exception e) {
            Log.e("Music service","Error: setting data source",e);  }
        player.prepareAsync();
        //this one is called, If I remove this, the above onPrepared method isnt called
        player.setOnPreparedListener(new OnPreparedListener() {

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

    public void go(){
        player.start();
    }
    //previous song
    public void playPrev() {
        songPosn--;
        if(songPosn<0)
            songPosn=songs.size()-1;
        playSong();
    }
    //next song
    public void playNext() {
        songPosn++;
        if(songPosn>=songs.size())
            songPosn=0;
        playSong();
    }
}

2 个答案:

答案 0 :(得分:3)

这不是Java的工作方式。

您正在扩展Service个对象。该对象的生命周期中没有任何内容触发对onPrepared()的调用。触发该呼叫的是您的player成员:

private MediaPlayer player;

要实现这一点,即player触发对onPrepared()的调用,您需要将其指向实现onPrepared()的对象。该对象是您的MusicService对象,因此您需要调用:

player.setOnPreparedListener(this);

答案 1 :(得分:1)

您没有设置正确的OnPreparedListener。

onCreate()创建播放器时,您应该在那里设置监听器:

player = new MediaPlayer();
player.setOnPreparedListener(this)

然后移除另一个,否则它将覆盖第一个。


当你实现一个接口时,它只是在类上设置一个契约:“这个类具有这些特殊的方法,随时可以调用它们”。 MediaPlayer需要知道要使用哪个 OnPreparedListenerMediaPlayer.setOnPreparedListener),否则它将无法执行任何操作。