求安卓音乐播放器的按专辑显示列表的实现代码 1C

想做一个音乐播放器,实现专辑列表的功能,按专辑显示列表,点击某个专辑能显示该专辑里对应的歌曲,
还有通过服务实现后台播放也有点问题,都写好了可就是播不出来,求大神指导,最好附上代码和注释,新手菜鸟一个

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

Android开发 简易音乐播放器列表无法显示

Android编程,设计一个简易的音乐播放器 主要是通过 ContentResolver 查询 MediaStore 多媒体文件获取文件名,列表显示的。目前真机调试是正常的,可以正常显示列表。 但是在模拟机上调试,列表显示是空白。logcat提示如下截图: ![图片说明](https://img-ask.csdn.net/upload/201906/05/1559744153_342027.png) MainActivity.java中代码如下: ``` package com.example.hp.ggmusic; import android.Manifest; import android.content.ContentResolver; import android.content.Context; import android.content.pm.PackageManager; import android.database.Cursor; import android.os.Build; import android.provider.MediaStore; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; import android.widget.ListView; import android.widget.MediaController; public class MainActivity extends AppCompatActivity { private final int REQUEST_EXTERNAL_STORAGE= 1; private static String[] PERMISSIONS_STORAGE={ Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; private ContentResolver mContentResolver; private ListView mPlaylist; private MediaCursorAdapter mCursorAdapter; //private Cursor mCursor; //音频文件是否属于音乐类型 private final String SELECTION = MediaStore.Audio.Media.IS_MUSIC + " = ?" + " AND " + MediaStore.Audio.Media.MIME_TYPE + " LIKE ?"; //音频文件的MIME类型,MP3对应的是“audio/mpeg” private final String[] SELECTION_ARGS ={ Integer.toString(1), "audio/mpeg"}; // @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContentResolver=getContentResolver(); //获取本地资源文件列表 mCursorAdapter=new MediaCursorAdapter(MainActivity.this); mPlaylist=findViewById(R.id.lv_playlist); mPlaylist.setAdapter(mCursorAdapter); if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) !=PackageManager.PERMISSION_GRANTED) { if(ActivityCompat.shouldShowRequestPermissionRationale( MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)){ }else { requestPermissions(PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE); } }else { initPlaylist(); } } public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults){ switch (requestCode){ case REQUEST_EXTERNAL_STORAGE: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ initPlaylist(); } break; default: break; } } private void initPlaylist(){ //查询本地资源 Cursor mCursor=mContentResolver.query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, SELECTION, SELECTION_ARGS, MediaStore.Audio.Media.DEFAULT_SORT_ORDER ); mCursorAdapter.swapCursor(mCursor); mCursorAdapter.notifyDataSetChanged(); } } ``` 编写的适配器MediaCursorAdapter.java中的代码如下 ``` package com.example.hp.ggmusic; import android.content.Context; import android.database.Cursor; import android.provider.MediaStore; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; import android.widget.TextView; public class MediaCursorAdapter extends CursorAdapter { private Context mContext; private LayoutInflater mLayoutInflater; public MediaCursorAdapter(Context context){ super(context,null,0); mContext=context; mLayoutInflater=LayoutInflater.from(mContext); } @Override public View newView(Context context, Cursor cursor, ViewGroup viewGroup){ View itemView =mLayoutInflater.inflate(R.layout.list_item, viewGroup,false); if (itemView != null){ ViewHolder vh = new ViewHolder(); vh.tvTitle = itemView.findViewById(R.id.tv_title); vh.tvArtist=itemView.findViewById(R.id.tv_artist); vh.tvOrder=itemView.findViewById(R.id.tv_order); vh.divider=itemView.findViewById(R.id.divider); itemView.setTag(vh); return itemView; } return null; } public class ViewHolder{ TextView tvTitle; TextView tvArtist; TextView tvOrder; View divider; } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder vh=(ViewHolder) view.getTag(); int titleIndex=cursor.getColumnIndex( MediaStore.Audio.Media.TITLE); int artistIndex=cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST); String title =cursor.getString(titleIndex); String artist =cursor.getString(artistIndex); int position=cursor.getPosition(); if (vh!=null){ vh.tvTitle.setText(title); vh.tvArtist.setText(artist); vh.tvOrder.setText(Integer.toString(position + 1)); } } } ```

eclipse开发安卓音乐播放器

在ddms中添加新的mp3文件但是运行avd时只能搜索到之前添加的搜索不到新添加的mo3文件而且在console中没有显示任何问题,这是怎么回事是我程序的问题还是虚拟器的问题

android 音乐专辑封面显示问题

一个没有专辑图片的音乐文件放在另一文件夹且该文件夹下有一张图片,android音乐播放器在读取该音乐文件专辑图片时不读去我提供的默认图片而是读文件夹下的图片。不知道是代码哪里有问题,希望有知道问题所在的大神,谢谢

为何音乐播放器代码在esclipse中没报错,点击按钮却没有实现功能,上图上代码,求前辈们指教

![图片说明](https://img-ask.csdn.net/upload/201604/24/1461461690_603209.png)![![图片说明](https://img-ask.csdn.net/upload/201604/24/1461461584_552949.png)图片说明](https://img-ask.csdn.net/upload/201604/24/1461461571_443206.png) Mainactivity package com.example.music; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.os.Bundle; import android.provider.MediaStore.Audio.Media; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity implements OnClickListener { List<Object> musiclists = new ArrayList<Object>(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Button play_pause, stop, onplay, downplay, close, exit; ActivityReceiver activityReceiver; public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION"; public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION"; Intent intentservice; // 定义音乐的播放状态 ,0X11 代表停止 ,0x12代表播放,0x13代表暂停 int status = 0x11; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); UIinit(); logic(); musicList(); activityReceiver = new ActivityReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(UPDATE_ACTION); registerReceiver(activityReceiver, filter); intentservice = new Intent(this, MusicService.class); startService(intentservice); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void UIinit() { play_pause = (Button) this.findViewById(R.id.play_pause); stop = (Button) this.findViewById(R.id.stop); onplay = (Button) this.findViewById(R.id.onplay); close = (Button) this.findViewById(R.id.close); exit = (Button) this.findViewById(R.id.exit); downplay = (Button) this.findViewById(R.id.downplay); } public void logic() { play_pause.setOnClickListener(this); stop.setOnClickListener(this); onplay.setOnClickListener(this); downplay.setOnClickListener(this); close.setOnClickListener(this); exit.setOnClickListener(this); } @Override public void onClick(View source) { Intent intent = new Intent(CTL_ACTION); switch (source.getId()) { case R.id.play_pause: { intent.putExtra("control", 1); break; } case R.id.stop: { intent.putExtra("control", 2); break; } case R.id.onplay: { intent.putExtra("control", 3); break; } case R.id.downplay: { intent.putExtra("control", 4); break; } case R.id.close: { this.finish(); break; } case R.id.exit: { stopService(intentservice); this.finish(); break; } } sendBroadcast(intent); } public class ActivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 获取Intent中的update消息,update代表播放状态 int update = intent.getIntExtra("update", -1); switch (update) { case 0x11: { play_pause.setText("播放"); status = 0x11; break; } // 控制系统进入播放状态 case 0x12: { // 播放状态下设置使用按钮 play_pause.setText("暂停"); // 设置当前状态 status = 0x12; break; } // 控制系统进入暂停状态 case 0x13: { play_pause.setText("播放"); status = 0x13; break; } } } } /* 播放列表 */ public void musicList() { // 取得指定位置的文件设置显示到播放列表 String[] music = new String[] { Media._ID, Media.DISPLAY_NAME, Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA }; Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, music, null, null, null); while (cursor.moveToNext()) { Music temp = new Music(); temp.setFilename(cursor.getString(1)); temp.setTitle(cursor.getString(2)); temp.setDuration(cursor.getInt(3)); temp.setArtist(cursor.getString(4)); temp.setData(cursor.getString(5)); musiclists.add(temp); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", cursor.getString(1)); map.put("artist", cursor.getString(4)); list.add(map); } ListView listview = (ListView) findViewById(R.id.musics); SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.musicsshow, new String[] { "name", "artist" }, new int[] { R.id.name, R.id.artist }); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int current, long id) { Intent intent=new Intent(CTL_ACTION); intent.putExtra("control", 5); intent.putExtra("current", current); sendBroadcast(intent); } }); } } TitanMusic.java package com.example.titan; public class TitanMusic { private String filename; private String Title; private int duration; private String artist; private String location; public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public int getDuration() { return duration; } public void setDuration(int duration) { this.duration = duration; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getData() { return location; } public void setData(String location) { this.location = location; } // {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, // MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, // MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA } TitanMusicService.java package com.example.titan; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.IBinder; import android.provider.MediaStore.Audio.Media; import android.util.Log; public class TitanMusicService extends Service { List<Object> musiclists = new ArrayList<Object>(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); MyReceiver serviceReceiver; MediaPlayer mPlayer; int status = 0x11; //歌曲下标 int current = 0; int count = 0; int flog = 0; //String filename; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onDestroy() { flog=0; mPlayer.stop(); mPlayer.release(); super.onDestroy(); } @Override public void onCreate() { flog=1; musicList(); count = list.size(); Log.e("Service", "onStart"); serviceReceiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(MainActivity.CTL_ACTION); registerReceiver(serviceReceiver, filter); mPlayer = new MediaPlayer(); mPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { current++; if (current >= count) { current = 0; } String filename = ((TitanMusic) musiclists.get(current)).getData(); playMusic(filename); } }); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { if(flog==2){ Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION); sendIntent.putExtra("update", status); //sendIntent.putExtra("current", current); sendBroadcast(sendIntent); } flog=2; } private void playMusic(String path) { try { /* 重置MediaPlayer */ mPlayer.reset(); /* 设置要播放的文件的路径 */ mPlayer.setDataSource(path); /* 准备播放 */ mPlayer.prepare(); /* 开始播放 */ mPlayer.start(); mPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer arg0) { //播放完成一首之后进行下一首 current--; if (current < 0) { current = count; } playMusic( ((TitanMusic) musiclists.get(current)).getData()); status = 0x12; } }); }catch (IOException e){} } /* 播放列表 */ public void musicList() { // 取得指定位置的文件设置显示到播放列表 String[] music = new String[] { Media._ID, Media.DISPLAY_NAME, Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA }; Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, music, null, null, null); while (cursor.moveToNext()) { TitanMusic temp = new TitanMusic(); temp.setFilename(cursor.getString(1)); temp.setTitle(cursor.getString(2)); temp.setDuration(cursor.getInt(3)); temp.setArtist(cursor.getString(4)); temp.setData(cursor.getString(5)); musiclists.add(temp); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", cursor.getString(1)); map.put("artist", cursor.getString(4)); list.add(map); } } public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int control = intent.getIntExtra("control", -1); switch (control) { case 1: { // 如果原来处于停止的状态 if (status == 0x11) { playMusic( ((TitanMusic) musiclists.get(current)).getData()); status = 0x12; } // 原来处于播放状态 else if (status == 0x12) { mPlayer.pause(); status = 0x13; } // 原来处于暂停状态 else if (status == 0x13) { mPlayer.start(); status = 0x12; } break; } case 2: { // 如果原来正在播放或暂停 if (status == 0x12 || status == 0x13) { mPlayer.stop(); status = 0x11; } break; } case 3: { current--; if (current < 0) { current = count; } playMusic( ((TitanMusic) musiclists.get(current)).getData()); status = 0x12; break; } case 4: { current++; if (current > count) { current = 0; } playMusic( ((TitanMusic) musiclists.get(current)).getData()); status = 0x12; break; } case 5: { current = intent.getIntExtra("current", -1);; playMusic( ((TitanMusic) musiclists.get(current)).getData()); status = 0x12; break; } } Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION); sendIntent.putExtra("update", status); // sendIntent.putExtra("current", current); sendBroadcast(sendIntent); } } } 布局代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/onplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一首" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:orientation="horizontal" > <Button android:id="@+id/play_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/downplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="下一首" /> </RelativeLayout> </LinearLayout> <!-- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="音乐列表" android:textColor="#ff0000ff" android:textSize="30sp" /> --> <ListView android:id="@+id/musics" android:layout_width="match_parent" android:layout_height="257dp" android:layout_weight="1.53" > </ListView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="最小化" /> <Button android:id="@+id/exit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="退出" /> </RelativeLayout> </LinearLayout> </LinearLayout> 歌曲布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" /> <TextView android:id="@+id/artist" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" /> </LinearLayout> ``` ```

android 如何获取第三方音乐播放器信息,歌名,进度

各位大神求助,最近项目要求获取像酷狗,qq,虾米,网易这些第三方播放器上的音乐信息到自己的应用上,在自己应用能显示当前播放音乐的歌手,歌名等信息,重要的是显示进度。之前用接收广播实现过,但有些播放器未发送广播 求助各位大神,好人有好报!谢谢了!

Android获取SQLite里面的本地音乐清单

编写音乐播放器的时候在写获取SQLite里面的本地音乐清单时遇到如下这个异常: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musicplayer/com.example.musicplayer.activity.Inventory}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference 下面是我的获取SQLite里面的本地音乐清单的代码类 public class Locality extends Activity { public List<Map<String, Object>> getCatalog() { Cursor mAudioCursor = this.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, // 查询条件 null, // 条件的对应?的参数 MediaStore.Audio.AudioColumns.TITLE);// 排序方式 List<Map<String, Object>> mListData = new ArrayList<Map<String, Object>>(); for (int i = 0; i < mAudioCursor.getCount(); i++) { mAudioCursor.moveToNext(); // 找到歌曲标题和总时间对应的列索引 int indexTitle = mAudioCursor .getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);// 歌名 int indexARTIST = mAudioCursor .getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);// 艺术家 /* * int indexALBUM = mAudioCursor * .getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM);//专辑 */ String strTitle = mAudioCursor.getString(indexTitle); String strARTIST = mAudioCursor.getString(indexARTIST); /* String strALBUM = mAudioCursor.getString(indexALBUM); */ HashMap<String, Object> nowMap = new HashMap<String, Object>(); nowMap.put("SongName", strTitle); nowMap.put("SongMessage", strARTIST); mListData.add(nowMap); } return mListData; } } 提示我是这一行出错: Cursor mAudioCursor = this.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,// 字段 没有字段 就是查询所有信息 相当于SQL语句中的 “ // * ” null, // 查询条件 null, // 条件的对应?的参数 MediaStore.Audio.AudioColumns.TITLE);// 排序方式

java安卓开发中,通过raw文件夹播放音乐,如何实现上一首下一首功能?

我是以mediaplayer进行播放的: mp = MediaPlayer.create(MainActivity.this,R.raw.music); mp.start(); 比如raw中有两个mp3文件,但是如何进行上下一首的切换? 希望能给我一个小例子看看,例子看得比较明白。

Android如何把获取到音乐文件的数据用simpleAdapter填充到listview列表当中去

* 专门创建一个类用来获取歌曲的信息 */ public class AudioUtils { /* * * */ //创建一个音乐实体类的实例的方法 public static ArrayList<Song> getAllSongs(Context context) { // 初始化一个音乐实体类集合对象,用来存储下面获取到的音乐信息 ArrayList<Song> songs = null; // 通过内容提供者的查询接口来获取到手机里面的音乐文件 Cursor cursor = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, // 这里新建一个数组告诉提供者要返回的列 new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA }, //设置查询的条件 MediaStore.Audio.Media.MIME_TYPE + "=? or " + MediaStore.Audio.Media.MIME_TYPE + "=?", new String[] { "audio/mpeg", "audio/x-ms-wma" }, null); // 实例化一个Song数组实体类对象 songs = new ArrayList<Song>(); if (cursor.moveToFirst()) { //当光标移动到第一行时,执行下面的方法 Song song = null;//新建一个歌曲类实例化对象,把cursor读出来的信息文件,存进去 do { // 实例化Song类对象 song = new Song(); //在把获取到的值通过实体类对象赋值到实体类当中去 // 文件名 song.setFileName(cursor.getString(1)); // 歌曲名 song.setTitle(cursor.getString(2)); // 时长 song.setDuration(cursor.getInt(3)); // 歌手名 song.setSinger(cursor.getString(4)); // 专辑名 song.setAlbum(cursor.getString(5)); // 年代 if (cursor.getString(6) != null) { song.setYear(cursor.getString(6)); } else { song.setYear("未知"); } // 歌曲格式 if ("audio/mpeg".equals(cursor.getString(7).trim())) { song.setType("mp3"); } else if ("audio/x-ms-wma".equals(cursor.getString(7).trim())) { song.setType("wma"); } // 文件大小 if (cursor.getString(8) != null) { float size = cursor.getInt(8) / 1024f / 1024f; song.setSize((size + "").substring(0, 4) + "M"); } else { song.setSize("未知"); } // 文件路径 if (cursor.getString(9) != null) { song.setFileUrl(cursor.getString(9)); } //把获得到的数据传输到ArrayList列表当中去 songs.add(song); // 当光标移动到下一行的时候,继续循环,当获取到的内容没有时,光标停止移动,不满足循环跳出去 } while (cursor.moveToNext()); //关闭光标释放资源 cursor.close(); } return songs; } 上面是获取音乐文件的代码,音乐实体类,主界面上面就是一个一个Button,点击的时候把获取到的数据显示到下面的按钮中间去

Android如何实现应用程序启动后将正在播放的音乐暂停?

自己写了一个Android程序,程序启动后,如何使得手机正在播放的音乐暂停呢?

在android系统中如何播放、暂停、停止歌曲?

我在程序中设置了播放、暂停、停止三个按钮。 当我点击播放按钮,这个按钮就消失了,显示的是暂停按钮。反之亦然。 当我点击播放按钮时,程序不出错。再点击暂停按钮时,就会出错。 贴出代码: package com.mpIlango; import java.io.IOException; import java.util.ArrayList; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; public class MpIlangoActivity extends Activity implements OnCheckedChangeListener { /** Called when the activity is first created. */ MediaPlayer song1,song2,song3; int whatsong = 0; private ArrayList<Integer> songIds; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RadioGroup rgMusic = (RadioGroup) findViewById(R.id.rgMusic); songIds = new ArrayList<Integer>(); songIds.add(R.raw.fluet); songIds.add(R.raw.airtel); songIds.add(R.raw.titanic); final Button bPlay = (Button) findViewById(R.id.bPlay); final Button bStop = (Button) findViewById(R.id.bStop); final Button bPause = (Button) findViewById(R.id.bPause); bPause.setVisibility(View.GONE); rgMusic.setOnCheckedChangeListener(this); bPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(song1!=null) { song1.release(); } if(song2!=null) { song2.release(); } if(song3!=null) { song3.release(); } switch (whatsong) { case 1: try { song1 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(0)); song1.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } song1.start(); bPlay.setVisibility(View.GONE); bPause.setVisibility(View.VISIBLE); break; case 2: try { song2 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(1)); song2.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } song2.start(); bPlay.setVisibility(View.GONE); bPause.setVisibility(View.VISIBLE); break; case 3: try { song3 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(2)); song3.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } song3.start(); bPlay.setVisibility(View.GONE); bPause.setVisibility(View.VISIBLE); break; } } }); bPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { bPlay.setVisibility(View.VISIBLE); bPause.setVisibility(View.GONE); if(song1.isPlaying()){ song1.pause(); } if(song2.isPlaying()){ song2.pause(); } if(song3.isPlaying()){ song3.pause(); } } }); bStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(song1!=null){ song1.release(); } if(song2!=null){ song2.release(); } if(song3!=null){ song3.release(); } } });; } @Override public void onCheckedChanged(RadioGroup group, int rbId) { switch (rbId) { case R.id.rbMusic1: whatsong = 1; break; case R.id.rbMusic2: whatsong = 2; break; case R.id.rbMusic3: whatsong = 3; break; } } }

android如何实现播放ape格式音乐

最近做毕设想做一个简单的播放器,然后想要能够支持ape,flac等格式的无损音乐。 我现在直接用getContentResolver().query()可以显示出来手机内的ape音乐,但是没法播放。请问是要自己解码么,有没有可以直接使用的类或者包之类的?

如何实现 本地html页面能播放mp3

我做了一个android应用是读取本地的html,模拟器中html里的超链接跳转没问题,图片也可以显示。html里的mp3文件却无法打开,请问高手怎么解决。 搞了两天了。。。。。郁闷啊。。谢谢谢谢。。 下面是我的其中一段html代码。 <td width="284"> <table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr>  <td width="284" valign="middle"> <a href="../voices/anyengmenu.mp3"><font size="5">Do you have a menu in English? </font><img src="../images/design/icon_voice.jpg" alt="" border="0" /></a>   </td> </tr> </table> </td> ZXD啊。谢谢了啊 [b]问题补充:[/b] embed标签我也试过了,也是不行。 [b]问题补充:[/b] 在html里加入object或embed 标签的话,模拟器编译不了。

startservice之后onstartcommand却没有启动

mainactivity代码 package com.example.test_01; //import android.R; import java.util.List; import com.example.service.PlayerService; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageButton; public class MainActivity extends ActionBarActivity implements MainFragment.OnMainFragmentInteractionListener, MyMusicFragment.OnMyMusicFragmentInteractionListener { //记录当前播放器的状态 private boolean isPause; private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; private MainFragment mainFragment; private MyMusicFragment myMusicFragment; //查找歌曲的类 private FindSongs finder; //歌曲列表 public static List<Mp3Info> mp3Infos; //音乐的位置 public static int music_position; //播放按钮控件 private ImageButton play_button; /* 这个方法是activity和fragment通信的一种方法 在MainFragment中调用这个方法,可以在activity中做出相应的反应 */ public void onMainFragmentInteraction(int msg){ /* 对其中的参数msg做出判断,如果为CHANGE_TO_MY_MUSIC_FRAGMENT 则执行跳转 */ if(msg == AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT){ /* 在这里并没有直接切换Fragment 而是调用了activity实现MyMusicFragment的那个接口 对后面的开发能带来一点便利之处 */ onMyMusicFragmentInteraction (AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT); } } public void onMyMusicFragmentInteraction(int msg){ myMusicFragment = new MyMusicFragment(MainActivity.this); //创建了MyMusicFragment的实例 FragmentManager fragmentManager = getFragmentManager(); //得到FragmentManager FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //得到fragmentTransaction if(msg == AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT){ fragmentTransaction.replace(R.id.fragment_layout, myMusicFragment); fragmentTransaction.addToBackStack(null); //这句话是将被替换的MainFragment加入到一个专门存放fragment的栈中,在回退的时候显示上一个Fragment fragmentTransaction.commit(); } if(msg == AppConstant.PlayerMsg.BACK_TO_MAIN_FRAGMENT){ fragmentTransaction.replace(R.id.fragment_layout, mainFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } } public void onMyMusicFragmentInteraction(int msg,int position){ if(msg == AppConstant.PlayerMsg.LIST_CLICK){ if (mp3Infos != null) { // Log.i("dada", "-------------here------------"); isPause = false; initService(position); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); music_position = 0; finder = new FindSongs(); mp3Infos = finder.getMp3Infos(getContentResolver()); play_button = (ImageButton)findViewById(R.id.play_button); //mainFragment = new MainFragment(); //创建了刚才定义的MainFragment实例 mainFragment = new MainFragment(MainActivity.this); fragmentManager = getFragmentManager(); //得到FragmentManager fragmentTransaction = fragmentManager.beginTransaction(); //得到fragmentTransaction,用于管理fragment的切换 fragmentTransaction.replace(R.id.fragment_layout, mainFragment).commit(); //将MainActivity里的布局模块fragment_layout替换为mainFragment } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void initService(int position) { music_position = position; Mp3Info mp3Info = mp3Infos.get(position); /* 这里新建了一个Intent 里面存放各种即将传给Service的数据 要启动自定义PlayerService类 还需要在AndroidManifest中加入如下代码 <service android:name="com.example.dada.myapplication.PlayerService" android:exported="false" > </service> */ //Intent intent = new Intent("com.example.service.PlayerService"); Intent intent = new Intent(MainActivity.this, PlayerService.class); play_button.setImageResource(R.drawable.play_pause); intent.putExtra("url", mp3Info.getUrl()); intent.putExtra("title", mp3Info.getTitle()); intent.putExtra("artist", mp3Info.getArtist()); intent.putExtra("album", mp3Info.getAlbum()); intent.putExtra("album_id", mp3Info.getAlbum_id()); intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG); intent.setClass(MainActivity.this, PlayerService.class); startService(intent); Log.w("dada", "------------hahaha----------"); } //@Override // public void onFragmentInteraction(int msg) { // // TODO Auto-generated method stub // // } } service代码 package com.example.service; import java.io.IOException; import com.example.test_01.AppConstant; import com.example.test_01.AppConstant.PlayerMsg; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; public class PlayerService extends Service implements AppConstant { public static MediaPlayer mediaPlayer = null; private int current_position; private String musicPath; private String music_artist; private String music_title; private String notification_msg; //private MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent intent) { Log.w("dada", "-------------here------------"); return null; } /* * onStartCommand()方法就是刚启动service时调用的一个方法 * 里面第一个参数intent就是在activity中的那个intent * 因此里面包含着被点击的歌曲相关信息 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { //MediaPlayer是android中自带的一个播放器类,直接实例化后使用即可 mediaPlayer = new MediaPlayer(); Log.w("dada", "-------------here------------"); try { int msg = intent.getIntExtra("MSG", 0); musicPath = intent.getStringExtra("url"); //从intent中拿出歌曲的路径 Log.w("dada", "-------------here------------"); if (msg == AppConstant.PlayerMsg.PLAY_MSG) { playMusic(0); } } catch (Exception e) { e.printStackTrace(); } return 0; } private void playMusic(int position) { Log.w("dada", "-------------here------------"); try { mediaPlayer.reset(); mediaPlayer.setDataSource(musicPath); mediaPlayer.prepare(); mediaPlayer.setOnPreparedListener(new MyPreparedListener(position)); } catch (IOException e) { e.printStackTrace(); } } private class MyPreparedListener implements MediaPlayer.OnPreparedListener { private int position; public MyPreparedListener(int position) { this.position = position; } public void onPrepared(MediaPlayer mp) { if (position > 0) Log.i("dada", "-------------here------------"); mediaPlayer.seekTo(position); mediaPlayer.start(); } } private void stopMusic() { if (mediaPlayer != null) { mediaPlayer.pause(); } } public void onDestory() { Log.e("dada", "-------------here------------"); if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } } startService(intent);后面的log显示了,但是service却没有反应

2019 Python开发者日-培训

2019 Python开发者日-培训

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

设计模式(JAVA语言实现)--20种设计模式附带源码

设计模式(JAVA语言实现)--20种设计模式附带源码

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

java后台+微信小程序 实现完整的点餐系统

java后台+微信小程序 实现完整的点餐系统

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

2019 AI开发者大会

2019 AI开发者大会

玩转Linux:常用命令实例指南

玩转Linux:常用命令实例指南

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

4小时玩转微信小程序——基础入门与微信支付实战

4小时玩转微信小程序——基础入门与微信支付实战

Git 实用技巧

Git 实用技巧

Python数据清洗实战入门

Python数据清洗实战入门

使用TensorFlow+keras快速构建图像分类模型

使用TensorFlow+keras快速构建图像分类模型

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

程序员的算法通关课:知己知彼(第一季)

程序员的算法通关课:知己知彼(第一季)

MySQL数据库从入门到实战应用

MySQL数据库从入门到实战应用

机器学习初学者必会的案例精讲

机器学习初学者必会的案例精讲

手把手实现Java图书管理系统(附源码)

手把手实现Java图书管理系统(附源码)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

.net core快速开发框架

.net core快速开发框架

玩转Python-Python3基础入门

玩转Python-Python3基础入门

Python数据挖掘简易入门

Python数据挖掘简易入门

微信公众平台开发入门

微信公众平台开发入门

程序员的兼职技能课

程序员的兼职技能课

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

HoloLens2开发入门教程

HoloLens2开发入门教程

微信小程序开发实战

微信小程序开发实战

Java8零基础入门视频教程

Java8零基础入门视频教程

Python可以这样学(第一季:Python内功修炼)

Python可以这样学(第一季:Python内功修炼)

C++语言基础视频教程

C++语言基础视频教程

Python可以这样学(第四季:数据分析与科学计算可视化)

Python可以这样学(第四季:数据分析与科学计算可视化)

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

Python数据分析与挖掘

Python数据分析与挖掘

微信小程序开发实战之番茄时钟开发

微信小程序开发实战之番茄时钟开发

相关热词 c# 开发接口 c# 中方法上面的限制 c# java 时间戳 c#单元测试入门 c# 数组转化成文本 c#实体类主外键关系设置 c# 子函数 局部 c#窗口位置设置 c# list 查询 c# 事件 执行顺序
立即提问