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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QI_Rainbow/article/details/50535487

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()
//1
		Intent 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;
		}



阅读更多
换一批

没有更多推荐了,返回首页