程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python and C language interactive nanny level tutorial 0 basic audio playback

編輯:Python

提示:本文需要一定的c編程基礎

文章目錄

  • 前言
  • 一、安裝ANACONDA
  • 二、使用步驟
    • 1.編寫py代碼
    • 2.編寫C代碼
    • 3.配置項目
  • 三、copy到python項目文件裡
  • 四、運行
  • 五、打造完美


前言

The recent application foundpy播放musicLibrary is a bit not get,網上推薦的pygame播放MP3
Can lead to serious distortion,文檔少,Other libraries run always fail,既然python可以聯合c It would be better withc的mci.
Can also learn how to usepython調用c,Kill two birds with one stone🧑‍


一、安裝ANACONDA

ANACONDA Is a can managepython包的工具,會python的應該都知道,Use this to prevent now because package version is not the same and cannot be used.
推薦博文
安裝好後自帶ctypes庫,The library can help us andc交流🤪

二、使用步驟

1.編寫py代碼

代碼如下(示例):

from ctypes import *
music = CDLL("Project7") # 加載dll 不用加後綴名
_str = b"m.mp3"
music.playMusic(_str)
input() # Stop don't let the end

First of all in your relative path put amp3文件
In front of the string must beb修飾 bytes類型
先不要運行

2.編寫C代碼

我這裡的IDE是VS2019 Some configuration I mentioned it
新建C++Empty project and then create a file,導入代碼

點擊源文件-添加-新建項

Here to change name to.c

這裡要改成x64 非常重要

貼代碼

Copy the code below:

#include <Windows.h>
#include <mmsystem.h>
#include <stdio.h>
#pragma comment(lib,"WINMM.lib")
_declspec(dllexport) void playMusic(char* file)
{

char temp[256] = {
 0 };
sprintf(temp,"play %s",file); // After playing back again "play %s wait" | 重復播放 "play %s repeat" 
mciSendString(temp, 0, 0, 0);
printf("this c !\n");
}
說明:You can't compile now Some configuration also didn't get Tell me about the code before
This code is purec語言 後綴是.c不是.cpp
如果你想使用cpp 請在—decl前面加 extern "C"
_declspec(dllexport)Is the key to export symbols modify 常用於dll文件
char *There can receive bytes這樣傳值
mmsystem是cThe multimedia library support It can even play video

`

3.配置項目

(1)點擊項目-屬性 Open this dialog🧑‍

(2)設置配置類型 為動態庫 dll

(3)點擊高級 設置字符集為多字節 Purpose is to have a voice

(4)點擊C/C++ 把SDL檢查改為 ‘’否‘’ 然後點 “確定”


(5)開始編譯 按ctrl+B

After successful compilation open the folder
Right click here

然後進入x64-Debug 注意是x64文件夾裡面的Debug


This is compileddll

三、copy到python項目文件裡

把dll文件拷貝到python項目裡面
提示:我這裡演示的IDE是PyCharm
打開文件夾

四、運行

from ctypes import *
music = CDLL("Project7") # 加載dll 不用加後綴名
_str = b"m.mp3"
music.playMusic(_str)
input() # Stop don't let the end Music can play normal

這裡用這個music.來訪問
CDLL表示載入CLanguage the invocation of the way

運行程序
This is a voice

At the end of the terminal by a carriage return will

五、打造完美

 添加 關閉、暫停、Fast forward function
#include <Windows.h>
#include <mmsystem.h>
#include <stdio.h>
#pragma comment(lib,"WINMM.lib")
_declspec(dllexport) void playMusic(char* file)
{

char temp[256] = {
 0 };
sprintf(temp, "play %s", file); // After playing back again "play %s wait" | 重復播放 "play %s repeat" 
mciSendString(temp, 0, 0, 0);
printf("this c !\n");
}
_declspec(dllexport) void closeMusic(char* file) // 關閉
{

char temp[256] = {
 0 };
sprintf(temp, "close %s", file);
mciSendString(temp, 0, 0, 0);
}
_declspec(dllexport) void pauseMusic(char* file) // 暫停
{

char temp[256] = {
 0 };
sprintf(temp, "pause %s", file);
mciSendString(temp, 0, 0, 0);
}
_declspec(dllexport) void resumeMusic(char* file) // 繼續
{

char temp[256] = {
 0 };
sprintf(temp, "resume %s", file);
mciSendString(temp, 0, 0, 0);
}
// 快進
_declspec(dllexport) void stepMusic(char* file) // Go to the right
{

char temp[256] = {
 0 };
sprintf(temp, "step %s", file);
mciSendString(temp, 0, 0, 0);
}
_declspec(dllexport) void reverseMusic(char* file) // 回退
{

char temp[256] = {
 0 };
sprintf(temp, "step %s reverse", file);
mciSendString(temp, 0, 0, 0);
}

關注我 More updatespython/C/C++內容🧑‍


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved