声音不会停止在Android中播放

时间:2013-11-29 09:48:26

标签: android audio video

我的应用可互换地显示视频和图像。显示图像时,音乐在后台播放,播放视频时,音乐应停止播放。问题是即使我正在调用媒体播放器的stop()函数,音乐有时也不会停止播放。任何帮助表示感谢提前。代码如下 -

public class Standee implements Runnable, OnCompletionListener {
    private Vector<File> files;
    private Vector<String> timings;
    private Handler h;
    private VideoView video;
    private ImageView image;
    private Context ctx;
    private int i;
    private MediaPlayer player;

    public MediaPlayer getMediaPlayer() {
        return player;
    }

    public Standee(Context ctx, VideoView video, ImageView image) {
        this.video = video;
        this.image = image;
        this.ctx = ctx;

        i = 0;
        h = new Handler();
        files = new Vector<File>();
        timings = new Vector<String>();
        player = MediaPlayer.create(ctx, R.raw.background);
        try {
            FileReader fr = new FileReader(ConfigLoader.BETA_PATH + "media.tmr");
            BufferedReader br = new BufferedReader(fr);
            String s;
            while ((s = br.readLine()) != null) {
                String[] data = s.split(";");
                files.add(new File(ConfigLoader.BETA_PATH + data[0]));
                timings.add(data[1]);
            }
            br.close();
            video.setOnCompletionListener(this);
            video.setMediaController(null);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        h.removeCallbacks(this);
        i++;
        if (i >= files.size())
            i = 0;
        run();

    }

    @Override
    public void run() {
        if (files.elementAt(i).getAbsolutePath().endsWith(".jpg")) {

            video.setVisibility(View.INVISIBLE);

            image.setVisibility(View.VISIBLE);
            if (video.isPlaying()) {
                video.stopPlayback();
            }
            image.bringToFront();
            player = MediaPlayer.create(ctx, R.raw.background);
            try {
                player.setLooping(true);
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            player.start();
            showPhoto();

        } else if (files.elementAt(i).getAbsolutePath().endsWith(".mp4")) {
            if (player.isPlaying()) {
                player.stop();

            }

            image.setVisibility(View.INVISIBLE);

            video.setVisibility(View.VISIBLE);
            video.bringToFront();
            playVideo();

        }

        h.postDelayed(this, Integer.parseInt(timings.elementAt(i)));
        i++;
        if (i >= files.size())
            i = 0;

    }

    public void start() {
        h.post(this);
    }

    public void playVideo() {
        Uri uri = Uri.parse(files.get(i).getAbsolutePath());
        video.setVideoURI(uri);
        video.requestFocus();
        video.start();
    }

    public void showPhoto() {
        image.setImageBitmap(BitmapFactory.decodeFile(files.elementAt(i)
                .getAbsolutePath()));
    }

    // private void fadeout(Object obj){
    // ValueAnimator videoAnim=ObjectAnimator.ofFloat(obj, "alpha", 1, 0);
    // videoAnim.setDuration(3000);
    // videoAnim.start();
    // }
    //
    // private void fadein(Object obj){
    // ValueAnimator imageAnim=ObjectAnimator.ofFloat(obj, "alpha", 0, 1);
    // imageAnim.setDuration(3000);
    // imageAnim.start();
    // }
}

0 个答案:

没有答案