Skip to content

Commit 668989e

Browse files
authored
Merge pull request #963 from VincentWSZ/Audio
update PlayLocalFileAudio
2 parents f049a6b + 4e6ac26 commit 668989e

8 files changed

+545
-0
lines changed

Demo/API_V2/Assets/API/Media/CameraAPI/CameraAPISO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ MonoBehaviour:
2222
initialButtonText: "\u5F00\u59CB\u76D1\u542C\u5E27\u6570\u636E"
2323
extraButtonList: []
2424
initialResultList: []
25+
entryOrder: 0

Demo/API_V2/Assets/API/Media/MediaSO.asset

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ MonoBehaviour:
1717
entryList:
1818
- {fileID: 11400000, guid: 1d998af43281d4eee9e1effc8f83bddd, type: 2}
1919
- {fileID: 11400000, guid: 1055cfc5cdda7407298fa2ff9997d0c6, type: 2}
20+
- {fileID: 11400000, guid: 480646e3cd4d6a948a7538d483c1b043, type: 2}
2021
- {fileID: 11400000, guid: ad04526c3748b4e2b8498f998dfea973, type: 2}
2122
- {fileID: 11400000, guid: 38b7b2300105146ce94a785a915252de, type: 2}
2223
- {fileID: 11400000, guid: 04a7c5dbfc2464252841043ba677dcab, type: 2}

Demo/API_V2/Assets/API/Media/PlayLocalFileAudio.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Collections;
3+
using UnityEngine;
4+
using UnityEngine.Networking;
5+
using WeChatWASM;
6+
7+
public class PlayLocalFileAudio : Details
8+
{
9+
public AudioSource audioSource;
10+
public AudioClip currentAudioClip;
11+
12+
private void Awake()
13+
{
14+
audioSource = GetComponent<AudioSource>();
15+
if (audioSource == null)
16+
{
17+
audioSource = gameObject.AddComponent<AudioSource>();
18+
}
19+
}
20+
21+
private void DownloadFileAudio()
22+
{
23+
WX.DownloadFile(new DownloadFileOption()
24+
{
25+
url = "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220901/211827/CallMeTeenTop.mp3",
26+
success = (res) =>
27+
{
28+
Debug.Log("WX.DownloadFile success");
29+
if (res.statusCode == 200)
30+
{
31+
Debug.Log(res.tempFilePath);
32+
var fs = WX.GetFileSystemManager();
33+
var filePath = fs.SaveFileSync(res.tempFilePath, WX.env.USER_DATA_PATH + "/CallMeTeenTop.mp3");
34+
StartCoroutine(LoadAndPlayAudio(filePath));
35+
}
36+
},
37+
fail = (res) =>
38+
{
39+
Debug.Log("WX.DownloadFile fail");
40+
},
41+
complete = (res) =>
42+
{
43+
Debug.Log("WX.DownloadFile complete");
44+
}
45+
});
46+
}
47+
48+
IEnumerator LoadAndPlayAudio(string localFilePath)
49+
{
50+
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(new Uri(localFilePath), AudioType.MPEG))
51+
{
52+
yield return www.SendWebRequest();
53+
54+
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
55+
{
56+
Debug.LogError(www.error);
57+
}
58+
else
59+
{
60+
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
61+
62+
audioSource.clip = audioClip;
63+
audioSource.Play();
64+
}
65+
}
66+
}
67+
private void CleanupAudio()
68+
{
69+
if (audioSource != null)
70+
{
71+
audioSource.Stop();
72+
audioSource.clip = null;
73+
}
74+
75+
if (currentAudioClip != null)
76+
{
77+
Destroy(currentAudioClip);
78+
currentAudioClip = null;
79+
}
80+
}
81+
protected override void TestAPI(string[] args)
82+
{
83+
DownloadFileAudio();
84+
}
85+
86+
private void OnDestroy()
87+
{
88+
CleanupAudio();
89+
}
90+
}

Demo/API_V2/Assets/API/Media/PlayLocalFileAudio/PlayLocalFileAudio.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: fb48e4613a53bb941a20036d7c08fefb, type: 3}
13+
m_Name: PlayLocalFileAudioSO
14+
m_EditorClassIdentifier:
15+
entryScriptTypeName: PlayLocalFileAudio
16+
entryName: "\u64AD\u653E\u672C\u5730\u6587\u4EF6\u8DEF\u5F84\u97F3\u9891"
17+
entryAPI:
18+
entryDescription: "\u8BE5\u529F\u80FD\u9700\u8981\u4FEE\u6539UnityPlugin\u7248\u672C\u4E3A1.2.68\u4EE5\u4E0A"
19+
optionList: []
20+
initialButtonText: "\u64AD\u653E\u672C\u5730\u8DEF\u5F84\u7684\u97F3\u9891"
21+
extraButtonList: []
22+
initialResultList: []
23+
entryOrder: 0

Demo/API_V2/Assets/API/Media/PlayLocalFileAudio/PlayLocalFileAudioSO.asset.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)