本篇文章给大家谈谈android音乐播放器项目源码,以及安卓项目音乐播放器源代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、android编程如何实现边下载边播放?
- 2、请问android2.3自带的音乐播放器有很多乱码,具体怎么修改源码?请指教!
- 3、基于Android音乐播放器源代码(正常播放、有列表)
- 4、android在线本地音乐播放器 简单点能看懂的 谁有源码给一个啊
android编程如何实现边下载边播放?
Android边下载边播放的实现应该需要用到多线程,一个线程下载,一个线程播放,具体可以找一个现有的音乐播放器源码: 研究看看
请问android2.3自带的音乐播放器有很多乱码,具体怎么修改源码?请指教!
我的答案绝对OK,1. 设置-》应用程序-》管理应用程序-》所有应用程序-》搜索到“媒体存储”-》执行“清除数据”
2. 重启系统,打开音乐播放器,等待媒体存储数据库重新加载
基于Android音乐播放器源代码(正常播放、有列表)
package my.android.players;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class mainActivity extends Activity {
private ListString myMusicList=new ArrayListString();
//当前播放歌曲的索引
private int currentListItem=0;
//音乐的路径
private static final String MUSIC_PATH="/sdcard/mp3";
//播放对象
private MediaPlayer myMediaPlayer;
private TextView m_TextView;
//播放按钮
private ImageButton m_start;
private ImageButton m_stop;
private ImageButton m_next;
private ImageButton m_last;
/*设定bIsPaused一开始为false */
private boolean bIsRun = false;
private boolean isplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
myMediaPlayer=new MediaPlayer();
musicList();
m_TextView=(TextView)findViewById(R.id.mtextview);
m_start=(ImageButton)findViewById(R.id.imgbtn_start);
m_stop=(ImageButton)findViewById(R.id.imgbtn_stop);
m_next=(ImageButton)findViewById(R.id.imgbtn_next);
m_last=(ImageButton)findViewById(R.id.imgbtn_last);
listener();
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
currentListItem = bundle.getInt("currentListItem");
isplay=bundle.getBoolean("isplay");
if(isplay==true)
{
bIsRun=false;
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem));
}
}
//监听事件
void listener(){
//开始
m_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
if(myMediaPlayer.isPlaying()==true)
{
myMediaPlayer.pause();
m_start.setImageResource(R.drawable.pause);
}
else
{
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem));
}
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//下一首
m_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nextMusic();
}
});
//上一首
m_last.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lastMusic();
}
});
//停止
m_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopMusic();
}
});
myMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
nextMusic();
}
});
}
//播放音乐
void playMusic(String path){
try {
if(bIsRun==false)
{
myMediaPlayer.reset();
myMediaPlayer.setDataSource(path);
myMediaPlayer.prepare();
myMediaPlayer.start();
/*
* 取出歌曲名的.mp3后缀
* */
String str=(myMusicList.get(currentListItem)).toString();
System.out.println(str);
String str1[]=str.split("\\.");
System.out.println(str1[0]);
m_TextView.setText(str1[0]);
}
else
{
myMediaPlayer.start();
}
m_start.setImageResource(R.drawable.start);
bIsRun=true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//下一首
void nextMusic(){
if(++currentListItem=myMusicList.size()){
currentListItem=0;
}
bIsRun=false;
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem));
}
//上一首
void lastMusic(){
if(--currentListItem0)
currentListItem=myMusicList.size()-1;
bIsRun=false;
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem));
}
//停止
void stopMusic() {
if (myMediaPlayer.isPlaying()) {
m_start.setImageResource(R.drawable.pause);
myMediaPlayer.stop();// 停止
bIsRun=false;
}
else
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem));
}
//当用户返回时结束音乐并释放音乐对象
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK){
new AlertDialog.Builder(mainActivity.this).setTitle("message")
.setIcon(android.R.drawable.dialog_frame)
.setMessage("你确定要离开吗?")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
myMediaPlayer.stop();
myMediaPlayer.release();
finish();
}
}).setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
return super.onKeyDown(keyCode, event);
}
/**
* 文件过滤器
*
* @author
*
*/
class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
return (filename.endsWith(".mp3"));
}
}
//绑定音乐
void musicList(){
try{
File home=new File(MUSIC_PATH);
if(!home.exists())
home.mkdirs();
if(home.listFiles(new MusicFilter()).length=0){
for(File file:home.listFiles(new MusicFilter())){
myMusicList.add(file.getName().toString());
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
package my.android.players;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Activity01 extends Activity {
//播放列表
private ListString myMusicList=new ArrayListString();
//当前播放歌曲的索引
private int currentListItem;
//音乐的路径
private static final String MUSIC_PATH="/sdcard/mp3";
//播放列表
private ListView m_ListView;
private boolean isplay=true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_ListView=(ListView)findViewById(R.id.lv_music);
musicList();
//当选择列表项时播放音乐
m_ListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView? arg0, View arg1, int arg2,
long arg3) {
currentListItem = arg2;
Intent intent = new Intent();
Bundle mBundle=new Bundle();
mBundle.putInt("currentListItem", currentListItem);
mBundle.putBoolean("isplay", isplay);
intent.putExtras(mBundle);
intent.setClass(Activity01.this,mainActivity.class);
startActivity(intent);
finish();
}
});
}
/**
* 文件过滤器
*
* @author
*
*/
class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
return (filename.endsWith(".mp3"));
}
}
//绑定音乐
void musicList(){
try{
File home=new File(MUSIC_PATH);
if(!home.exists())
home.mkdirs();
if(home.listFiles(new MusicFilter()).length=0){
for(File file:home.listFiles(new MusicFilter())){
myMusicList.add(file.getName().toString());
}
ArrayAdapterString musicList=new ArrayAdapterString
(Activity01.this,android.R.layout.simple_list_item_1, myMusicList);
m_ListView.setAdapter(musicList);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
android在线本地音乐播放器 简单点能看懂的 谁有源码给一个啊
下面这个例子是使用service和broadcastReceiver实现的简单音乐播放器,麻雀虽小,五脏俱全,百度搜索安卓无忧,点击源码看源码,你看演示:
MusiceService代码
package mm.shandong.com.testsimplemedia.service;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service {
int currentMusic = 0;//0表示没有音乐播放,1表示暂停,2表示正在播放,
int status;
MediaPlayer mp;
String[] musics = new String[]{"first.mp3", "second.mp3", "third.mp3"};
AssetManager am;
public MusicService() {
}
@Override
public void onCreate() {
am = getAssets();
mp = new MediaPlayer();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
currentMusic++;
currentMusic = currentMusic % musics.length;
playMusic();
status = 2;
Intent intentTemp = new Intent("Update_Music_State");
intentTemp.putExtra("status", status);
intentTemp.putExtra("currentMusic", currentMusic);
sendBroadcast(intentTemp);
}
});
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Get_Music_Control");
///动态注册播放器变化的广播接收者
MusicServiceReceiver musicServiceReceiver = new MusicServiceReceiver();
registerReceiver(musicServiceReceiver, intentFilter);
super.onCreate();
}
public class MusicServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int control = intent.getIntExtra("control", 0);
switch (control) {
case 0:
//暂停
if (status == 2) {
mp.pause();
status = 1;
}
break;
case 1:
///播放
if (status == 0) {
playMusic();
status = 2;
} else if (status == 1) {
mp.start();
status = 2;
}
break;
case 3:
break;
case 4:
//上一首
if (status == 0) {
if (currentMusic == 0) {
currentMusic = 2;
} else {
currentMusic--;
}
} else if (status == 1) {
if (currentMusic == 0) {
currentMusic = 2;
} else {
currentMusic--;
}
status = 0;
} else if (status == 2) {
if (currentMusic == 0) {
currentMusic = 2;
} else {
currentMusic--;
}
playMusic();
}
break;
case 5:
//下一首
if (status == 0) {
if (currentMusic == 2) {
currentMusic = 0;
} else {
currentMusic++;
}
} else if (status == 1) {
if (currentMusic == 2) {
currentMusic = 0;
} else {
currentMusic++;
}
status = 0;
} else if (status == 2) {
if (currentMusic == 2) {
currentMusic = 0;
} else {
currentMusic++;
}
playMusic();
}
break;
}
Intent intentTemp = new Intent("Update_Music_State");
intentTemp.putExtra("status", status);
intentTemp.putExtra("currentMusic", currentMusic);
sendBroadcast(intentTemp);
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
public void playMusic() {
try {
AssetFileDescriptor ad = am.openFd(musics[currentMusic]);
mp.reset();
mp.setDataSource(ad.getFileDescriptor(), ad.getStartOffset(), ad.getLength());
// mp.create(this,musics[currentMusic]);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
activity代码:
package mm.shandong.com.testsimplemedia;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import mm.shandong.com.testsimplemedia.service.MusicService;
public class TestSimpleMediaActivity extends AppCompatActivity {
ImageButton imageButton;
TextView textViewMp3Name;
TextView textViewMp3Author;
int status = 0;
String[] authors = new String[]{"张三", "李四", "王二"};
String[] names = new String[]{"first", "second", "thrid"};
int currentMusic = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_simple_media);
imageButton = (ImageButton) findViewById(R.id.imageButton);
textViewMp3Name = (TextView) findViewById(R.id.textViewMp3Name);
textViewMp3Author = (TextView) findViewById(R.id.textViewMp3Author);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("Get_Music_Control");
if (status == 2) {
intent.putExtra("control", 0);
} else {
intent.putExtra("control", 1);
}
sendBroadcast(intent);
}
});
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("Update_Music_State");
MusicStatusReceiver musicStatusReceiver = new MusicStatusReceiver();
//动态注册activity变化的广播接收者
registerReceiver(musicStatusReceiver, intentFilter);
Intent intent = new Intent(this, MusicService.class);
startService(intent);
Intent intentMusicReceiver = new Intent("Get_Music_Control");
intentMusicReceiver.putExtra("control", 3);
sendBroadcast(intentMusicReceiver);
}
///上一首
public void preMusic(View view) {
Intent intentMusicReceiver = new Intent("Get_Music_Control");
intentMusicReceiver.putExtra("control", 4);
sendBroadcast(intentMusicReceiver);
}
///下一首
public void nextMusic(View view) {
Intent intentMusicReceiver = new Intent("Get_Music_Control");
intentMusicReceiver.putExtra("control", 5);
sendBroadcast(intentMusicReceiver);
}
///定义操作音乐的广播接收者
public class MusicStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
status = intent.getIntExtra("status", 0);
currentMusic = intent.getIntExtra("currentMusic", 0);
switch (status) {
case 0:
textViewMp3Name.setText(names[currentMusic]);
textViewMp3Author.setText(authors[currentMusic]);
imageButton.setBackgroundResource(R.drawable.play32);
break;
case 1:
textViewMp3Name.setText(names[currentMusic]);
textViewMp3Author.setText(authors[currentMusic]);
imageButton.setBackgroundResource(R.drawable.play32);
break;
case 2:
imageButton.setBackgroundResource(R.drawable.pause32);
textViewMp3Name.setText(names[currentMusic]);
textViewMp3Author.setText(authors[currentMusic]);
break;
}
}
}
}
最后,您觉得回答满意的话,请关注一下我的微博,我的名字除了中文以外的。
关于android音乐播放器项目源码和安卓项目音乐播放器源代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
2、本站永久网址:https://www.yuanmacun.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
源码村资源网 » android音乐播放器项目源码(安卓项目音乐播放器源代码)
1 评论