commit 845fcd80427ed9afb5135cc857e74f1d97238c9e Author: ricksteves90 Date: Wed Nov 26 21:13:52 2025 +0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..eaf91e2 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/AndroidProjectSystem.xml b/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..d58d49b --- /dev/null +++ b/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..8b3f102 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..0d46093 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..db61896 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..48052b2 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2c751a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..5bd6771 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..910ffe6 --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,49 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) +} + +android { + namespace = "com.yourname.simplemusicplayer" + compileSdk { + version = release(36) + } + + defaultConfig { + applicationId = "com.yourname.simplemusicplayer" + minSdk = 21 + targetSdk = 36 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + kotlinOptions { + jvmTarget = "11" + } +} + +dependencies { + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.appcompat) + implementation(libs.material) + implementation(libs.androidx.activity) + implementation(libs.androidx.constraintlayout) + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/yourname/simplemusicplayer/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/yourname/simplemusicplayer/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..2c713c4 --- /dev/null +++ b/app/src/androidTest/java/com/yourname/simplemusicplayer/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.yourname.simplemusicplayer + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.yourname.simplemusicplayer", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d3ce07d --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/yourname/simplemusicplayer/MainActivity.kt b/app/src/main/java/com/yourname/simplemusicplayer/MainActivity.kt new file mode 100644 index 0000000..72df2eb --- /dev/null +++ b/app/src/main/java/com/yourname/simplemusicplayer/MainActivity.kt @@ -0,0 +1,141 @@ +package com.yourname.simplemusicplayer + +import android.media.MediaPlayer +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.widget.Button +import android.widget.SeekBar +import android.widget.TextView +import android.widget.Toast + +class MainActivity : AppCompatActivity() { + + private lateinit var mediaPlayer: MediaPlayer + private lateinit var playButton: Button + private lateinit var pauseButton: Button + private lateinit var stopButton: Button + private lateinit var seekBar: SeekBar + private lateinit var songTitle: TextView + + private lateinit var runnable: Runnable + private lateinit var handler: Handler + private var delay = 1000L // 更新进度条的延迟时间(毫秒) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + // 初始化UI组件 + initViews() + + // 初始化MediaPlayer + initMediaPlayer() + + // 设置按钮点击事件 + setupButtonClickListeners() + + // 初始化Handler用于更新进度条 + handler = Handler(Looper.getMainLooper()) + } + + private fun initViews() { + playButton = findViewById(R.id.playButton) + pauseButton = findViewById(R.id.pauseButton) + stopButton = findViewById(R.id.stopButton) + seekBar = findViewById(R.id.seekBar) + songTitle = findViewById(R.id.songTitle) + } + + private fun initMediaPlayer() { + mediaPlayer = MediaPlayer() + + try { + // 尝试从raw文件夹加载音乐文件 + val resourceId = resources.getIdentifier("song", "raw", packageName) + if (resourceId != 0) { + mediaPlayer = MediaPlayer.create(this, resourceId) + songTitle.text = "当前歌曲: 示例音乐" + } else { + // 如果没有找到音乐文件,尝试从网络加载一个示例音乐 + // 注意:实际应用中需要处理网络权限和异常 + Toast.makeText(this, "使用在线示例音乐", Toast.LENGTH_SHORT).show() + mediaPlayer.setDataSource("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3") + mediaPlayer.prepareAsync() + songTitle.text = "当前歌曲: 在线示例音乐" + } + } catch (e: Exception) { + e.printStackTrace() + Toast.makeText(this, "音乐加载失败: ${e.message}", Toast.LENGTH_LONG).show() + // 创建一个空的MediaPlayer防止崩溃 + mediaPlayer = MediaPlayer() + } + + // 设置进度条最大值 + seekBar.max = mediaPlayer.duration + + // 设置SeekBar变化监听器 + seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + if (fromUser) { + mediaPlayer.seekTo(progress) + } + } + + override fun onStartTrackingTouch(seekBar: SeekBar?) { + // 停止自动更新进度条 + handler.removeCallbacks(runnable) + } + + override fun onStopTrackingTouch(seekBar: SeekBar?) { + // 重新开始自动更新进度条 + updateSeekBar() + } + }) + + // 准备更新进度条的回调 + runnable = Runnable { + updateSeekBar() + } + } + + private fun setupButtonClickListeners() { + playButton.setOnClickListener { + if (!mediaPlayer.isPlaying) { + mediaPlayer.start() + updateSeekBar() + Toast.makeText(this, "开始播放", Toast.LENGTH_SHORT).show() + } + } + + pauseButton.setOnClickListener { + if (mediaPlayer.isPlaying) { + mediaPlayer.pause() + Toast.makeText(this, "已暂停", Toast.LENGTH_SHORT).show() + } + } + + stopButton.setOnClickListener { + if (mediaPlayer.isPlaying) { + mediaPlayer.stop() + mediaPlayer.prepare() + mediaPlayer.seekTo(0) + seekBar.progress = 0 + Toast.makeText(this, "已停止", Toast.LENGTH_SHORT).show() + } + } + } + + private fun updateSeekBar() { + seekBar.progress = mediaPlayer.currentPosition + handler.postDelayed(runnable, delay) + } + + override fun onDestroy() { + super.onDestroy() + // 释放MediaPlayer资源 + mediaPlayer.release() + handler.removeCallbacks(runnable) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..55dd084 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,72 @@ + + + + + + + + + +