十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
首先service的启动方式有两种,Context.startService()方式启动和Context.bindService()方式启动.前者如果你的程序退出时不停止Service,它会在后台一直运行.后者是跟你的程序绑定了,你的程序退出时Service也停止了.详细的你去查一下.用Service的好处就是当你的程序进入后台时,不影响音乐的播放.如果没有把播放音乐写在Service中当你的播放器进入后台或者退出时音乐就停止了.
在长宁等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、成都网站设计 网站设计制作按需求定制制作,公司网站建设,企业网站建设,成都品牌网站建设,全网整合营销推广,外贸网站建设,长宁网站建设费用合理。
下面把具体编译步骤描述如下,假定NDK安装在~/android-ndk-r7:
1. 首先从FFmpeg官网下载最新的release版本源码ffmpeg-0.11.tar.gz解压缩到Android源码树的ffmpeg/下。
2 准备一个编译脚本build_android.sh并放在ffmpeg/下面,这个脚本也是Rockplayer提供的,需做一些修改,其内容附在后面。我目前用的也会附在后面。
3 在ffmpeg目录下运行./build_android.sh开始编译FFmpeg,编译好的libffmpeg.so会放在文件夹android里面,一共有3个版本分别对应3种ARM体系结构,包括armv7-a、armv7-a-vfp、armv6_vfp,根据所运行的硬件平台选取其中一个版本。为了编译使用FFmpeg的程序时可以方便地找到libffmpeg.so,可将它复制到$OUT/system/lib/和$OUT/obj/lib/,当然这一步也可以加在build_android.sh中做。
4. 接下来就是编译可执行文件ffmpeg了,这个工具可以在命令行下完成FFmpeg提供的几乎所有功能包括编码、解码、转码等,也是用来调试和验证很有用的工具。其实上述编译完后在$ANDROID_BUILD_TOP/external/ffmpeg/下也会生成ffmpeg,但是在设备上无法运行。为了编出能在设备上运行的ffmpeg,可以写一个简单的Android.mk,
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();
}
}
}
我的答案绝对OK,1. 设置-》应用程序-》管理应用程序-》所有应用程序-》搜索到“媒体存储”-》执行“清除数据”
2. 重启系统,打开音乐播放器,等待媒体存储数据库重新加载
严格来说,线程是共享资源的,所以线程之间是不存在通信的,Android里面的Handle是用来解决异步调用的,这个观念很重要,想明白了,代码也就写的更合理了。
进程之间内存等资源是隔离的,而AIDL,是Android提供的跨进程通信IPC工具Binder的具体使用方法,跟其他Linux跨进程通信(socket,管道,能存共享等)在概念上没啥差。所以要实现跨进程的内存访问(比如数据传输,函数跨进程同步调用等)就需要了AIDL了,当然了AIDL并不是Android中跨进程通信的唯一选择,socket(systemServer进程与zygote进程,pkms调用install进程,MountService与vold进程通信都是通过socket),共享内存(sqlite3查询就用了)等都可以用的,但是没有Binder通过AIDL好用罢了。
下面这个例子是使用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;
}
}
}
}
最后,您觉得回答满意的话,请关注一下我的微博,我的名字除了中文以外的。