背景音乐播放器
来源:互联网 发布:斗鱼登录网络异常 编辑:程序博客网 时间:2024/06/10 21:34
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="wyl music player" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnprev"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:text="上一首"></Button>
<Button
android:id="@+id/btnplay"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="播放" />
<Button
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="停止" />
<Button
android:id="@+id/btnnext"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="下一首" />
</LinearLayout>
</RelativeLayout>
MainActivity.Java
package com.example.musicplayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button Btnprev;
private Button Btnplay;
private Button Btnstop;
private Button Btnnext;
private ComponentName component;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
setListener();
}
//得到布局中的控件
private void findView(){
component=new ComponentName(this,MusicService.class);
Btnprev=(Button) findViewById(R.id.btnprev);
Btnplay=(Button) findViewById(R.id.btnplay);
Btnstop=(Button) findViewById(R.id.btnstop);
Btnnext=(Button) findViewById(R.id.btnnext);
}
//绑定控件事件
private void setListener(){
Btnprev.setOnClickListener(this);
Btnplay.setOnClickListener(this);
Btnstop.setOnClickListener(this);
Btnnext.setOnClickListener(this);
}
@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 void onClick(View v) {
//单击上一首时,就在intent中传递上一首歌曲的参数
if(v==Btnprev){
Intent intent=new Intent(MusicService.BTNPREV_ACTION);
intent.setComponent(component);
startService(intent);
}else if(v==Btnplay){
Intent intent=new Intent(MusicService.BTNPLAY_ACTION);
intent.setComponent(component);
startService(intent);
}else if(v==Btnstop){
Intent intent=new Intent(MusicService.BTNSTOP_ACTION);
intent.setComponent(component);
startService(intent);
}else{
Intent intent=new Intent(MusicService.BTNNEXT_ACTION);
intent.setComponent(component);
startService(intent);
}
}
}
MusicServices.Java
package com.example.musicplayer;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.provider.MediaStore;
import android.widget.Toast;
public class MusicService extends Service{
//定义需要显示的音乐的字段
String[]CursorCols=new String[]{
"audio._id AS_id",MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.MIME_TYPE,MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ARTIST_ID,MediaStore.Audio.Media.DURATION
};
private MediaPlayer mediaPlayer;
private Cursor cursor;
private int PlayPosition=0;
//注册意图
public static final String BTNPLAY_ACTION="com.wyl.music.BTNPLAY_ACTION";
public static final String BTNSTOP_ACTION="com.wyl.music.BTNSTOP_ACTION";
public static final String BTNNEXT_ACTION="com.wyl.music.BTNNEXT_ACTION";
public static final String BTNPREV_ACTION="com.wyl.music.BTNPPREV_ACTION";
@Override
public IBinder onBind(Intent intennt
) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
super.onCreate();
mediaPlayer=new MediaPlayer();
//通过一个URI可以获取所有音频文件
Uri MUSIC_URL=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor=getContentResolver().query(MUSIC_URL, CursorCols, "duration > 60000", null, null);
}
public void onStart(Intent intent,int startId){
//根据不同的action,做出不同的响应
String action=intent.getAction();
//播放
if(action.equals(BTNPLAY_ACTION)){
play();
//停止
}else if(action.equals(BTNSTOP_ACTION)){
stop();
//下一首
}else if(action.equals(BTNNEXT_ACTION)){
next();
//上一首
}else if(action.equals(BTNPREV_ACTION)){
prev();
}
}
private void prev() {
//得到上一首的歌曲
if(PlayPosition==0){
PlayPosition=cursor.getCount()-1;
}else {
PlayPosition--;
}
}
private void next() {
//得到下一首歌曲
if(PlayPosition==cursor.getCount()-1){
PlayPosition=0;
}else {
PlayPosition++;
}
}
private void stop() {
//停止音乐播放
stopSelf();
}
private void play() {
//初始化音乐播放器
inite();
}
private void inite() {
//充值MediaPlayer
mediaPlayer.reset();
//获取歌曲位置
String dataSource=getDateByPosition(cursor,PlayPosition);
//歌曲信息
String info=getInfoByPosition(cursor,PlayPosition);
Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT).show();
try{
//播放器绑定资源
mediaPlayer.setDataSource(dataSource);
//播放器准备
mediaPlayer.prepare();
//播放
mediaPlayer.start();
}catch(IllegalArgumentException el){
el.printStackTrace();
}catch(IllegalStateException el){
el.printStackTrace();
}catch(IOException el){
el.printStackTrace();
}
}
//获取当前播放歌曲演唱者及歌名
private String getInfoByPosition(Cursor c, int position) {
c.moveToPosition(position);
int titleColumn=c.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artistColumn=c.getColumnIndex(MediaStore.Audio.Media.ARTIST);
String info=c.getString(artistColumn)+" "+c.getString(titleColumn);
return info;
}
//根据位置来获取歌曲位置
private String getDateByPosition(Cursor c, int position) {
// TODO Auto-generated method stub
c.moveToPosition(position);
int dataColumn=c.getColumnIndex(MediaStore.Audio.Media.DATA);
String data=c.getString(dataColumn);
return data;
}
public void onDestroy(){
super.onCreate();
mediaPlayer.release();
}
}
- 博客背景音乐播放器
- 背景音乐播放器
- 播放背景音乐
- 背景音乐播放
- 背景音乐播放
- 背景音乐播放
- 背景音乐播放
- delphi播放mp3背景音乐
- Inno Setup 播放背景音乐
- C#播放背景音乐
- jsp播放背景音乐
- windows form播放背景音乐
- jsp 播放背景音乐
- 背景音乐自动播放代码
- QT 播放背景音乐
- js控制背景音乐播放
- iOS后台播放背景音乐
- Android 应用播放背景音乐
- Vivado_MicroBlaze_问题及解决方法_汇总(不定时更新)
- 内容包装:jQuery中wrap、wrapAll和wrapInner用法以及区别
- 如何选择开源许可证?
- 推荐系统:技术、评估及高效算法
- 线性代数:理解矩阵(一)
- 背景音乐播放器
- leetcode | Balanced Binary Tree
- iOS 异常处理
- java.lang.OutOfMemoryError: PermGen space
- Python入门
- 【SpringMVC之配置文件(一)】spring 里面的ApplicationContext.xml 和 xxx-servlet.xml分别的作用是什么?
- Python编写程序实现把某一文件夹下的所有文件重新命名为他的MD5值
- Longest Substring Without Repeating Characters
- UICollectionView基础