news 2026/6/15 2:36:39

手机端AIDE安卓音乐播放器软件代码

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
手机端AIDE安卓音乐播放器软件代码

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"/>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/13 11:51:12

你是项目经理,还是项目领导者?

上周和几个同行吃饭&#xff0c;聊起一个现象&#xff1a;为什么有些项目经理能把跨部门团队拧成一股绳&#xff0c;项目再难也能推动下去&#xff1b;而有些人虽然计划做得漂亮&#xff0c;却总在协调和救火中疲于奔命&#xff0c;团队怨声载道&#xff1f;这让我意识到&#…

作者头像 李华
网站建设 2026/6/15 13:15:58

瑜伽冥想引导词:LobeChat营造放松氛围

LobeChat&#xff1a;为冥想与心灵疗愈注入温度的AI交互引擎 在快节奏的现代生活中&#xff0c;越来越多的人开始寻求内心的平静。清晨五点&#xff0c;有人戴上耳机&#xff0c;在柔和语音的引导下缓缓睁开双眼&#xff1b;深夜入睡前&#xff0c;也有人依靠一段温柔的呼吸练习…

作者头像 李华
网站建设 2026/6/15 13:16:26

解决微软输入法无法添加多个动态自定义短语的问题

我们可以在微软输入法中的 设置 > 词库和自学习 > 用户自定义短语 > 添加或编辑自定义短语 的设置中&#xff0c;去管理自定义短语&#xff0c;并使用特殊占位符去设置为动态的短语。 比如&#xff0c;我们希望输入 riqi 的时候&#xff0c;能够打出形如 2025年12月1…

作者头像 李华
网站建设 2026/6/13 19:34:16

一键部署LobeChat镜像,开启高效AI交互新时代

一键部署LobeChat镜像&#xff0c;开启高效AI交互新时代 在企业智能化转型加速的今天&#xff0c;越来越多团队开始尝试引入大语言模型来提升工作效率。然而现实往往并不理想&#xff1a;API 调用混乱、界面体验割裂、数据安全堪忧——开发者疲于对接各种 SDK&#xff0c;非技…

作者头像 李华
网站建设 2026/6/14 18:30:00

Flink SQL INSERT 语句单表写入、多表分流、分区覆盖与 StatementSet

1. INSERT 语句是干嘛的 INSERT 用于把查询结果或字面量数据写入目标表&#xff08;sink 表&#xff09;。在 Flink 里&#xff0c;执行 INSERT 会提交一个 Flink Job&#xff08;流式作业通常是长期运行&#xff09;。2. Java 里怎么跑 INSERT&#xff1a;单条 executeSql vs …

作者头像 李华
网站建设 2026/6/15 13:54:56

Swagger2Word完全指南:快速将API文档转换为专业Word格式

Swagger2Word完全指南&#xff1a;快速将API文档转换为专业Word格式 【免费下载链接】swagger2word 项目地址: https://gitcode.com/gh_mirrors/swa/swagger2word Swagger2Word是一个功能强大的开源工具&#xff0c;专门用于将Swagger和OpenAPI接口文档转换为格式规范的…

作者头像 李华