packagecom.music.app;/* 手机编程王APP & AIDE编译器联合出品 官方微信2133688724 微信公众号:手机编程APP 官网:www.shoujibiancheng.com */importandroidx.appcompat.app.AppCompatActivity;importandroidx.core.app.ActivityCompat;importandroidx.core.content.ContextCompat;importandroid.Manifest;importandroid.content.Intent;importandroid.content.pm.PackageManager;importandroid.media.MediaPlayer;importandroid.net.Uri;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ArrayAdapter;importandroid.widget.Button;importandroid.widget.ListView;importandroid.widget.Toast;importjava.io.IOException;importjava.util.ArrayList;publicclassMainActivityextendsAppCompatActivity{privatestaticfinalintREQUEST_READ_STORAGE=1;privatestaticfinalintREQUEST_OPEN_FILE=2;privateListViewfileListView;privateButtonopenButton,playButton,pauseButton,stopButton,resumeButton;privateArrayList<String>audioFiles=newArrayList<>();privateArrayAdapter<String>adapter;privateMediaPlayermediaPlayer;privateintcurrentIndex=-1;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fileListView=findViewById(R.id.file_list);openButton=findViewById(R.id.open_button);playButton=findViewById(R.id.play_button);pauseButton=findViewById(R.id.pause_button);stopButton=findViewById(R.id.stop_button);resumeButton=findViewById(R.id.resume_button);adapter=newArrayAdapter<>(this,android.R.layout.simple_list_item_1,audioFiles);fileListView.setAdapter(adapter);checkStoragePermission();openButton.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED){openFileSelector();}else{Toast.makeText(MainActivity.this,"需要存储权限才能选择文件",Toast.LENGTH_SHORT).show();}}});fileListView.setOnItemClickListener(newAdapterView.OnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,longid){currentIndex=position;playMusic(audioFiles.get(position));}});playButton.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){if(currentIndex!=-1){if(mediaPlayer==null){playMusic(audioFiles.get(currentIndex));}elseif(!mediaPlayer.isPlaying()){mediaPlayer.start();}}else{Toast.makeText(MainActivity.this,"请先选择一个音频文件",Toast.LENGTH_SHORT).show();}}});pauseButton.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){if(mediaPlayer!=null&&mediaPlayer.isPlaying()){mediaPlayer.pause();}}});stopButton.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){if(mediaPlayer!=null){mediaPlayer.stop();mediaPlayer.release();mediaPlayer=null;}}});resumeButton.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){if(mediaPlayer!=null&&!mediaPlayer.isPlaying()){mediaPlayer.start();}}});}privatevoidcheckStoragePermission(){if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){ActivityCompat.requestPermissions(this,newString[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_READ_STORAGE);}}@OverridepublicvoidonRequestPermissionsResult(intrequestCode,String[]permissions,int[]grantResults){super.onRequestPermissionsResult(requestCode,permissions,grantResults);if(requestCode==REQUEST_READ_STORAGE){if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){Log.d("MainActivity","Storage permission granted");}else{Toast.makeText(this,"需要存储权限才能读取音频文件",Toast.LENGTH_SHORT).show();}}}privatevoidopenFileSelector(){Intentintent=newIntent(Intent.ACTION_OPEN_DOCUMENT);intent.addCategory(Intent.CATEGORY_OPENABLE);intent.setType("audio/*");startActivityForResult(intent,REQUEST_OPEN_FILE);}@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==REQUEST_OPEN_FILE&&resultCode==RESULT_OK){if(data!=null){Uriuri=data.getData();if(uri!=null){StringfilePath=uri.toString();audioFiles.add(filePath);adapter.notifyDataSetChanged();}}}}privatebooleanisAudioFile(StringfileName){StringlowerCaseFileName=fileName.toLowerCase();returnlowerCaseFileName.endsWith(".mp3")||lowerCaseFileName.endsWith(".wav")||lowerCaseFileName.endsWith(".aac")||lowerCaseFileName.endsWith(".ogg");}privatevoidplayMusic(StringfilePath){if(mediaPlayer!=null){mediaPlayer.stop();mediaPlayer.release();}mediaPlayer=newMediaPlayer();try{mediaPlayer.setDataSource(this,Uri.parse(filePath));mediaPlayer.prepare();mediaPlayer.start();}catch(IOExceptione){e.printStackTrace();Toast.makeText(this,"播放失败: "+e.getMessage(),Toast.LENGTH_SHORT).show();}}@OverrideprotectedvoidonDestroy(){super.onDestroy();if(mediaPlayer!=null){mediaPlayer.release();mediaPlayer=null;}}}main.xml代码<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"><Buttonandroid:id="@+id/open_button"android:layout_width="200dp"android:layout_height="50dp"android:text="打开音频文件"android:layout_gravity="center_horizontal"android:layout_marginTop="20dp"/><ListViewandroid:id="@+id/file_list"android:layout_width="match_parent"android:layout_height="200dp"android:layout_marginTop="16dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"android:layout_marginTop="16dp"><Buttonandroid:id="@+id/play_button"android:layout_width="80dp"android:layout_height="40dp"android:text="播放"/><Buttonandroid:id="@+id/pause_button"android:layout_width="80dp"android:layout_height="40dp"android:text="暂停"/><Buttonandroid:id="@+id/stop_button"android:layout_width="80dp"android:layout_height="40dp"android:text="停止"/><Buttonandroid:id="@+id/resume_button"android:layout_width="80dp"android:layout_height="40dp"android:text="继续"/></LinearLayout></LinearLayout>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">添加这句权限语句即可。<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>手机端AIDE安卓音乐播放器软件代码
张小明
前端开发工程师
你是项目经理,还是项目领导者?
上周和几个同行吃饭,聊起一个现象:为什么有些项目经理能把跨部门团队拧成一股绳,项目再难也能推动下去;而有些人虽然计划做得漂亮,却总在协调和救火中疲于奔命,团队怨声载道?这让我意识到&#…
瑜伽冥想引导词:LobeChat营造放松氛围
LobeChat:为冥想与心灵疗愈注入温度的AI交互引擎 在快节奏的现代生活中,越来越多的人开始寻求内心的平静。清晨五点,有人戴上耳机,在柔和语音的引导下缓缓睁开双眼;深夜入睡前,也有人依靠一段温柔的呼吸练习…
解决微软输入法无法添加多个动态自定义短语的问题
我们可以在微软输入法中的 设置 > 词库和自学习 > 用户自定义短语 > 添加或编辑自定义短语 的设置中,去管理自定义短语,并使用特殊占位符去设置为动态的短语。 比如,我们希望输入 riqi 的时候,能够打出形如 2025年12月1…
一键部署LobeChat镜像,开启高效AI交互新时代
一键部署LobeChat镜像,开启高效AI交互新时代 在企业智能化转型加速的今天,越来越多团队开始尝试引入大语言模型来提升工作效率。然而现实往往并不理想:API 调用混乱、界面体验割裂、数据安全堪忧——开发者疲于对接各种 SDK,非技…
Flink SQL INSERT 语句单表写入、多表分流、分区覆盖与 StatementSet
1. INSERT 语句是干嘛的 INSERT 用于把查询结果或字面量数据写入目标表(sink 表)。在 Flink 里,执行 INSERT 会提交一个 Flink Job(流式作业通常是长期运行)。2. Java 里怎么跑 INSERT:单条 executeSql vs …
Swagger2Word完全指南:快速将API文档转换为专业Word格式
Swagger2Word完全指南:快速将API文档转换为专业Word格式 【免费下载链接】swagger2word 项目地址: https://gitcode.com/gh_mirrors/swa/swagger2word Swagger2Word是一个功能强大的开源工具,专门用于将Swagger和OpenAPI接口文档转换为格式规范的…