当前位置: 动力学知识库 > 问答 > 编程问答 >

java - OnCompletionListener is not working

问题描述:

I'm trying to get videos to play on my Android app back-to-back and only after each video's been watched in its entirety.

I've come up with the following code:

package com.davekelley;

import android.support.v7.app.ActionBarActivity;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.media.MediaPlayer;

import android.net.Uri;

import android.widget.MediaController;

import android.widget.VideoView;

public class Watch extends Activity {

MediaPlayer player;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_watch);

player = new MediaPlayer();

player.setOnCompletionListener(new OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

// TODO Auto-generated method stub

mp.stop();

mp.release();

int i = 0;

if (i < 2) {

i++;

String path="http://domain.com/videos/"+i+".mp4";

playVideo(path);

}

else i=0;

}

});

player.start();

}

private void playVideo(String filename) {

try {

player.setDataSource(filename);

player.prepare();

player.start();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.watch, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

But I can't even run it because Eclipse tells me that "The method setOnCompletionListener(MediaPlayer.OnCompletionListener) in the type MediaPlayer is not applicable for the arguments (new OnCompletionListener(){})".

What is causing this? How about the rest of my code...would it achieve what I'm trying to make? Thanks.

网友答案:

copy this lines

   String path="http://domain.com/videos/"+i+".mp4";
   playVideo(path);

after

   player = new MediaPlayer();
网友答案:

Try to play first video and set OnCompletionListener for media player which give you call back when first video completed then try to check for second video with new player instance in your case you never set data source for first video just intialize player instance and set OnCompletionListener which never called becz not set first video data source.

    private MediaPlayer player;
    private int i = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playVideo("http://domain.com/videos/"+i+".mp4\"");
    }

    public void playVideo(String path){

        try{
            player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource(path);
            player.prepareAsync();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    player.release();
                    player = null;
                    if(i<2){
                        playVideo("http://domain.com/videos/"+(++i)+".mp4");
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: