news 2026/1/16 9:19:16

手机端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进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/17 0:47:11

饮食营养搭配:LobeChat生成一周食谱

饮食营养搭配&#xff1a;用 LobeChat 生成一周科学食谱 在现代快节奏的生活中&#xff0c;很多人知道“吃得健康”很重要&#xff0c;但真正落实却困难重重——不知道怎么搭配三餐、不清楚热量摄入是否合理、更别提长期坚持。传统的饮食建议往往来自固定模板或一次性咨询&…

作者头像 李华
网站建设 2025/12/29 8:17:26

数据可视化工具,助你打造好看图表

从职场汇报的严谨分析到学习笔记的灵感呈现&#xff0c;从营销方案的逻辑梳理到社交分享的吸睛设计&#xff0c;数据可视化早已成了信息传递的 “加分项”。一张配色舒服、逻辑清晰的图表&#xff0c;能让抽象数据变得直观易懂&#xff0c;甚至比文字更有感染力。但选对工具&am…

作者头像 李华
网站建设 2025/12/27 6:19:59

拯救大模型的逻辑短板:Nature文章预测符号主义AI与神经网络的联姻将引爆下一场技术革命

结合逻辑系统与驱动大语言模型的神经网络&#xff0c;正在成为人工智能领域最炙手可热的趋势&#xff0c;这种复古与前沿的碰撞或许才是通往通用人工智能的真正钥匙。虽然神经网络在当前AI领域独领风骚&#xff0c;但由于其缺乏逻辑推理能力和黑盒特性&#xff0c;绝大多数专家…

作者头像 李华
网站建设 2025/12/17 0:43:26

终极指南:免费部署Llama-2-7b-chat-hf打造企业级AI助手

终极指南&#xff1a;免费部署Llama-2-7b-chat-hf打造企业级AI助手 【免费下载链接】Llama-2-7b-chat-hf 项目地址: https://ai.gitcode.com/hf_mirrors/NousResearch/Llama-2-7b-chat-hf 还在为商业大模型的高昂费用而烦恼吗&#xff1f;Meta开源的Llama-2-7b-chat-hf…

作者头像 李华
网站建设 2025/12/31 1:56:29

LobeChat能否支持量子纠缠通信?超距作用原理与应用设想

LobeChat 与量子纠缠通信&#xff1a;一场关于现实与想象的对话 在人工智能产品日新月异的今天&#xff0c;一个看似简单的问题却频频浮现&#xff1a;“LobeChat 能不能用量子纠缠来通信&#xff1f;”这个问题背后&#xff0c;其实藏着两股力量的碰撞——一边是人们对“超距作…

作者头像 李华