自动化windows media play

2024-05-20 18:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中控制windows媒体播放器有什么想法吗?我在网上找到了以下代码,运行良好,但没有音频播放。am使用win7 64位计算机

# this program will play MP3, WMA, MID, WAV files via the WindowsMediaPlayer
from win32com.client import Dispatch
mp = Dispatch("WMPlayer.OCX")
#tune = mp.newMedia("./SleepAway.mp3")
tune = mp.newMedia("./plays.wav")
mp.currentPlaylist.appendItem(tune)
mp.controls.play()
raw_input("Press Enter to stop playing")
mp.controls.stop()

Tags: 代码playwindows计算机mp音频thisam
2条回答

我已经为您编写了维护良好的代码。您可以保存当前状态。 我不推荐boolean。因为如果你使用int,你可以在将来保存更多的状态,而在boolean中,你只能保存两个状态-truefalse

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
    ImageView imageView;
    TextView textView;
    Button button;


    final int STATE_HUNGRY = 1;
    final int STATE_FULL = 2;
    int currentState = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.android_cookie_image_view);
        textView = (TextView) findViewById(R.id.status_text_view);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (currentState) {
                    case STATE_FULL:
                        returnCookie();
                        break;
                    case STATE_HUNGRY:
                        eatCookie();
                        break;
                    default: // used when there is no state available
                        eatCookie();
                }
            }
        });
    }

    public void eatCookie() {
        currentState = STATE_FULL;
        imageView.setImageResource(R.drawable.after_cookie);
        textView.setText("Im so full");
    }

    public void returnCookie() {
        currentState = STATE_HUNGRY;
        imageView.setImageResource(R.drawable.before_cookie);
        textView.setText("I'm so hungry");
    }
}

您是否尝试过使用静态变量跟踪当前显示的图像?静态意味着它将在函数调用之间保持其状态。然后在每次调用函数时切换它。初始声明将只调用一次

static Boolean eaten = false;

相关问题 更多 >