简体   繁体   English

播放完媒体文件时的Android通知

[英]Android notification when media file is done playing

I have a mediaplayer class with all the methods in there. 我有一个带有所有方法的mediaplayer类。 I am just trying to get an indication of when a file is done playing, but I'm having trouble. 我只是想获得何时播放文件的指示,但是遇到了麻烦。 Here is the method: 方法如下:

public boolean isPlaying()
{
    final boolean still_playing = true;
    mediaplayer.setOnCompletionListener(new OnCompletionListener() 
    {
        @Override
        public void onCompletion(MediaPlayer mp) 
        {
            still_playing = false;
        }
    });
    return still_playing;
}

I'm getting errors like the final local variable cannot be assigned, since it is defined in an enclosing type or when I try something different I get variabledeclaratorIdexpected Cheers 我遇到错误,例如the final local variable cannot be assigned, since it is defined in an enclosing type或者当我尝试其他操作时,我得到了variabledeclaratorIdexpected

Your final boolean still_playing = true; 您的final boolean still_playing = true; is a constant variable which means it can't be changed during runtime. 是一个常量变量,表示在运行时无法更改。

Later in the program: 在程序的后面:

@Override
public void onCompletion(MediaPlayer mp) 
{
    still_playing = false;
}

You're trying to change it's value. 您正在尝试更改其价值。

To fix this you should define boolean still_playing = true; 为了解决这个问题,您应该定义boolean still_playing = true; as a member variable of the class like sow: 作为sow这样的类的成员变量:

public class classExample {
    // member variables
    private boolean still_playing = true;

    public boolean isPlaying() {

        /* I DELETED THE VARIABLE AND DECLARED IT OUTSIDE THE METHOD */

        mediaplayer.setOnCompletionListener(new OnCompletionListener() {
             @Override
             public void onCompletion(MediaPlayer mp) {

                  /* You could declare it here too without the private
                     keyword like this:
                             boolean still_playing = true; */

                  still_playing = false;
             }
        });
        return still_playing;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM