实现点击音乐文件跳转到音乐播放器并自动播放音乐

来源:互联网 发布:细说php第二版电子书 编辑:程序博客网 时间:2024/05/21 01:30

1、AndroidManifest..xml

        <activity            android:name="com.happyplayer.ui.MainActivity"            android:excludeFromRecents="true"            android:exported="true"            android:taskAffinity="" >            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <category android:name="android.intent.category.DEFAULT" />                <data android:scheme="file" />                <!-- <data android:scheme="http" />                <data android:scheme="content" /> -->                <data android:mimeType="audio/*" />                <!-- <data android:mimeType="application/ogg" />                <data android:mimeType="application/x-ogg" />                <data android:mimeType="application/itunes" /> -->            </intent-filter>        </activity>

2、MainActivity.java

onCreate()
//1Intent mediaPlayerServiceIntent =new Intent(MainActivity.this, MediaPlayerService.class);mediaPlayerServiceIntent.setData(getIntent().getData());startService(mediaPlayerServiceIntent);//2                Intent intent = getIntent();uri = intent.getData();if (uri!=null&&!"".equals(uri)) {SongMessage songMessage = new SongMessage();List<SongInfo> songInfos = SongDB.getSongInfoDB(this).getAllSong();SongInfo songs = new SongInfo();for (int i = 0; i < songInfos.size(); i++) {if (songInfos.get(i).getPath().equals(uri.getPath()) ) {songs = songInfos.get(i);break;}}songMessage.setType(SongMessage.INIT);songMessage.setSongInfo(songs);ObserverManage.getObserver().setMessage(songMessage);}

3、MediaPlayerService.java

onStart()Uri uri = intent.getData();if (uri!=null&&!"".equals(uri)) {play(uri.getPath());}
onPlay(String songInfoPath)if (songInfoPath==null||"".equals(songInfoPath)) {songInfo = MediaManage.getMediaManage(context).getPlaySongInfo();}else {List<SongInfo> songInfos = SongDB.getSongInfoDB(this).getAllSong();SongInfo songs = new SongInfo();for (int i = 0; i < songInfos.size(); i++) {if (songInfos.get(i).getPath().equals(songInfoPath) ) {songs = songInfos.get(i);break;}}songInfo = songs;}



0 0