实例介绍
【实例截图】
【核心代码】
xml代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="16dp"> <Button android:id="@ id/key1" android:layout_width="20dp" android:layout_height="200dp" android:layout_marginLeft="0dp" android:background="@android:color/white" android:onClick="playNote" android:text="C1" /> <Button android:id="@ id/key2" android:layout_width="20dp" android:layout_height="200dp" android:layout_marginLeft="0dp" android:background="@android:color/white" android:onClick="playNote" android:text="D1"/> <Button android:id="@ id/key3" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="E1"/> <Button android:id="@ id/key4" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="F1"/> <Button android:id="@ id/key5" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="G1"/> <Button android:id="@ id/key6" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="A1"/> <Button android:id="@ id/key7" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="B1"/> <Button android:id="@ id/key8" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="C2"/> <Button android:id="@ id/key9" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="D2"/> <Button android:id="@ id/key10" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="E2"/> <Button android:id="@ id/key11" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="F2"/> <Button android:id="@ id/key12" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="G2"/> <Button android:id="@ id/key13" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="A2"/> <Button android:id="@ id/key14" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="B2"/> <Button android:id="@ id/key15" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="C3"/> <Button android:id="@ id/key16" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="D3"/> <Button android:id="@ id/key17" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="E3"/> <Button android:id="@ id/key18" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="F3"/> <Button android:id="@ id/key19" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="G3"/> <Button android:id="@ id/key20" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="A3"/> <Button android:id="@ id/key21" android:layout_width="20dp" android:layout_height="200dp" android:layout_weight="1" android:background="@android:color/white" android:onClick="playNote" android:text="B3"/> </LinearLayout> java代码 package com.example.beepgangqinchengxu; import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioTrack; import android.os.Bundle; import android.view.View; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void playNote(View view) { int keyId = view.getId(); double frequency = 0; switch (keyId) { case R.id.key1: frequency = 261.63; // C 键频率 break; case R.id.key2: frequency = 277.18; // C# 键频率 break; case R.id.key3: frequency = 293.66; // D 键频率 break; case R.id.key4: frequency = 311.13; // D# 键频率 break; case R.id.key5: frequency = 329.63; // E 键频率 break; case R.id.key6: frequency = 349.23; // F 键频率 break; case R.id.key7: frequency = 369.99; // F# 键频率 break; case R.id.key8: frequency = 392.00; // G 键频率 break; case R.id.key9: frequency = 415.30; // G# 键频率 break; case R.id.key10: frequency = 440.00; // A 键频率 break; case R.id.key11: frequency = 466.16; // A# 键频率 break; case R.id.key12: frequency = 493.88; // B 键频率 break; case R.id.key13: frequency = 523.25; // 高音 C 键频率 break; case R.id.key14: frequency = 554.37; // 高音 C# 键频率 break; case R.id.key15: frequency = 587.33; // 高音 D 键频率 break; case R.id.key16: frequency = 622.25; // 高音 D# 键频率 break; case R.id.key17: frequency = 659.25; // 高音 E 键频率 break; case R.id.key18: frequency = 698.46; // 高音 F 键频率 break; case R.id.key19: frequency = 739.99; // 高音 F# 键频率 break; case R.id.key20: frequency = 783.99; // 高音 G 键频率 break; case R.id.key21: frequency = 830.61; // 高音 G# 键频率 break; default: Toast.makeText(this, "无效按键", Toast.LENGTH_SHORT).show(); return; } generateTone(frequency, 300); // 播放 300 毫秒的声音 } private void generateTone(double frequency, int duration) { int sampleRate = 44100; int numSamples = duration * sampleRate / 1000; short[] audioData = new short[numSamples]; for (int i = 0; i < numSamples; i ) { double angle = i / ((double) sampleRate / frequency) * 2.0 * Math.PI; audioData[i] = (short) (Math.sin(angle) * 32767.0); } AudioTrack audioTrack = new AudioTrack( AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, numSamples * 2, AudioTrack.MODE_STATIC ); audioTrack.write(audioData, 0, numSamples); audioTrack.play(); // 等待播放完成 try { Thread.sleep(duration); } catch (InterruptedException e) { e.printStackTrace(); } // 停止播放并释放资源 audioTrack.stop(); audioTrack.release(); } } AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.Beepgangqinchengxu" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
标签: android studio Android Studio and ST
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论