news 2026/6/23 21:04:51

手机端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/19 7:54:12

Scholar Inbox 订阅最新学术进展至邮箱

1. 注册账号可直接使用Google账号登陆用哪个邮箱注册&#xff0c;就会在哪个邮箱收到每天的最新论文精选2. 训练模型给模型提供启动数据——确认哪些文献与你的方向匹配、哪些文献不匹配&#xff0c;以便模型能够精准匹配你的研究方向scope。有两种方法&#xff1a;通过作者添加…

作者头像 李华
网站建设 2026/6/23 2:42:38

A.每日一题——3562. 折扣价交易股票的最大利润

题目链接&#xff1a;3562. 折扣价交易股票的最大利润&#xff08;困难&#xff09; 算法原理&#xff1a; 解法&#xff1a;01背包动态规划 297ms击败34.61% 时间复杂度O(N∗Budget) ①树形结构构建&#xff1a;将层级关系&#xff08;hierarchy&#xff09;转换为邻接表形式的…

作者头像 李华
网站建设 2026/6/19 7:44:15

圣默思 Teledyne DalsaFilr SWIR相机

Teledyne Dalsa&Filr SWIR相机 成像方案 什么是SWIR? 短波红外&#xff08;SWIR&#xff0c;一般定义为0.9 - 1.7μm波长范围内的光&#xff0c;但也可归入0.7 - 2.5μm波长范围&#xff09;成像使我们能够看到我们肉眼无法看到的物体。与物体本身发出的中波红外光…

作者头像 李华
网站建设 2026/6/23 10:47:01

Go 语言结构

Go 语言结构 概述 Go 语言,也称为 Golang,是由 Google 开发的一种静态强类型、编译型、并发型编程语言。自 2009 年发布以来,Go 语言以其简洁的语法、高效的并发处理能力和高性能而受到开发者的青睐。本文将深入探讨 Go 语言的各个结构特性,帮助读者更好地理解和应用 Go …

作者头像 李华